A Linux hostname is a unique name that identifies a computer on a network. When working on multiple computers on the same network, you may get confused because of duplicate hostnames. So the need to change your hostname arises.
In this article, we cover the best methods (including terminal and GUI) on how to change Linux hostname in some of the major Linux distributions.
Let’s get started.
Change hostname on Ubuntu
First, let’s see how to check your hostname. You can choose either the hostname or hostnamectl command to check your hostname. Type any of the two commands in your terminal, and you should be able to see your current hostname.
In this example, our hostname is rustypc. Let’s change it to hecticgeek.
Method 1: Change hostname using hostnamectl command
Here’s the syntax if you choose to change the hostname in the Linux terminal.
sudo hostnamectl set-hostname hecticgeek
There’s another step to this method. We need to tweak the /etc/hosts file to reflect this change.
Open the file in your terminal with this command
sudo nano /etc/hosts
Now change it to the new hostname.
Save and exit nano. You’re done.
Method 2: Changing hostname by editing /etc/hostname
This method is similar to the second step in Method 1. Open your terminal and type the below command.
sudo nano /etc/hostname
You will see the current hostname, in our case hecticgeek.
Now edit the name and put your new hostname. Save and exit.
You can check whether it worked or not by using the hostname or hostnamectl command again. Close and re-open your terminal and type the commands.
Hostname changed to geek
Method 3: Change hostname using the GUI
This method is quite easy and needs only a couple of button clicks. If you’re not good with the terminal, this method is perfect for you.
First, go to settings. You can find it in the dropdown menu on top. Alternatively, you can go to All applications and type settings in the search bar.
Open settings and go to About on the left side. Now go to Device Name.
Type in your new hostname and press Rename.
You can re-check if it worked, from your terminal.
Change hostname on CentOS
You can change the hostname on CentOS using the nmcli command.
First, check your current hostname with this command.
sudo nmcli general hostname
To change the hostname, just type this command
sudo nmcli general hostname hecticgeek
Finally, use this command to restart the systemd-hostnamed service.
sudo systemctl restart systemd-hostnamed
Your changes will now take effect. Use the hostnamectl command to check it.
Change hostname on Debian
You can use the hostname command to change your hostname. Open your terminal and type hostname then your preferred name.
You can check your new hostname by using the hostname command again.
Why should you change your Linux hostname?
There are a few reasons why you might need to change the hostname on Linux. The most common reason is when you change the network configuration of your computer. For example, if you change your computer’s IP address, you will need to change the hostname so that other computers on the network can still identify your machine.
By changing your hostname to something that can be remembered easily, you can avoid many hassles. For example, the name hostingserver is much better than server2. This way, you know which device you’re currently operating on.
Another reason you might need to change the hostname is when you have multiple computers on the same network and you want to give each one a unique name.
In this guide, we’ve demonstrated some of the methods you can follow to change your hostname on Linux. We’ve covered some of the major distros.
Now then, which method do you think is the best for changing your hostname on Linux? Let us know in the comments.
1 thought on “How To Change Linux Hostname (Ubuntu, CentOS & more)”
Very fruitful article. Easy to understand with those image. Go on guys