Changing the hostname on Ubuntu

So for whatever reason you need to change the hostname of your Ubuntu Linux machine. For me this is usually because I am playing around with a virtual machine. I have automated the setup for a number machine installations. Once I have it running and configured I may want to keep, so changing its name is a nice thing to do.

Check your hostname is set to something and no errors are returned. The commands below will return the host name with an without the domain.

uname -a 
uname -n 
hostname -a 
hostname -f 

To change the hostname until the new reboot you can use:

hostname newname

To change so it persists through a reboot you need to edit some files. For your installation you may not have all these files present, they can be ignored.

nano /etc/hostname
nano /etc/mailname
nano /etc/hosts

You may also have other config files where the hostname has been hardcoded. You can find these with grep

sudo grep -rn oldname /etc/*

Not all of the listed occurrences need to be changed, e.g. anything with LVM and the ssh keys should be left unchanged.

Once the machine has rebooted check that the new names are all set correctly, by using any of the commands above to see all went well.

uname -a 
uname -n 
hostname -a 
hostname -f

There you go all done!

One thought on “Changing the hostname on Ubuntu

Leave a Reply

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