Configuring the Ubuntu Server Hostname and Host Domain
Published: July 3, 2023 and last edited July 3, 2023
To check the server's current hostname, run this command:
To change the hostname for your server, open the file /etc/hostname in your editor:
sudo vim /etc/hostname
Edit or delete the current hostname and replace it with your hostname. In this 'hostname' file, enter the hostname only, not the entire Fully Qualified Domain Name (“FQDN”). For example, if your FQDN is server1.example.com, enter server1 in this file. After editing, then save the 'hostname' file and exit the editor.
You can add both the hostname and FQDN in the /etc/hosts file:
sudo vim /etc/hosts
Modify the line that starts with 127.0.0.1 to use your FQDN and hostname, in that order:
/etc/hosts 127.0.0.1 localhost 127.0.1.1 hostname.exampledomain.com hostname
Remember to replace hostname with your server's hostname, and replace hostname.exampledomain.com with your own hostname and domain name. Save the 'host' file and exit the editor.
To check whether these names have been changed correctly, reboot the server.
sudo reboot now
While the server is rebooting, you can add an 'A' record for your hostname.domain.com to your primary DNS server. On a Windows DNS server, select the forward zone for yourdomain.com and then add an 'A' record for your hostname in that zone, and the hostname will be prepended to the domain name of the selected zone. Close the DNS.
Next, ssh into your server again. You should see the new hostname on your terminal prompt. For example:
Use the hostname command to verify that the FQDN was set correctly:
You should see your FQDN in the output:
If not, then rinse and repeat (including the server reboot)