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 ...
Additionally your /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:
/etc/hostname
and
/etc/hosts
In /etc/hosts look for:
127.0.1.1 old-host-name
and change to
127.0.1.1 new-host-name
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:
hostname new-host-name
Old information
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
Then
/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 127.0.0.1
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:
preserve_hostname: true
In summary:
hostnamectl set-hostname new-hostname
/etc/hosts
/etc/cloud/cloud.cfg
References
- https://linuxconfig.org/how-to-change-fqdn-domain-name-on-ubuntu-20-04-focal-fossa-linux
- https://www.cyberciti.biz/faq/ubuntu-change-hostname-command/
- https://linuxize.com/post/how-to-change-hostname-on-ubuntu-18-04/