How to Change Your Linux Hostname

๐Ÿ“… February 18, 2024
“How do I change the name of my computer without reinstalling it?”

The name of your Linux computer is called a hostname, and it can be changed anytime by the administrator.

Most of the time, the name you assign to your Linux installation need never be changed, but there are situations where this is necessary. For example, duplicating VMs or accidental duplicate naming by two different people. If you use LibreNMS, then you cannot add devices with duplicate hostnames. Therefore, a hostname might need to be changed.

No, you do not need to reinstall your entire OS. In fact, it is easy with a few commands and a reboot. Here are two ways to rename the hostname.

Check the Hostname

There is a dedicated command, hostname, that returns the hostname from a command line.

hostname

If you desire more information, use hostnamectl.

hostnamectl

Result will be something like this:

Static hostname: pihole
Icon name: computer-vm
Chassis: vm
Machine ID: 10111111111111111111111111111111188
Boot ID: d8vvvvvvvvvvvvvvvvvvvvvvvvvv430
Virtualization: oracle
Operating System: Linux Mint 21.2 
Kernel: Linux 6.4.10-060410-generic
Architecture: x86-64
Hardware Vendor: innotek GmbH
Hardware Model: VirtualBox

Static hostname is the line containing the current hostname that appears in the terminal prompt. This is what we want to change.

(These commands were tested in Ubuntu Server and Linux Mint 21.2 running as VirtualBox VMs.)

You can also just look at your default prompt in gnome-terminal.

myusername@pihole

The hostname is pihole.

And if that is not enough, Linux stores its hostname in the file /etc/hostname, so we can look at this file.

cat /etc/hostname

Changing the Hostname

We have a VM named pihole that conflicts with another host on the same network also named pihole. Not good. We need to rename this system to humperdink. Yes, humperdink. Didn’t see that one coming, did you?

As you might be aware, there is more than one way to skin the proverbial penguin on a Linux system. Here are two of them.

Renaming Technique 1 (The Long Way): File Editing

(Use whatever text editor that you prefer, but make sure you have sudo permission since the files require administrator rights.)

Step 1. Edit /etc/hostname

sudo nano /etc/hostname

Just change the existing name pihole to humperdink. This file only contains one line with one word. Save and exit.

Keep in mind that the best hostnames should be single words only. No spaces or special characters. Keep it easy to type.

Step 2. Edit /etc/hosts

sudo nano /etc/hosts

Locate the current hostname pihole, and change it to humperdink.

From this:

127.0.0.1 localhost
127.0.1.1 pihole

To this:

127.0.0.1 localhost
127.0.1.1 humperdink

Save and exit.

Step 3. Update GRUB

sudo update-grub

I found that simply rebooting without updating GRUB will not affect the hostname change.

Step 4. Reboot

Editing these two files is not enough to change the name. Reboot the system. Logging out is not enough since other users might still be using the system. We need to perform an actual reboot of the Linux system to make the new hostname take effect.

sudo reboot

Upon logging back in following the reboot, the hostname will now be humperdink.

Renaming Technique 2 (The Shorter Way): hostnamectl

Using hostnamectl, we can simplify the process a little. Let’s rename humperdink into pi-pit. (“Hey, what is a hyphen doing in the hostname? You said avoid using special characters.” Yes. This is to show that we can use a hyphen in a hostname, and…well…pipit is just…weird. Do you see PIE-pit or PIP-it? pi-pit makes it clear.)

hostnamectl

Current results:

Static hostname: humperdink
Icon name: computer-vm
Chassis: vm
Machine ID: 10111111111111111111111111111111188
Boot ID: d8vvvvvvvvvvvvvvvvvvvvvvvvvv430
Virtualization: oracle
Operating System: Linux Mint 21.2 
Kernel: Linux 6.4.10-060410-generic
Architecture: x86-64
Hardware Vendor: innotek GmbH
Hardware Model: VirtualBox

We can see that everything else remains the same. We have the new humperdink hostname. However, we are already tired of that name and wish to change it to pi-pit because everyone loves to type hypens in a terminal.

Step 1. Use hostnamectl

sudo hostnamectl set-hostname pi-pit

hostnamectl will return a prompt if successful.

Step 2. Edit /etc/hosts

hostnamectl will change the name in /etc/hostname and you will see the new name in the terminal prompt without rebooting, but /etc/hosts will remain the same. Just like before, open this file in a text editor with sudo privileges and change humperdink to pi-pit.

sudo xed /etc/hosts

Change this:

127.0.0.1 localhost
127.0.1.1 humperdink

To this:

127.0.0.1 localhost
127.0.1.1 pi-pit

Step 3. Reboot

sudo reboot

Using hostnamectl, I found that I did not need to run update-grub. At least, I have not experienced any problems. In fact, a reboot might not even be necessary since I did not notice any network issues. However, I rebooted just to be safe. Upon logging back in after the reboot, the system will show pi-pit for its hostname.

Conclusion

Yes, we can also use the GUI to change the hostname, but what if we are running a headless server or a distribution lacking a GUI, such as Ubuntu Server?

Illustrating these two terminal-based techniques show how to rename a system via SSH if you need to, and they show what files are affected behind the scenes to successfully rename a system.

Have fun!

, ,

  1. Leave a comment

Leave a comment