There are times where you might want to change the hostname of your Linux system. Perhaps you changed your domain name or when you first provisioned the system, you called it something else and the name just doesn’t seem to work anymore. Changing the hostname is a common procedure, and I’ll walk you through the steps.
What is a hostname?
As the name suggests, it’s a name for your host. If you’re from the Windows world, this is the “Computer Name”. At the end of the day, both are fundamentally the same thing – a way to uniquely identify the system on the network in a human-friendly manner.
When I say unique, I don’t mean in the sense like an IP address or MAC address. You can easily have two systems called “webserver” on the same network. But it is good to make these unique so that you can easily identify the systems. Additionally, hostnames and DNS go hand in hand. If you have two “webserver” machines within the same DNS zone, your requests could go to either one and you won’t easily know. By setting a unique hostname, you know exactly where your requests are going.
Why change a hostname?
Sometimes you might miss where the hostname is set during system installation and you end up with a hostname like “localhost.localhost” or some installers might not even give you the option to set a hostname and will randomly generate one for you. Or maybe you just mistyped or you have changed a domain name.
Changing a hostname is actually very easy on Linux with the
How to change your hostname
First, we’re going to cover using
hostnamectl which is used on modern systems with systemd. Let’s go ahead and change your hostname! To get started, check your current hostname:
You should see something like the following:
Static hostname: system1.example.com
Icon name: computer-vm
Machine ID: 8bde47de792244a1bcb2b77e05a76545
Boot ID: 698e3ddfc67441f6a7dea1e2a6365348
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-1160.53.1.el7.x86_64
This tells us that the computer hostname is system1.example.com.
To update our hostname, run the following:
user@computer$ hostnamectl set-hostname ‘web-server-01.example.com’
Note: You won’t see your terminal prompt update unless you log out and log back in.
Next, update your hosts file. This is good practice because it ensures that the server is getting its own hostname from itself. Open your hosts file:
user@computer$ sudo vi /etc/hosts
To edit a file in vi, press the I key. Use your arrow keys to move the cursor and make your changes. Once you’re done, press the ESC key and then type
:wq! and press enter. This tells vi to write the changes to the file and quit. It’s best to learn the basics of vi because not every system ships with nano and you may find yourself on an air-gapped system or a system where you do not have sudo permission to download it.
You should see two lines like the following:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
Update the lines to include your new hostname at the end:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 web-server-01 web-server-01.example.com ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 web-server-01 web-server-01.example.com
Save the file and you’re done. Reboot your system to be sure the hostname properly applies and no programs are caching it.
What if you have an older system or one that doesn’t use systemd? You’ll use the
hostname command. This command requires some additional work, but it’s not too bad. First, check your current hostname by typing:
You’ll see your hostname returned. For example:
Now, update your hostname.
Red Hat/Fedora/CentOS systems:
user@computer$ sudo vi /etc/sysconfig/network
and then find the HOSTNAME line and change it to your new hostname:
Save the file and exit.
user@computer$ sudo vi /etc/hostname
Delete your old hostname and replace it with your new hostname.
Save the file and exit.
Now, exit the system hosts file, find all instances of your old hostname and update them accordingly. Save the file and exit. You may need to reboot your system for programs to pick up the new hostname.