How to Change Hostname in Debian 10

August 11, 2021

Introduction

A hostname is a label used to identify your system on a network. You can change your hostname in Debian 10 (Buster) by using the hostname command or editing system files.

In this tutorial, we will cover different methods you can use to change the hostname in Debian 10.

How to change hostname in Debian 10

Prerequisites

  • A system running Debian 10 (Buster)
  • An account with sudo level privileges
  • Access to the terminal window/command line

Check the Current Hostname

Use the following command to check the current hostname:

hostname

The output displays the current hostname as plain text:

Checking the current system hostname

Change the Hostname

The first method involves using the hostname command to change the system name. The hostname command is used to obtain and change the system's hostname.

It is important to note that any changes made using the hostname command are temporary. Unless you use the permanent method of changing the hostname outlined below, it will revert back to its original value after the next system reboot.

Follow the steps below to change the hostname:

1. Changing the hostname requires a user with sudo/root permissions. To switch to the root user, enter:

sudo -s

Note: Learn how to grant sudo privileges in our guide on creating a sudo user in Debian.

2. Use the hostname command to set up a new hostname:

hostname [name]

Where:

  • [name]: The new hostname you want to set up.

In this example, we are using phoenixNAP as the new hostname:

hostname phoenixNAP

3. Check if the new hostname is set up correctly:

hostname
Checking the new hostname

Change the Hostname Permanently

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

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

Regardless of the method you use, you also need to edit the system's hosts file to match the changes in hostname.

To change the hostname permanently:

1. As the root user, open the hostname file:

sudo nano /etc/hostname

The first line of the hostname file lists the current hostname. Replace it with the new name you want to set up:

Replace the old name in the hostname file

Press Ctrl+X to exit the file, then type Y and press Enter to save the changes.

Another method of setting a new permanent hostname is by using the hostnamectl command:

hostnamectl set-hostname [name]

2. Open the hosts file:

sudo nano /etc/hosts

3. In the hosts file, find all the instances of the old hostname and replace them with the new one:

Replace the old name in the hosts file

Press Ctrl+X, and then type Y and press Enter to exit the file and save the changes you made.

4. Restart the hostname.sh shell script for the changes to take effect:

invoke-rc.d hostname.sh start

Hostname Format Restrictions

By default, static hostnames follow the same restrictions as Internet domain names:

  • They have a minimum length of 2 and maximum length of 63 characters.
  • They support letters from a to z, numbers from 0 to 9, and hyphens (-).
  • They can't start or end with a hyphen, or have two or more consecutive hyphens.

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

The error message when setting a hostname with special characters

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, if we want to set phoenixNAP's test system as the pretty hostname:

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

Verify the Hostname Change

Verify the new hostname by running the following command:

hostnamectl

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

Verify if the hostname was successfully changed

Conclusion

After reading this tutorial, you should be able to change the hostname in Debian 10 using commands or editing system files.

Was this article helpful?
YesNo
Aleksandar Kovačević
With a background in both design and writing, Aleksandar Kovacevic aims to bring a fresh perspective to writing for IT, making complicated concepts easy to understand and approach.
Next you should read
How To Install Debian 10 (Buster)
October 14, 2019

Debian has released its newest stable version Debian 10, nicknamed Buster. The latest release includes many updates. This article will help you how to install Debian 10 Buster as your operating system...
Read more
How to Use apt-get reinstall on Debian and Ubuntu
October 22, 2020

When packages are accidentally removed or modified on Debian or Ubuntu, reinstalling them usually resolves the problem. The --reinstall flag is an apt-get command shortcut for reinstalling packages using the command line.
Read more
How to Fix 'add-apt-repository command not found' on Ubuntu & Debian
March 21, 2024

This guide shows you how to fix the 'add-apt-repository command not found' error in Ubuntu. This specific Ubuntu error appears when trying to add a new software repository...
Read more
How to Generate & Set Up SSH Keys on Debian 10
September 14, 2020

This article will help system administrators configure SSH Keys on Debian 10. The instructions allow you to set up encrypted credentials quickly, log in remotely, and disable authentication.
Read more