How to Change Hostname on Ubuntu 18.04

View the current hostname

$ hostnamectl
 Static hostname: dbserver
         Icon name: computer-vm
           Chassis: vm
        Machine ID: baf17aaaa3c343c48cc85d38fa7534c8
           Boot ID: 0c82f3ccd20c4fda9d6d317d551d3ba9
    Virtualization: kvm
  Operating System: Ubuntu 18.04.4 LTS
            Kernel: Linux 4.15.0-1058-aws
      Architecture: x86-64

As you can see in the image above, the current hostname is set to dbserver.

Now, we will change the hostname

1. Using hostnamectl command

sudo hostnamectl set-hostname dbserver2

2. Edit the /etc/hosts file

127.0.0.1   localhost
127.0.0.1   dbserver2