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
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
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.
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
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.
127.0.1.1 LinuxShellTips
Now you have to restart network services to make the changes effective.
$ sudo systemctl restart network-online.target
Run ping command to verify if mapping works.
$ ping -c4 LinuxShellTips
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.
Nice article as I was just changing the hostname not what was in the hosts’ file. Now all is well.
@Marshall,
I am glad that the article worked for you in setting up your hostname in Debian Linux.