Set hostname and fqdn in Ubuntu 18.04 & 20.04 & 22.04

Hostname in Ubuntu 18.04 & 20.04 & 22.04

You can find and change the hostname with the commands hostname or hostnamectl in Ubuntu 18.04 & 20.04, if you run the command on its own it will tell you what the current name is, the example below is  named racknerd-la

hostnamectl
root@racknerd-la:~# hostnamectl
Static hostname: racknerd-la
Icon name: computer-vm
Chassis: vm
Machine ID: 38db1f2160cc4dad9a4a1999bdf332a91
Boot ID: 477dac72e3694ab6a0aabb070f8f2110
Virtualization: kvm
Operating System: Ubuntu 18.04.4 LTS
Kernel: Linux 4.15.0-106-generic
Architecture: x86-64

To change the hostname use hostnamectl set-hostname

hostnamectl set-hostname newhostname

This will change the hostname, verify by running hostnamectl or hostname command.

Fully Qualified Domain Name (FQDN) in Ubuntu 18.04

Yo see the fully qualified domain name run

hostname  --fqdn

To change it you need to change the domain in /etc/hosts in the example below…

nano /etc/hosts
127.0.0.1 localhost localhost.localdomain

::1 localhost localhost.localdomain

203.125.45.92 racknerd-la.wpbeaches.com racknerd-la

The domain name is marked followed by the hostname, it happens to be a subdomain name with the the same name as the hostname ‘racknerd-la’ just change the domain name and test hostname  –fqdn again to verify.

Fully Qualified Domain Name (FQDN) in Ubuntu 20.04, 22.04

hostnamectl set-hostname your.hostname.com

Verify it

hostnamectl status

Domain Name now should also be correct, verify it…

dnsdomainname

To add a reverse dns entry to the fully qualified domain name I find that most VPS providers will have an option via their control panels to set this or a support ticket needs to be raised.

Leave all Comment