Get Rewarded! We will reward you with up to €50 credit on your account for every tutorial that you write and we publish!

Check and change hostname on Debian

profile picture
Author
yFStein
Published
2024-07-31
Time to read
4 minutes reading time

Introduction

The hostname of a Linux system is an important component of the system. You cannot only use it to identify the system for yourself but also for software, which often needs a correctly set hostname for clear identification. In functionality, the hostname will bring up a name for your IP address. The hostname 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.

If you only have one server for a small static webpage, for example, you can use something like foo.bar.

The reccomended way, however, is to choose a name that is representable of the server, for example:

  • mail for a mailserver
  • web for a webserver

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 server with a fresh installion of Debian 12.

Example terminology

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.

Step 1 - Check the hostname

To check the actual hostname, there are three different ways.

  • First:

    sudo cat /etc/hosts
  • Second:

    sudo hostname
  • Third:

    sudo hostname -f

The last option will show you the fully qualified hostname.

Step 2 - Change the hostname

There are different ways to change the hostname.

Change the hostname via a command

The simplest method would be this option:

Replace servername.fqdn.tld to your server's fully qualified hostname.

sudo hostnamectl set-hostname servername.fqdn.tld

Then, restart the SSH service and the new hostname is set:

sudo systemctl restart ssh

Change the hostname manually

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

  • Hostname in /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.

    sudo nano /etc/hostname
  • Mailname 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 one you want.

After you saved the changes, restart the SSH service:

sudo systemctl restart ssh

Conclusion

You have successfully checked and changed the hostname of your server.

License: MIT
Want to contribute?

Get Rewarded: Get up to €50 in credit! Be a part of the community and contribute. Do it for the money. Do it for the bragging rights. And do it to teach others!

Report Issue

Discover our

Dedicated Servers

Configure your dream server. Top performance with an excellent connection at an unbeatable price!

Want to contribute?

Get Rewarded: Get up to €50 credit on your account for every tutorial you write and we publish!

Find out more