January 19, 2020

Change a hostname on Ubuntu

Change a hostname on Ubuntu

Ubuntu 16+

On Ubuntu 16+ you can use hostnamectl to change the hostname via:

hostnamectl set-hostname <new name>

Edit hosts and hostname directly:

To Change a hostname on Ubuntu use an editor like vim or nano to edit /etc/hostname

sudo nano /etc/hostname

Erase the old hostname and add the new one

Next you will want to edit the /etc/hosts file and replace the old hostname after 127.0.1.1 to the new hostname

sudo nano /etc/hosts

Now verify with:

 hostnamectl
 

And you should get a response as follows:

  Static hostname: doublebracket
        Icon name: computer-desktop
          Chassis: desktop
       Machine ID: ca6bba1fbb154992a5fe74d564b42e22
          Boot ID: 1d92d7c3d74f494eaba5eb9a078894f9
 Operating System: Ubuntu 19.10
           Kernel: Linux 5.3.0-26-generic
     Architecture: x86-64

Note: You may see tutorials that say just use hostname <newhostname>. This will create a tranisent hostname, i.e one that will not survive a reboot.  Aftering setting a hostname in this way you will see it when you type hostname. However if you type hostnamectl you will see it listed as transient. You can see the example output below: