Change hostname on Ubuntu 18.04 LTS

If you have Cloud cloud-ini installed you will have to edit

sudo nano /etc/cloud/cloud.cfg

to preserve your new hostname.

# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: true

When that is done, run the rename command

sudo hostnamectl set-hostname new-hostname

Edit /etc/hosts so it corresponds with your new hostname   localhost   new-hostname

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

You can verify your changes by running


At last, reboot and it should show the correct hostname.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.