Introduction
The hostname of a linux system is an important component of the system. Not only that one can identify one’s own system here, also software often needs a correctly set hostname for clear identification. In functionality the hostname will bring up a name for your IP address. The host name is a domain name given to a host machine. You can set different hostnames, but to get fully qualified we should set just one per host. For example we use: foo.bar. Sure, we can name the system foo.bar if we just had this little server for just a little static webpage. But more recommended is to choose a name for the host like “mail” for example for a mailserver or “web” for a webserver. So in our example (a little server for just different things) we use srv1.foo.bar so we can later – if we need more servers – just name the next one srv2.foo.bar.
Prerequisites
- To make the hostname fully qualified you need to set the rDNS entry of your machine to the wanted fqhn (fully qualified hostname). You can do this in the web console of your server.
- To make your hostname fully qualified you need to create an “A” record in your DNS zone for your hostname. So in our example for the first little server for our domain foo.bar we create an “A” record with the value “srv1.foo.bar” and the IP address to the one of our machine. After 12-48 hours the DNS changes should be globally propagated.
- To check and change the hostname on your server, make sure you are logged into your server with a sudo user.
- This guide is based on a fresh installed Debian 9 server.
Step 1 – Check the hostname
To check the actual hostname, there are three different ways. For the first you can type: sudo cat /etc/hosts
The second command to show the hostname is: sudo hostname
And the last way is: sudo hostname -f
The last option will show you the fully qualified hostname.
Step 2 – Change the hostname
When changing the hostname we have different possibilities. The simplest method would be this option: sudo hostnamectl set-hostname servername.fqdn.tld
Here you just need to change servername.fqdn.tld to your server’s fully qualified hostname, and the daemon will do the rest. Then restart the SSH service and the new hostname is set: sudo systemctl restart ssh
You also can change the hostname manually. We just open some files and change/check the hostname: sudo nano /etc/hostname
In this file only the name of the server should appear. In our case “servername”. If not or you want to do it manually, just change it to the wanted name. Next we check, if the mailname is the same: sudo nano /etc/mailname
Also we check our /etc/hosts file: nano /etc/hosts
It should look something like this:
127.0.1.1 servername.fqdn.tld servername
127.0.0.1 localhost
::1 ip6-localhost ip6-loopback servername.fqdn.tld servername
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
Here you can see where the hostname is set and just change it to the one you want.
After the changes, if we did some manually, we restart the ssh service: sudo systemctl restart ssh
Conclusion
You have successfully checked and changed the hostname of your server.
Reprint:https://community.hetzner.com/tutorials/check-and-change-hostname-on-debian-9
Leave a Reply