How to Change Hostname in Ubuntu Server/Desktop

Hostname is the friendly name use to identify your server on a network. Configuring the hostname is an important network configuration task and therefore as a Linux administrator, it is important that you know how to set the hostname in your Ubuntu system.

The hostname specified in the /etc/hostname file. The hostnamectl command is used to modify this file and can be used to view the status of the system's fully qualified host name.

To change the hostname using hostnamectl, we use the set-name option. For example, the following command will change the Ubuntu hostname to server1.example.com.

hostnamectl set-hostname server1.example.com

This command does not need to run with sudo if the user is a member of the sudo administrative group, but you will be prompted to enter your password.

ubuntu 18.04 change hostname

When executed, the hostnamectl command edit the /etc/hostname file. You can verify this by viewing the contents of this file before and after making the change.

When executed, the hostnamectl command edit the /etc/hostname file

To view the current hostname, you can use the status option:

hostnamectl status

The status command also displays information about the Operating System, Linux kernel, Architecture, Virtualization type, and much more.

Ubuntu 18.04 hostname status

Also, don't forget to set hostname in the /etc/hosts file. This step is important to preventing some application of throwing errors such as: "unable to resolve host".

For example, if you set the server name to "ubuntu18.localdomain.local", then you should add the following lines to the /etc/hosts file:

127.0.0.1 ubuntu18.localdomain.local ubuntu18
::1 ubuntu18.localdomain.local ubuntu18

Update the hosts file each time you change the server name.

There is a another command. The hostname command temporarily modifies the system's fully qualified host name, but it is non-persistent across reboot (Not recommended to use).

hostname ubuntu18.localdomain.local

The above command will change the Ubuntu hostname to ubuntu18.localdomain.local. However, this change is only temporary and is not reflected in the /etc/hostname file, therefore changes will be lost after a system reboot.

The hostnamectl command introduced in Ubuntu 16.04. Before that we used to edit /etc/hostname file manually (Still possible) using a text editor.