Press "Enter" to skip to content

Linux how to change hostname on Ubuntu, Debian

During the install process of Debian and Ubuntu you have to choose a hostname, but what if you want to change it later? The hostname is the name your system will have on your network, it will be used for example when other systems scan the network for accessible resources. In a well configured network, the hostname could even be used instead of the ip address to access the system remotely.

The hostname is not fixed and can be configured and changed after the os is installed. Let’s see how to change hostname on your Ubuntu system.

How to see the current hostname on Ubuntu

As a first step we will see the actual hostname of our linux system.

We can see the actual hostname configuration from the command line terminal, we can open it with <Ctrl> + <Alt> + t on Ubuntu.

By default the initial ubuntu terminal prompt is prefixed with username@hostname , so you will see it directly, but it’s always better to read it from the actual configuration, so we have to use the command hostnamectl. To do so, just execute the command hostnamectl without arguments/options. It will show us some configuration details, and as first, the hostname.

hostnamectl
The output of hostnamectl without arguments
The output of hostnamectl without arguments

in our case the hostname is simply ubuntu as we choose a pretty generic one during the os setup, we will change it in the next step.

How to change hostname on Ubuntu linux

Now that we checked the actual hostname, we can proceed to change it to a new one.

We will use the same hostnamectl command, but adding the command set-hostname and the new hostname as a parameter. The command syntax is the following:

hostnamectl set-hostname <new hostname>

For example, let’s change the actual ubuntu hostname to valerioangelici.com, the command to execute is:

hostnamectl set-hostname valerioangelici.com

If the hostname is on the command prompt prefix, it will be updated the next time you open a terminal, but you can verify that the hostname has been set by executing hostnamectl with no arguments again.

Unfortunately it’s not just that simple, we have to manually edit the hosts file, as it doesn’t get updated automatically.

We can edit the hosts file with our favourite text editor:

sudo vi /etc/hosts
or
sudo nano /etc/hosts

The file should look like this:

Default /etc/hosts file
Default /etc/hosts file

In this file we have to replace all occurrences of the old hostname with the new one. In this case it’s only on the second line, so it will be an easy job.

At this point we completed the procedure, the hostname has been changed, it could be needed some time or a system reboot to propagate all the changes in the network.

For further info on the systemctl command, you can ge to the dedicated ubuntu manuals page.

Leave a Reply

Your email address will not be published.