How to Change Hostname on Ubuntu 22.04
Display the Current Hostname
Find out your current system name by running the following command.
hostnamectl
Output:
root@crown:~# hostnamectl
Static hostname: crown
Icon name: computer-vm
Chassis: vm
Machine ID: 98ef1ad056e345ba99622d140a2f234e
Boot ID: 81c898352d6c42b690210031a82595f5
Virtualization: kvm
Operating System: Ubuntu Jammy Jellyfish (development branch)
Kernel: Linux 5.15.0-23-generic
Architecture: x86-64
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@crown:~# hostnamectl
Static hostname: crowncloud.net
Pretty hostname: crowncloud production server
Icon name: computer-vm
Chassis: vm
Machine ID: 98ef1ad056e345ba99622d140a2f234e
Boot ID: 81c898352d6c42b690210031a82595f5
Virtualization: kvm
Operating System: Ubuntu Jammy Jellyfish (development branch)
Kernel: Linux 5.15.0-23-generic
Architecture: x86-64