How to Change Hostname on Debian

By
Sara Zivanov
Published:
January 22, 2025

Introduction

A hostname is a unique label that identifies a system on a network, making it easier for devices to communicate. In Debian, you can change your hostname temporarily using the hostname command or permanently by editing system config files like /etc/hostname and /etc/hosts.

In this tutorial, you will learn different methods to change the hostname in Debian.

How to Change Hostname on Debian heading image

Prerequisites

How to Check Current Hostname in Debian

Knowing how to check the current hostname in Debian is important because it is a critical part of network identification. Use the following command to check the current hostname:

hostname
hostname terminal output

The output displays the current hostname as plain text.

Changing the Hostname in Debian

In Debian, change the hostname either temporarily, using commands, or permanently by modifying configuration files. The following text explains how to change the hostname using each method.

Change Hostname in Debian Temporarily

The hostname command is used to obtain and change the system's hostname.

Any changes made using the hostname command are temporary, and it reverts to its original value after the next system reboot.

To set up a new hostname, run:

hostname [name]

In this case, [name] represents the new hostname you want to set up.

For example, use phoenixNAP as the new hostname:

sudo hostname phoenixNAP

The command has no output. Check whether the new hostname is set up correctly with:

hostname
terminal output for hostname

Change Hostname in Debian Permanently

There are two ways to change your hostname in Debian permanently:

  • Using the hostnamectl command.
  • Editing the hostname file.

Regardless of your chosen method, you must update the system's /etc/hosts file to reflect the new hostname.

One way to set a new permanent hostname is with the hostnamectl command. The syntax is:

hostnamectl set-hostname [name]

To accomplish this, take the following steps:

1. Run the following to change the hostname back to pnap:

hostnamectl set-hostname pnap

The command has no output.

2. Open the hosts file:

sudo vim /etc/hosts

3. Find all the instances of the old hostname and replace them with the new one:

change Debian hostname in /etc/hosts file in Vim terminal output

4. Save and exit the file.

5. Reboot the system for the changes to take effect:

sudo reboot

6. Verify the new hostname by running the following command:

hostnamectl
verify new hostname on Debian

The output lists the new hostname in the Static hostname section:

Another way to change the hostname permanently is by editing the /etc/hostname file. To accomplish that, take the following steps:

1. Open the hostname file with a text editor of choice. In this case, Vim:

sudo vim /etc/hostname

The first line of the hostname file lists the current hostname.

Hostname in /etc/hostname file in Vim terminal output

2. Replace the line with the new name you want to set up. For example, replace it with phoenixNAP:

New hostname in /etc/hostname file in Vim terminal output

3. Save and exit the file.

4. Open the hosts file, locate all the instances of the old hostname, and replace them with the new one.

5. Save and exit the file.

6. Reboot the system.

7. Verify the changes with:

hostnamectl

Hostname Format Restrictions in Debian

Static hostnames in Debian must adhere to the same rules as Internet domain names:

  • The length must be between two and 63 characters.
  • Hostnames can include letters (A-Z), numbers (0-9), and hyphens (-).
  • They cannot begin or end with a hyphen nor contain consecutive hyphens.

Using the hostname command with a special character produces the following error:

hostname phoenixNAP!
hostname phoenixNAP! terminal output

Pretty hostnames are high-level hostnames assigned to users or admins. These hostnames can contain special characters and are assigned using the hostnamectl command with the --pretty option:

hostnamectl set-hostname "[name]" --pretty

Note: When adding a pretty hostname, the name you want to assign must be surrounded by double quotation marks.

For example, to set phoenixNAP's test system as the pretty hostname, run:

hostnamectl set-hostname "phoenixNAP's test system" --pretty

The command has no output. Verify the change with:

hostnamectl
verify hostname change on debian

Conclusion

This tutorial explained how to change the hostname in Debian temporarily using commands. It also covered how to make the change permanent by editing system files.

Next, learn about Domain Name System (DNS) and how it works.

Was this article helpful?
YesNo