This guide will show you how to interrogate the hostname of an Ubuntu server.
When working with hostnames, there is the shortened version (the name of the host) and the FQDN – the fully qualified domain name which includes the hostname. Numerous systems, for example WHM and Virtualmin, needs the FQDN to be correct too. With WHM is easy with the UI to change the hostname, but it’s best with Virtualmin to do this before installing the control panel.
Please be careful of making the hostname of WHM or Virtualmin the same as an existing working domain – rather seperate the hostname to something that will only be used to access the host. WHM warns you about this, and Virtualmin will have Postfix issues due to this.
The commands you would use to interrogate the hostname and FQDN are:
hostname hostname --fqdn dnsdomainname
All three those should return the correct results.
Furthermore, after a perfect setup, your
/etc/hosts file should look like this:
[email protected]:~# cat /etc/hosts ... 127.0.1.1 host.example.com host 127.0.0.1 localhost ...
/etc/hostname file should look like this:
[email protected]:~# cat /etc/hostname host
Instructions for Changing the Hostname
Changing the hostname on Ubuntu needs to happen in two places to make it permanent:
In /etc/hosts look for:
and change to
If you prefer fully qualified, which is better, do this:
127.0.1.1 new-host-name.example.com new-host-name
Finally to make it stick for now, do this:
If you’re used to looking for
/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
sudo hostnamectl set-hostname new-hostname
/etc/hosts look for a line such the one below. Warning, do not change
localhost entry may be used and as convention
localhost is always
127.0.0.1 localhost 127.0.0.1 old-hostname
You might also have a
/etc/cloud/cloud.cfg file. If so, set the following entry:
hostnamectl set-hostname new-hostname