Check/change hostname in Ubuntu 16.04 and above

So, it looks like in Ubuntu 16.04 and above, changing the hostname is no longer using the hostname command and editing the /etc/hosts file.

New (at least to me) command hostnamectl is now used.

This command recognizes three different classes of hostname (straight from the man page):

static - The traditional hostname. It is stored in the /etc/hostname file and can be set by the user.

pretty - A descriptive free-form UTF8 hostname used for presentation to the user. For example, Linuxize's laptop.

transient - A dynamic hostname that is maintained by the kernel. DHCP or mDNS servers can change the transient hostname at run time. By default, it is the same as the static hostname.

It is recommended to use a fully-qualified domain name (FQDN ) such as host.example.com for both static and transient names:

sudo hostnamectl set-hostname mars.example.com
sudo hostnamectl set-hostname “Docker test box” --pretty
sudo hostnamectl set-hostname mars.example.com --static
sudo hostnamectl set-hostname mars.example.com --transient
hostnamectl
    Static hostname:  mars.example.com
    Pretty hostname:  Docker test box
          Icon name:  computer-vm
            Chassis:  vm
         Machine ID:  a0269f7cf11c44d1bde52dafedd4686c
            Boot ID:  b1a6f34efb54d238c4250a474c4909d
     Virtualization:  bhyve
   Operating System:  Ubuntu 20.04.1 LTS
             Kernel:  Linux 5.4.0-58-generic
       Architecture:  x86-64
cat /etc/hosts | grep "127."
127.0.0.1    localhost
127.0.0.1    mars.example.com

Reference