What is a hostname in Linux?
A Linux hostname is a unique name given to a device on a network. It is used to identify and communicate with other devices on the network.
A fully qualified name (FQDN) is the complete domain name for a device, including the hostname and the domain name.
If you’re using just a single computer that’s not part of a network, then just the hostname is applicable.
Other systems such as WHM and Virtualmin needs the FQDN to be correct too. With WHM is easy with the UI to change the hostname, but with Virtualmin it’s best to set the correct name before installing the control panel software.
Be careful of making the hostname of WHM or Virtualmin the same as an existing working domain – rather separate the hostname to something that will only be used to access the host. WHM warns you about this, and Virtualmin will have Postfix issues if the hostname is the same as a working domain.
How to see the current hostname
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:
root@host:~# 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:
root@host:~# cat /etc/hostname host
How to change 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
If you have a terminal open already, it won’t change the current signature so you’ll have to open a new terminal.
Additional Information
There are more places to look for the hostname excpet looking at /etc/hostname
and /etc/hosts
and the hostname
command Here are the various places you have to go and look to change the hostname:
hostnamectl
displays the current hostname, for example:
# hostnamectl Static hostname: eugene-nuc Icon name: computer-desktop Chassis: desktop Machine ID: b76cc7b1bbdc489e93909d2043031de8 Boot ID: ba0a677263cf490b8098d8e75f43cf5e Operating System: Linux Mint 21.1 Kernel: Linux 5.15.0-60-generic Architecture: x86-64 Hardware Vendor: Intel_R_ Client Systems Hardware Model: NUC6CAYH
To change the hostname using the hostnamectl command, do this:
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/