How to change the hostname on an Ubuntu 18.04 server

If you’re used to looking for /etc/hostname and /etc/hosts  and the hostname command to change your hostname, things are much more intricate with Ubuntu 18.04. Here are the various places you have to go and look to change the hostname:

hostnamectl display the current hostname

To change:

sudo hostnamectl set-hostname new-hostname


/etc/hosts look for a line such the one below. Warning, do not change localhost. The localhost entry may be used and as convention localhost is always   localhost   old-hostname

You might also have a /etc/cloud/cloud.cfg file. If so, set the following entry:

preserve_hostname: true

In summary:

hostnamectl set-hostname new-hostname


Share this article

Share on facebook
Share on twitter
Share on linkedin

Leave a Reply

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

Scroll to Top