How to change the Hostname on Ubuntu?

In a computer network, computers are identified by a name that can be understood in human language. This name is called the hostname. And this post, you will learn how to change the hostname on Ubuntu.

In general, the hostname is a unique modifiable name that a computer has. This name helps to identify a computer within a network. This way, the server and the rest of the nodes can recognize the computer.

On the other hand, within a computer network, many network services require knowing exactly each member of the network. Therefore, it is important to know how to identify and change it.

Changing the hostname on Ubuntu

Ubuntu and its recent versions have given way to systemd. Therefore the command that allows us to query and change the hostname is hostnamectl.

If you execute the command without parameters you will have an output per screen like the following one:

Static hostname: imaginelinux
         Icon name: computer-vm
           Chassis: vm
        Machine ID: e776705030044bcd8e44b31b97856afe
           Boot ID: af591d2413544229936d0cfdb6d54cf5
    Virtualization: oracle
  Operating System: Ubuntu 20.04 LTS
            Kernel: Linux 5.4.0-33-generic
      Architecture: x86-64

In this screen output, the first value obtained is the hostname of the computer.

If you want to change it, you have to follow the next syntax:

hostnamectl set-hostname [new-hostname]

To change the hostname you need to be a root user. Or at least, be a user who has permission to use sudo.

An example of how to change the hostname on Ubuntu is the following

sudo hostnamectl set-hostname imaginelinux

For changes to be applied, the session must be closed and reopened. And some network services may need to be restarted or refreshed.

To avoid problems on the same computer, it is necessary to modify the hosts file and add a new parameter on the new hostname.

So, edit the file:

sudo nano /etc/hosts

And add the next line:

127.0.0.1 imaginelinux

Save the changes and close the file. Remember you have to replace “imaginelinux” with your hostname.

Again, you can check the changes.

hostnamectl

So, this way you open the hostname on Ubuntu changed.

Conclusion

Usually, a computer is part of a computer network and for it to work properly, a hostname must be defined correctly.

So, thanks to this post, you now know how to change the hostname in Ubuntu.

We invite you to read our post about Python on Ubuntu.

Scroll to Top