How to Set or Change Hostname in Debian Linux

Setting up hostname is an important task for any system administrator or anyone who uses Linux. It is tedious to remember a machine by IP address. Hostname helps humans to remember the machine name easily.

In this article, you will learn how to set or change the system hostname in the Debian machine. This procedure is well situated for any Debian flavors of Linux machine you run with.

Check Debian System Hostname

First, check what is the current hostname for your Debian machine. There are many commands available that can give you the hostname of your machine.

$ uname -n
$ hostname
$ hostnamectl 
$ cat /etc/hostname
Check Debian Hostname
Check Debian Hostname

I am running a Debian 10 buster in vagrant and my hostname is primarily set to localhost. You can confirm this by running the lsb_release command, which will display information about your Linux distribution, version number, release codename, and distributor ID.

$ lsb_release -a
Check Debian System Info
Check Debian System Info

Change Debian System Hostname

You can set or change the hostname temporary or permanent. The difference is former will not persist after reboot while the latter will persist even after reboot.

Change Debian Hostname Temporary

To set a temporary hostname run the following command.

$ sudo hostname LinuxShellTips
$ hostname [To check the modified hostname]
$ bash     [To start a new session]

You have to start a new terminal or open a new bash session for the changes to be effective. You can check the /etc/hostname file and this temporary change will not be updated in the file.

Change Debian Hostname Temporary
Change Debian Hostname Temporary

Reboot the machine and you will see the changes gone.

Change Debian Hostname Permanently

To set or change a hostname permanently run the following hostnamectl command – is a utility that comes with machines running with systemd.

$ sudo hostnamectl set-hostname LinuxShellTips
$ bash
$ cat /etc/hostname
Change Debian Hostname Permanent
Change Debian Hostname Permanent

You can check the /etc/hostname file where the hostname is modified according to our hostnamectl command. Now we have to add an entry in the /etc/hosts file to map hostname with IP address.

$ sudo nano /etc/hosts

I am mapping Hostname with a Loopback address. LinuxShellTips
Add Hostname in Host File
Add Hostname in Host File

Now you have to restart network services to make the changes effective.

$ sudo systemctl restart

Run ping command to verify if mapping works.

$ ping -c4 LinuxShellTips
Verify Hostname Connectivity
Verify Hostname Connectivity

That’s it for this article. This might be seen as a simple tutorial but a mandatory step for everyone who is using a Linux machine.

Tutorial Feedback...
Was this article helpful? If you don't find this article helpful or found some outdated info, issue or a typo, do post your valuable feedback or suggestions in the comments to help improve this article...

2 thoughts on “How to Set or Change Hostname in Debian Linux”

  1. Nice article as I was just changing the hostname not what was in the hosts’ file. Now all is well.


Got Something to Say? Join the Discussion...

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.