How to Change Hostname on Ubuntu 20.04

Displaying the Current Hostname

To find out your current system name by running the following command.

hostnamectl

Output:

root@vps:~# hostnamectl
     Static hostname: vps.server.com
                 Icon name: computer-vm
                     Chassis: vm
                Machine ID: 20111e34366400515e16e9f15f909808
                     Boot ID: c10547c46d214b1a96117029642dd499
        Virtualization: kvm
    Operating System: Ubuntu 20.04 LTS
                        Kernel: Linux 5.4.0-26-generic
            Architecture: x86-64

Changing the System Hostname

Now that you know the current hostname, you can change the hostname by using the following command and Optionally you can also set the pretty hostname as follows.

hostnamectl set-hostname crowncloud.net

hostnamectl set-hostname "crowncloud production server" --pretty

And also /etc/hosts file should be edited to successfully change Ubuntu server name.

vi /etc/hosts

Output:

root@crowncloud:~# cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 crowncloud.net

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet

Then replace the old name with the new, then save the file and exit.

Ubuntu servers might need to install cloud-init. If it is installed you will also need to edit the cloud.cfg file.

apt install cloud-init

Once the cloud-init is installed need to find "preserve_hostname: false" line in the follwoing configuration file "/etc/cloud/cloud.cfg" and change it to true.

vi /etc/cloud/cloud.cfg

preserve_hostname: true

Save the file and exit,

Verify the change

To verify the hostname has been fully changed, enter the "hostnamectl" command:

hostnamectl

Output:

root@vps:~# hostnamectl
     Static hostname: crowncloud.net
     Pretty hostname: crowncloud production server
                 Icon name: computer-vm
                     Chassis: vm
                Machine ID: 20111e34366400515e16e9f15f909808
                     Boot ID: c10547c46d214b1a96117029642dd499
        Virtualization: kvm
    Operating System: Ubuntu 20.04 LTS
                        Kernel: Linux 5.4.0-26-generic
            Architecture: x86-64
root@vps:~#

CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!

1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month

Available Locations: LAX | MIA | ATL | FRA | AMS