To make it easier for us humans to remember, we also assign a hostname using words. If the need arises, here is how to change hostname in Linux.
The History of Hostnames
Before the advent of DNS servers, every computer on a network needed a list of hostnames and their associated IP addresses. On Linux, this was found in the /etc/hosts file. We do have DNS servers now, but it’s still up to the computer to broadcast what its name and address is. Today, modern Linux systems have three different hostnames. The one most commonly seen by human eyes is the pretty hostname. This can contain symbols and spaces that aren’t useful in networking, but help make it more user-friendly. The other two are:
The transient hostname, which is how the network views your server or desktop computer.The static hostname, which is set by the Linux kernel. This is the main type of hostname that you’ll be changing.
If you use it, you should configure your pretty hostname to be something easily remembered, and allow the transient and static hostnames to be variations of it. Linux will automatically remove symbols from the pretty hostname and replace spaces with hyphens.
How to View Your Hostname in Linux
Before you set out to change the hostname, let’s look at what it’s currently configured to. After all, you may have already set the hostname to precisely what you want during installation. If not, it’s easy to change. The most basic way to view your Linux computer’s hostname is to just look at the prompt in a terminal. You’ll typically see the following structure at the beginning of every command: You can also check the computer’s hostname by issuing the command hostname from the terminal.
To get your computer’s hostname along with a wealth of further information, you’ll use the hostnamectl command.
As you can see, this command also provides you with your computer’s unique Machine ID, Boot ID, and information about virtualization, the operating system, the Linux kernel, and your processor architecture.
How to Change Your Hostname in Linux
You can use either the hostname or hostnamectl commands to change your Linux computer’s hostname. Another command, sysctl, will allow you to change the kernel parameter for your computer’s hostname without needing to reboot. First, though, you’ll need to know a few basic rules about static and transient hostnames:
They must be at least 2 characters long, and no more than 64 characters.A hostname can contain digits, lower-case letters, hyphens, and dots.Your computer’s hostname must start and end with either a letter or a number.
The first, and quickest, way to change a hostname in Linux is using the hostname command. You need to be logged in as root, or issue the command using sudo.
As you can see, the command won’t return any output. You can verify the hostname was changed by running the hostname command again without any arguments. Also, you’ll notice the hostname won’t reflect the modification in your terminal. However, once you close and reopen the terminal, you’ll see the change. Next, you can use hostnamectl to make the change. In fact, hostnamectl can change all three types of hostnames, not just the static hostname. By default, hostnamectl set-hostname will modify all three types of hostnames. You won’t see any confirmation, but can again use hostnamectl by itself to see your changes. Advanced users can use different flags and arguments to change this behavior. You can find a full description of these in the online manual page for hostnamectl. Finally, you can also use sysctl to change the kernel hostname.
As you can see, if not applied to all three hostname types, this results in your system having a transient hostname different from your static and pretty hostnames.
Using the GUI to Change Your Hostname in Linux
If you use the Gnome desktop, there’s one more way to change your computer’s hostname. If you go to Settings > About, you change the Device Name field to set a different hostname.
Issuing the hostnamectl command after this shows how Linux adapts the pretty hostname to apply to the static version. It changes capital letters to lower case, swaps spaces for hyphens, and removes any symbols.
Properly Administering Linux Doesn’t Have to Be Difficult
As you see, changing your hostname in Linux is fairly straightforward and easy. It’s so easy, in fact, that I’ve inadvertently done so a couple of times without realizing it. This brings us to one final point: be intentional and deliberate about any changes you’re making and document them. If you’re managing a number of servers or Linux desktops, you may want to institute a policy regarding hostnames. If so, you can (and should) incorporate this into the same document where you spell out security and password change policies, user management, file and disk management schedules, and other important details about your Linux computers. Comment Name * Email *
Δ Save my name and email and send me emails as new comments are made to this post.