How to Change or Set Hostname on Ubuntu Server.

In Unix-like operating systems, users assign a hostname to the machine during the OS setup. You may decide to change your computer’s hostname for diverse reasons. For instance, no two computers on a network can have the same hostname and, if you find yourself in this situation, you will have to change your hostname. In this article, you will learn how to Set or Change a hostname on Ubuntu server 20.04 using the Linux command line.

Check your current Hostname

To check the current hostname of your Ubuntu system, use one of two available commands.

The hostname command displays only the hostname itself.

$ hostname
batman

The other command, hostnamectl, displays additional information about your computer system.

$ hostname
   Static hostname: batman
         Icon name: computer-vm
           Chassis: vm
        Machine ID: c1cbeec7609843cdae2d7e8b4d359513
           Boot ID: d66ebd50543d405481332a5506a5dbc5
    Virtualization: oracle
  Operating System: Ubuntu 20.04.2 LTS
            Kernel: Linux 5.4.0-67-generic
      Architecture: x86-64

As you can see the Static hostname line displays your machine’s hostname.

Change or Set Hostname (Reboot Required)

A way to permanently change the hostname is by editing those two configuration files:

  • /etc/hostname
$ sudo nano /etc/hostname

Delete the old name and setup a new one.

  • /etc/hosts
$ sudo nano /etc/hosts

The file /etc/hosts maps hostnames to IP addresses. Look for any occurrence of the existing computer name and simply replace it with your new choice.

This changes take effect immediately after system reboot.

$ sudo reboot

Change or Set Hostname (No Reboot Required)

To make a temporary change to your computer’s hostname without rebooting your computer use the hostnamectl command.

$ sudo hostnamectl set-hostname new-hostname

Of course, you will need to replace new-hostname with your own hostname choice.
Use hostnamectl to Confirm the Change.

Change or set the Pretty Hostname (Optional & Bonus)

A "pretty" hostname is the hostname presented to the user, not to another computer on a network.
A computer system identifies another computer only by its static hostname.

To change a machine’s “pretty” hostname, use the hostnamectl command with the --pretty option:

$ sudo hostnamectl set-hostname new-hostname --pretty

Of course, you will need to replace new-hostname with your own hostname choice.

After checking the result with hostnamectl, you should notice an additional line in the output, listing your computer’s "pretty" hostname.

$ hostnamectl                                                              
   Static hostname: batman
   Pretty hostname: Mr. Snubmonkey
         Icon name: computer-vm
           Chassis: vm
        Machine ID: c1cbeec7609843cdae2d7e8b4d359513
           Boot ID: d66ebd50543d405481332a5506a5dbc5
    Virtualization: oracle
  Operating System: Ubuntu 20.04.2 LTS
            Kernel: Linux 5.4.0-67-generic
      Architecture: x86-64

The "pretty" hostname is stored in /etc/machine-info. Updating this file is another way to perform this optional step.

That's it... you've learned something; how to Change or Set a hostname on Ubuntu Server using the Linux command line.