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.
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:
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
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:
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:
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:
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
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:
Conclusion
After reading this tutorial, you should be able to change the hostname in Debian 10 using commands or editing system files.