How to Change Hostname on Ubuntu 23.10

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: b12e449600f14184b7e13199a433a67e
           Boot ID: 2da169c305a941ad87900de1ed851586
    Virtualization: kvm
  Operating System: Ubuntu 23.10
            Kernel: Linux 6.5.0-9-generic
      Architecture: x86-64
   Hardware Vendor: Red Hat
    Hardware Model: KVM
  Firmware Version: 1.16.0-3.module_el8.7.0+3346+68867adb
     Firmware Date: Tue 2014-04-01
  root@vps:~#

Changing the System Hostname

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

hostnamectl set-hostname vps.crowncloud.net

Replace "vps.crowncloud.net" with your preferred hostname.

Also, it is necessary to update the hostname in the /etc/hosts file to successfully change the name of the Ubuntu server.

nano /etc/hosts

Replace the old hostname with the new hostname, then save the file and exit.

  127.0.0.1 localhost
  127.0.1.1 vps.crowncloud.net

  # 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

If cloud-init is installed on your Ubuntu server, you'll also have to modify the cloud.cfg file. To edit the cloud.cfg file, by using the following command.

nano /etc/cloud/cloud.cfg

Find preserve_hostname: false and change it to preserve_hostname: true.

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: vps.crowncloud.net
               Icon name: computer-vm
                 Chassis: vm 🖴
              Machine ID: b12e449600f14184b7e13199a433a67e
                 Boot ID: 2da169c305a941ad87900de1ed851586
          Virtualization: kvm
        Operating System: Ubuntu 23.10
                  Kernel: Linux 6.5.0-9-generic
            Architecture: x86-64
         Hardware Vendor: Red Hat
          Hardware Model: KVM
        Firmware Version: 1.16.0-3.module_el8.7.0+3346+68867adb
           Firmware Date: Tue 2014-04-01