How to change the hostname on Ubuntu 20.04 or CentOS

Share this article

The hostname is a name assigned to a network device by a user. We know that remembering the hostname is much easier than IP address. In order to use the hostname as a fully qualified domain name need many configurations like DNS conf etc. but in this article, we are not going to dive into that detail.

We have seen many instances as system admins that we need to change the hostname of the system. We are going to see in this article how to change the local hostname of the Linux systems. By default, we assign a hostname during the host setup.

How to Check hostname

You can check the hostname by running the following command.

hostname

The output of the above command is following, which shows in my ubuntu machine hostname is ubuntu

foofunc@ubuntu:~$ hostname 
ubuntu
foofunc@ubuntu:~$ 

If you are working with ubuntu or centos terminal. You can see the snapshot of the terminal, where first-half mentions the user and after @ is the hostname.

How to change hostname

In this section, we will discuss two methods for changing the hostname.

Non-persistent method

Here we will use the hostname command to set the different hostname but it would be valid till the reboot. See the following command syntax

sudo hostname <new-host-name>

Below, I have mentioned the output of the above-mentioned command.

foofunc@ubuntu:~$ hostname 
ubuntu
foofunc@ubuntu:~$ 
foofunc@ubuntu:~$ sudo hostname foofunc-host
foofunc@ubuntu:~$ 
foofunc@ubuntu:~$ hostname
foofunc-host
foofunc@ubuntu:~$ 

You can see in the above code section the name is changed from ubuntu to foofunc-host.

Persistent method

To make hostname persistent even after the reboots. We need to change in /etc/hostname file, remove the old hostname then enter the new hostname and save the file.

foofunc@ubuntu:~$ cat /etc/hostname
foofunc-host
foofunc@ubuntu:~$ 

Now reload the network manager use the following command:

In Ubuntu:

sudo service network-manager restart

In Centos:

sudo service NetworkManager restart

Verify

You can close the terminal if you are using the Ubuntu/CentOS desktop version. When you again open the terminal you will see that your hostname is changed in shell. If you have ssh in remote server close the session, when you again connect you will see the changes.

You can also reboot the machine in that case restarting the network manager is not needed.

In my case after closing the ssh session and connecting it again I see the following output, now hostname ubuntu is changed with foofunc-host.

foofunc@foofunc-host:~$ hostname 
foofunc-host
foofunc@foofunc-host:~$ 

Other important entry

If you use the hostname instead of IP address on the local network you need to change in /etc/hosts file.

In our scenario, we have changed our hostname from ubuntu to foofunc-host. If you want to use the hostname change it according to your IP address.

In our case, 192.168.1.189 is the IP address of foofunc-host. I have added the following line in /etc/hosts

192.168.1.189       foofunc-host 

Verify by pinging the hostname.

foofunc@foofunc-host:~$ ping foofunc-host -c 4 
PING foofunc-host (192.168.1.189) 56(84) bytes of data.
64 bytes from foofunc-host (192.168.1.189): icmp_seq=1 ttl=64 time=0.096 ms
64 bytes from foofunc-host (192.168.1.189): icmp_seq=2 ttl=64 time=0.084 ms
64 bytes from foofunc-host (192.168.1.189): icmp_seq=3 ttl=64 time=0.079 ms
64 bytes from foofunc-host (192.168.1.189): icmp_seq=4 ttl=64 time=0.081 ms

--- foofunc-host ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3075ms
rtt min/avg/max/mdev = 0.079/0.085/0.096/0.006 ms
foofunc@foofunc-host:~$ 

Leave a Comment

Your email address will not be published. Required fields are marked *