Tips

How to Change Hostname in Debian

This quick tip teaches you various ways to change hostname in Debian Linux distributions.

Abhishek Prakash
Abhishek Prakash

A hostname is basically your computer’s name by which it is recognized on the network. The hostname should be unique to avoid conflicts.

When you install Debian, you are asked to create a user name and a computer name. That is the hostname of your system,

The easiest way to know the hostname of your system is to open a terminal on the system or log in via SSH on the remote Linux system.

In terminal, you’ll notice that the prompt is usually in the fashion of [email protected]

If it doesn’t show you that, you can always use the hostname command:

hostname

Now that you know your computer’s name and don’t like it, you can change it easily. In this tutorial, I’ll show you several ways of changing hostname of a Debian system.

Change hostname in Debian

I am going to show you two command line ways of changing the hostname in Debian Linux. The method should work on both Debian 9 and Debian 10.

Method 1: Use systemd for changing hostname in Debian

The first and preferred method for changing the hostname in Debian is to use the systemd command hostnamectl.

It’s a simple, single command and you don’t even need to restart your system for the changes to be taken in affect.

hostnamectl set-hostname new_hostname

Once you have done this, verify that the hostname change has been successful with the hostname or hostnamectl command:

hostnamectl
   Static hostname: raspberrypi
         Icon name: computer
        Machine ID: 0035faf761f945b8923fc7d54632a941
           Boot ID: c487a76d67a34fd28a5f608aff19281c
  Operating System: Raspbian GNU/Linux 10 (buster)
            Kernel: Linux 4.19.29-v7l+
      Architecture: arm

Exit the shell to see that [email protected] displays the new hostame.

Method 2: Change hostname in Debian using /etc/hosts

The etc directory in Linux contains the various configuration files. A couple of them relate to hostname. You can modify these files to change the hostname of your system.

First, use a command line text editor like Emacs or Vim to edit this file:

vim /etc/hostname

You’ll find just the old hostname here. Delete it and replace it with the new hostname.

It’s not done yet. You have to modify one more file. Go to /etc/hosts file and edit it. Here, replace your old hostname with the new one.

127.0.0.1       localhost
127.0.0.1       new_hostname

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

Save and close the file and then verify it using the hostname or hostnamectl command. That’s it. If you won’t do this step, you’ll encounter “sudo: unable to resolve host” warning message every time you run sudo.

So, you just learn to change the hostname of your Debian server. Personally, I prefer the first method because it’s less of a hassle. How about you? Which method of changing hostname would you prefer?



Join the conversation.