Debian

Change Hostname Debian 10

The hostname of a system is basically the name of the system over a network. Whenever installing Debian, the installer will ask for a username and a computer name. It serves as the hostname of the system. Any hostname should be unique to avoid conflicts over the network.

Although first assigned during installation, we can change the hostname later on. This guide showcases changing the hostname on Debian 10.

Debian 10 Hostname

The hostname is mandatory during the installation of Debian 10. The demonstrations were performed on a properly-configured Debian 10 virtual machine. For reference, check out this guide on installing Debian 10.

The hostname is a system-wide configuration. Trying to make any changes requires permission from the root. Here, we’ll assume that you have access to the root account or a non-root user with sudo permission. Check out how to add users to sudoers in Debian 10.

Displaying Current System Hostname

First, we need to know how to check the current hostname of the system. There are a couple of ways we can check it.

Displaying Hostname Using hostnamectl

All the Linux distros that use systemd comes with the tool hostnamectl. It’s a dedicated command-line tool to work with the system hostname.

The following command will print the current hostname.

$ hostnamectl

In the report, hostnamectl will list various information. For example, processor architecture, kernel version, OS info, virtualization, etc.

Displaying Hostname Using hostname

Similar to hostnamectl, hostname is also a dedicated command to display the hostname of the system.

Run hostname.

$ hostname

The hostname tool is a part of a group of tools that prints various system info. For example, domain name, DNS domain name, NIS/YP domain name etc.

Run the following command(s) to print the system’s NIS/YP domain name.

$ domainname
$ ypdomainname
$ nisdomainname

The following command will show the system’s DNS domain name.

$ dnsdomainname

For further in-depth info, check out the hostname man page.

$ man hostname

Displaying Hostname From /proc/sys/kernel/hostname

As the title of this section suggests, the file located at the following location contains the hostname of the system. We can read the content using cat.

$ cat /proc/sys/kernel/hostname

Displaying Hostname From /etc/hostname and /etc/hosts

Similar to the previous section, the files located at /etc/hostname and /etc/hosts also contain the hostname of the system.

Check out the content of these files.

$ cat /etc/hostname
$ cat /etc/hosts

Changing Hostname

There are multiple ways to change the system hostname. In most cases, it’s strongly recommended to use hostnamectl to change the system hostname.

Changing Hostname Using hostnamectl

Hostnamectl is a dedicated tool to manage the system hostname. The following hostname command will change the static hostname.

$ sudo hostnamectl set-hostname "<new_hostname>"

Hostnamectl distinguishes three different types of hostnames.

Pretty hostname: It’s the high-level hostname that may contain all kinds of special characters.

Static hostname: The hostname that initializes the kernel hostname at boot.

Transient hostname: It’s a fallback value assigned during network configuration. If the static hostname is available and valid, then the transient hostname isn’t used.

The static and transient hostnames have a limit of 64 characters at maximum.

To change the pretty hostname, add the flag –pretty.

$ sudo hostnamectl set-hostname "<new_pretty_hostname>" --pretty

The hostnamectl tool can also change the value of other host-related info. For example, chassis, deployment environment, location, etc. For further info, check out the hostnamectl man page.

$ man hostnamectl

Changing Hostname Manually

Instead of using hostnamectl, we can manually tweak the content of a couple of hostname-related files and change the system hostname.

We’re going to edit the two files /etc/hostname and /etc/hosts. It requires root permission (or sudo privilege) to make changes.

The file /etc/hostname contains the current hostname. Open up the file in a text editor and change the hostname.

$ sudo nano /etc/hostname

Next, we’ll make changes to /etc/hosts. This file links hostnames to IP addresses. Open it up in a text editor and change the hostname of the current system.

$ sudo nano /etc/hosts

These changes won’t take into effect without system reboot.

$ sudo systemctl reboot

Changing Hostname Using GUI

We can also change the hostname using GUI. Here, I’ll demonstrate how to change it on the GNOME desktop.

Go to “Settings”.

From the left panel, scroll down and go to “Details”.

From the “About” tab, click the field “Device name”.

Changing the value of the device name will change the hostname of the system.

Final Thoughts

Hostname is a simple yet important value for every operating system. On Debian, changing the hostname is a simple task. After consulting this guide, you should be able to change the hostname on any Debian system. Follow whichever method suits you the best.

As we’ve seen, the hostname is used when connecting to a network. Learn how to find the hostname of a system given its IP address.

Happy computing!

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.