How to Change the Hostname of Ubuntu 18.04 Server

Tired of using the same old default hostname that comes with your Ubuntu 18.04 server, and want to change it? And you want to do it now, without having to restart? Oh yes, you can do both!!

Let us see how to change the hostname of our Ubuntu server. It’s just a few simple steps.


1. View the current hostname

To know the current hostname of your machine, simple type:

hostnamectl
Ubuntu View Hostname
Ubuntu View Hostname

This gives information about the host machine, which includes its hostname, which is ubuntu, in my case.


2. Change the hostname via hostnamectl

Now, we need to change the hostname in hostnamectl, the controller program for information regarding the host machine.

To do this, you must have administrative privileges to use sudo. To change the hostname to newhostname, simply type:

sudo hostnamectl set-hostname newhostname

This returns 0 on success, and only gives an error on failure. So if you return to the command line, then it’s all good, and we can proceed to the next step.

Check the output of hostnamectl again, just to make sure that the name has been changed correctly.

Ubuntu Set Hostname
Ubuntu Set Hostname

While this will definitely change the hostname of our machine, changes will be applied only after a reboot. So we need to manually edit the hosts configuration file and change it to our new hostname since we do not want to restart.


3. Manually edit the /etc/hosts file

Information about localhost and hostname are stored in the /etc/hosts file, so open an editor (eg. nano, vi) and edit the /etc/hosts configuration file.

root@ubuntu:~# vi /etc/hosts

This is how it looks for me:

Ubuntu Old Host Config File
Ubuntu Old Host Config File

Change the third line (The line containing your old hostname) and replace that with newhostname.

The updated version looks like this: (I’ve changed the hostname from ubuntu to HowLinux)

Ubuntu Updated Host Config File
Ubuntu Updated Host Config File

Save all changes and exit the editor.

Unfortunately, for me, there is still no change yet, because I am still in the old terminal session.

Ubuntu Still No Change In Hostname 1
Ubuntu Still No Change In Hostname 1

To see the updated hostname, open a new Terminal session and you will see your updated hostname. I have opened a new terminal session, and this is my Output:

Ubuntu New Hostname
Ubuntu New Hostname

As you can see, my hostname has been updated successfully, and I didn’t have to restart my machine!


Conclusion

In this tutorial, we have shown how to successfully change the hostname of our Ubuntu 18.04 Server, without having to restart the machine. I hope this helps you in successfully configuring your server!