facebook youtube pinterest twitter reddit whatsapp instagram

Easy Guide To Setting A Hostname In Linux/Ubuntu

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.

First,

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:
Hostname at shell prompt

You can see my hostname is devsrealmthe 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:

fully qualified domain name

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:

cat /etc/hostname

or

nano /etc/hostname

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:

 etc-hosts file

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.

That's' it!

Related Post(s)

  • Setting Up Send-Only Mail Server From Scratch (With Haraka)

    In this guide, I would walk you through the steps of setting up an email server that can be used as a send-only mail server, we would not be dealing with receiving mails, we only care about sending em

  • Send Mail with Attachment Using Mutt in GNU/Linux

    Mutt is a powerful text-based mail client for Unix/Linux operating systems. It features color support, message threading, MIME support...

  • Installing WP-CLI In a GNU/Linux Server

    WP-CLI is a command-line interface for WordPress. It can also be used with ClassicPress, as they are no differences in their usage, maybe just minimal if you are updating or downloading new ClassicPr

  • Mounting and Unmounting Cloud Storage With (Rclone) in GNU/Linux

    Ever wondered if you could mount your preferred cloud storage as a virtual drive on your system? Thanks to Rclone, you can mount and access different kinds of cloud storage from a file manager, I'll

  • Monitoring Multiple Log Files In RealTime With MultiTail (Ubuntu)

    Oh my... I really find scanning through the logs file time consuming, and painful. Luckily for me, I founded Multitail, which is an awesome, and powerful tool for not only browsing through several f

  • Access A Web Page In GNU/Linux Using (Links) [Web Browser in Text Mode]

    Links is a text-mode World Wide Web (WWW) browses that can be used to view a web page either via a local (file://) or remote ((http:// or ftp://) URLs. To get started with Links, install it using the