The hostname is the name of your computer. It is generally used to identify your PC on the network and it’s also usually part of the command prompt in Linux terminal.
The hostname is assigned to the computer during the operating system installation, but you can change it later, if you want. In this post we will see how you can change hostname of your Linux Mint PC.
Before we start
To find the current hostname of your Linux PC simply open the terminal and run the following command:
The image below is from my home Linux Mint VM and its hostname is old-hostname :
Notice that the current hostname is also the part of the command prompt, which includes:
current directory: ~/Desktop
Now, let’s see how we can change the hostname.
Method #1. Use hostnamectl ( recommended)
The recommended way is to use hostnamectl command which is a part of systemd. It means that you can use this method on any Linux distribution running systemd.
If you run hostnamectl in the Terminal you will see some information about your system including the hostname:
To change the hostname run:
hostnamectl set-hostname <NEW-HOSTNAME>
As you can see on the image below the hostname has been changed:
Method #2. Use hostname command
This method is a bit longer and not as convenient as the first one, because it requires more work. Nevertheless, I decided to describe it here, because it involves a manual editing of /etc/hostname system file and it’s useful to be aware of its existence.
You can run the following command to set the new hostname(temporary):
sudo hostname <NEW-HOSTNAME>
After running this command the hostname will be changes, but if you reboot the PC, it will revert back to the previous hostname. The reason for this is that the permanent hostname of the machine should be specified in the /etc/hostname file. In the previous Method #1 it was done automatically by the hostnamectl command, but here we need to do this manually.
Use any text editor to edit the file and make sure it contains a single line which is the new hostname. You will need sudo privileges to edit this file, so if you use nano editor, run the command:
sudo nano /etc/hostname
I edited the file to look like this:
Now, after the reboot the hostname will be set to new-hostname
Another important file which you should edit is /etc/hosts file which is used for hostname resolution. Whenever your Linux PC wants to reach another machine on the local network or the Internet it’s going to look for the matching name in this file. Since you haven’t edited this file it will contain the old hostname resolving to a loopback address. This means that you will be able to ping your PC via the old name, even after you change the hostname, which is confusing.
Therefore, let’s edit it too:
sudo nano /etc/hosts
After the change it look like this:
After you change it to the correct hostname and save the file you will not be able to ping the old hostname anymore which is what we want.
Now you can reboot the PC and after the reboot the new hostname will be preserved.