Check and change hostname in Debian

0 Shares
0
0
0
0

Introduction

The hostname of a Linux system is an important part of the system. You can use it not only to identify the system to yourself, but also to software, which often requires a properly configured hostname for clear identification. In practice, a hostname is a name that appears to be your IP address. A hostname is the domain name given to the host machine. You can configure multiple hostnames, but to be fully qualified, we should configure only one name for each host.

For example, if you only have one server for a small static web page, you could use something like foo.bar.

However, the recommended way is to choose a name that is displayable to the server, for example:

  • Mail for a mail server
  • Web for a web server
Prerequisites
  • To make the hostname fully qualified, you need to set your device's rDNS entry to the desired FQHN (Fully Qualified Hostname). You can do this in your web server console.
  • In order for your hostname to be fully qualified, you need to create an “A” record in the DNS zone for your hostname. So in our example, for the first small server for the domain foo.bar, we would create an “A” record with the value srv1.foo.bar and the IP address to one of our machines. After 12-48 hours the DNS changes should propagate globally.
  • To check and change the hostname on your server, make sure you are logged in to your server with a sudo user.
  • This guide is based on a server with a fresh Debian 12 installation.
Example terms

In our example (a small server just for various things) we use srv1.foo.bar so that later – if we need more servers – we can just name the next server srv2.foo.bar.

Step 1 – Check the hostname

There are three different ways to check the real hostname.

  • First:
sudo cat /etc/hosts
  • Second:
sudo hostname
  • Third:
sudo hostname -f

The last option shows you the fully qualified hostname.

Step 2 – Change the hostname

There are several ways to change the hostname.

Change hostname via a command

The simplest way would be this option:

sudo hostnamectl set-hostname servername.fqdn.tld

Then, restart the SSH service and the new hostname will be set:

sudo systemctl restart ssh
Change the hostname manually

You can also change the hostname manually. Just open a few files and change/check the hostname:

  • Hostname in /etc/hostname

In this file only the server name should appear. In our case “ServerName”. If not or you want to do it manually, just change it to the desired name.

sudo nano /etc/hostname
  • Email name in /etc/mailname
sudo nano /etc/mailname
  • Hostname in /etc/hosts
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 name you want.

After saving the changes, restart the SSH service:

sudo systemctl restart ssh

Result

You have successfully checked and changed your server hostname.

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like