Setting Hostname in Debian

There are multiple ways of setting hostname in Debian or Linux system. You can temporarily change the hostname, or permanently apply the changes to a system. Let’s go over these methods.

Setting hostname temporarily

Let’s start with the temporary and simple method. The hostname command in Debian allows you to set up the system hostname until the system is restarted. Let’s see how we can change the hostname temporarily in Debian.

root@HowLinux:~# hostname HowLinux-Test
Temporary Hostname Setup In Debian
Temporary Hostname Setup In Debian

As you can see, the new hostname reflects immediately. But the moment I restart my system, the hostname is back to the original one. How do we change?

Steps to setting hostname in Debian permanently

This involves editing 2 files in Debian so the changes stick around even after we restart our systems. The files that we will need to edit are:

  • /etc/hostname
  • /etc/hosts

The /etc/hostname file stores the permanent hostname of the system while the /etc/hosts file points the hostname to the local IP address. Let’s start with editing the hostname file first.

Editing the /etc/hostname file

Let’s start by checking the contents of the hostname file. For this purpose, we’ll make use of the cat command in Linux. Let’s see what the hostname for our system is.

root@HowLinux:~# cat /etc/hostname
Hostname File
Hostname File

We’ll quickly edit the file with the use of the cat command since we just have to replace the hostname. You can also use any of your favorite text editors to perform the same action.

root@HowLinux:~# cat > /etc/hostname
HowLinux-TEST
Edit Hostname File Using Cat Command
Edit Hostname File Using Cat Command

Alternative: Using the hostnamectl command

You can make use of the hostnamectl command from the new systemd that was introduced in Linux a while back. The hostnamectl command allows a proper API to interact with the hostname in Linux without having to hack into any files.

root@HowLinux:~# hostnamectl set-hostname HowLinux --static
Hostnamectl Command
Hostnamectl Command to set hostname

The –static option will automatically edit the hostname file and setup the static hostname for you. A few other options that can explore:

  • –pretty : Allows you to set up a temporary hostname that stays active only for the session and changes once rebooted
  • –transient : This is a dynamic hostname that by default changes to the “–static” hostname on reboot, but can change according to what is set in the DHCP server.

Editing the /etc/hosts file

The above methods will change the /etc/hostname file that will make the hostname stick even after a reboot. Now, our next step is to edit the /etc/hosts file to point to our local IP address. So any data packets that are sent out addressed to the hostname are also delivered to the local IP address.

root@HowLinux:~# nano /etc/hosts
Editing The Hosts File
Editing the Hosts File

This command will give you similar output to the one below.

Editing The Hosts File 2
Editing The Hosts File 2
root@HowLinux:~# ping HowLinux
Resolving Hostname
Resolving Hostname

After we’ve edited the /etc/hosts file, the hostname will automatically resolve and point to the local address which is 127.0.0.1. The hostname will also auto-resolve to your system’s LAN IP when on a network of computers making it easier for other users to communicate with the system.

Conclusion

In this short tutorial, we learned how setting hostname in Debian works. It’s quick, easy and a pretty useful thing to keep at the back of your hand when working with Linux systems. When working with multiple servers, having a good hostname naming convention will make things very simple for you.