Change hostname on Ubuntu Server 14.04

20151229113855Sometimes I forget some of the steps to changing the hostname on Ubtuntu as they vary slightly to other Linux distros such as RHEL or CentOS. The process is just as simple but there is a slight difference in that we need to amend the hostname file. So without further ado, here are the steps to change the hostname on Ubunutu Server 14.04:

The Steps

1. The first step is to run the hostname command followed by the new server name and hit Enter:

admin@web01:~$ sudo hostname web02

2. As you can see from the above the current hostname was web01, I changed it to web02. Running the hostname command on it’s own will show you the current and new hostname of web02:

admin@web01:~$ hostname

The hostname has now been changed, however this change is not permanent. In other words if you reboot the server the changes will not survive, the hostname will revert back to its previous name. To make the hostname change permanent continue to step 3 below.

3. The next step is to update the /etc/sysconfig/network file by opening it with vi or any other editor of your choice:

admin@web01:~$ sudo vi /etc/hostname

4. Within the file you will see your old hostname, in my case web01:


Simply change the hostname to the new name, in my case it is web02:


Exit and save the changes to the file.

5. We have one final file to update, which is /etc/hosts. Edit it vi:

admin@web01:~$ sudo vi /etc/hosts

You should see the lines below, the bottom line showing your old hostname, in my case web01:   localhost   web01

Once again, change it to reflect the new desired hostname, in my case web02:   localhost   web02

Exit and save the changes to the file. The changes so far should be enough to ensure your system recognises the new hostname. I like to restart networking services to ensure that the new changes take effect, to do this proceed to step 6 below.

6. To commit the changes made in the network, hostname and hosts files above in steps 4 & 5 respectively you can either reboot the server or restart the network services. Restart the network services by running:

admin@web01:~$ sudo ifdown eth0 && sudo ifup eth0

Please note that in previous version of Ubuntu server you could run service networking restart to restart services. This no longer works in Ubuntu Server 14.04. You need to shut down the interface and then bring it back up per step 6 above.

After restarting the network services the operating system and all applications will use the new hostname.

How to successfully restart a network without reboot over SSH?
How do I change the hostname without a restart?