How to Change Hostname on Ubuntu 23.04

Display the Current Hostname

Find out your current system name by running the following command.

hostnamectl

Output:

root@ubuntu:~# hostnamectl
 Static hostname: ubuntu
       Icon name: computer-vm
         Chassis: vm 🖴
      Machine ID: 973a8956d87c496f82a6a9db031a8253
         Boot ID: b2de8d85387b4aa3a6d86559f7f2a5d1
  Virtualization: kvm
Operating System: Ubuntu 23.04
          Kernel: Linux 6.2.0-20-generic
    Architecture: x86-64
 Hardware Vendor: Red Hat
  Hardware Model: KVM
Firmware Version: 1.16.0-3.module_el8.7.0+3346+68867adb

Change 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 the Ubuntu server name.

nano /etc/hosts

Output:

127.0.0.1 localhost
127.0.1.1 crown

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

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, find the line "preserve_hostname: false" in the following configuration file "/etc/cloud/cloud.cfg" and change it to true.

nano /etc/cloud/cloud.cfg

Change the value to true,

preserve_hostname: true

Save the file and exit,

Verify the Changes

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

hostnamectl

Output:

root@ubuntu:~# hostnamectl
 Static hostname: crowncloud.net
 Pretty hostname: crowncloud production server
       Icon name: computer-vm
         Chassis: vm 🖴
      Machine ID: 973a8956d87c496f82a6a9db031a8253
         Boot ID: b2de8d85387b4aa3a6d86559f7f2a5d1
  Virtualization: kvm
Operating System: Ubuntu 23.04
          Kernel: Linux 6.2.0-20-generic
    Architecture: x86-64
 Hardware Vendor: Red Hat
  Hardware Model: KVM
Firmware Version: 1.16.0-3.module_el8.7.0+3346+68867adb