In this guide, you would learn what a hostname means, how to view a hostname and how to properly set up a hostname on your Linux system.
What is a Hostname in Linux?
A hostname is a name that is assigned to a device connected to a network and that identifies your server to the rest of the network.
As humans need a name to address each other, a computer or server also need a name to differentiate from one another, so basically, they are human-readable names that are used to identify a specific server or computers, for example, you can have "Faruq", "John", "Clark", and basically anything as your hostnames.
Bear in mind that a hostname differs from a domain name in the sense that the hostname is the name assigned to a device while a domain name is a hostname+DNS domain.
E.g if Computer 1 which is named Mary and Computer 2 which is named Jane is connected to a network named Family, the domain name of the first computer would be mary.family and the second would be jane.family.
I guess you got the idea ;)
Viewing Your Hostname In Linux
There are a couple of ways to view your hostname, one way is to look at your shell prompt: e.g:
You can see my hostname is devsrealm, the default prompt shows the hostname and would strip out any further names e.g if your fully qualified domain name is blog.yourwebsite.com, your prompt will only show blog and nothing more.
If you want to view the fully qualified domain name, you can simply enter
hostname the command:
You can see the Fully qualified domain name is blog.devsrealm.com.
The next step is showing you how to change the hostname...
Changing Hostname In Linux
To change the hostname, you can issue the following command:
sudo hostnamectl set-hostname dev.devsrealm.com
The above command changes the content of a text file called /etc/hostname, you can cross-check for your self by opening the file:
The above command would reflect the name you just changed to.
Once you've changed your hostname, the next step is updating the hostname in /etc/hosts file, the /etc/hosts file associates IP addresses with hostnames. If the hostname is missing in /etc/hosts file, it won't be able to resolve the hostname to an IP address anymore, so basically, you won't be able to serve a web page or do a DNS query.
To change the hosts file, open it in a text editor:
sudo nano /etc/hosts
This is my output:
In my case, I would change the devsrealm to the name I added in the /etc/hostname, which is dev.devsrealm.com, save, and close.