Changing the hostname of your Ubuntu server installation is a pretty simple affair.

To view the current hostname of your system, simply enter hostname.

Now to affect a termporary change of hostname, you could simply use:

sudo hostname mynewhostname

However, be careful here as this is really just a temporary hostname change and won’t be saved when the system restarts. The reason for this is because the hostname is usually set on system startup when the system loads the detail from a file, generally located at “/etc/hostname”.

So in order to permanently change your system’s hostname, you need to edit the correct host-related files like so:

sudo nano /etc/hosts

Set your new desired hostname next to the entry for 127.0.1.1 (leaving the 127.0.0.1 localhost entry as is). Next, run:

sudo nano /etc/hostname

Simply enter your desired hostname on a single line and save.

Once you have made your changes and saved both of the files, you should be good to go – as you can test out for yourself by simply rebooting your machine with a good old sudo reboot and then checking with hostname!

Nifty.