Home Linux How To Find Hostname In Linux
How To Find Hostname In Linux

How To Find Hostname In Linux

By sk
1116 Views

A Hostname is an unique alphanumeric label assigned to a Linux system in order to identify it on the network. It can also contain a few special characters such as hyphen (-), period (.), and underscore (_). A typical hostname consists of up to 253 characters. Generally, the hostname is stored in /etc/hostname file in most Linux distributions. In this brief guide, we will learn about various commands to find hostname in Linux operating systems.

1. Find Hostname in Linux with hostname command

The hostname command is used to display a Linux system's DNS name and Hostname. It also allows the users to change or set the Hostname.

To find Hostname in Linux, run hostname command without any options:

$ hostname
ostechnix
Find Hostname in Linux with hostname command
Find Hostname in Linux with hostname command

As you see in the output, my Linux system's hostanme is ostechnix.

If you want to show the FQDN (Fully Qualified Domain Name), run:

$ hostname --fqdn

2. Display Hostname with hostnamectl command in Linux

The hostnamectl command is used to get and set the system hostname and related settings in Linux.

To display the hostname of your Linux machine, run hostnamectl command without any options:

$ hostnamectl

Sample output:

   Static hostname: ostechnix
         Icon name: computer-laptop
           Chassis: laptop
        Machine ID: 09bfdb996ad847c782739db185f3e634
           Boot ID: 97d0b5bdeacd4f8e852a042c9228be74
  Operating System: Ubuntu 20.04.1 LTS
            Kernel: Linux 5.4.0-62-generic
      Architecture: x86-64
Display Hostname with hostnamectl command in Linux
Display Hostname with hostnamectl command in Linux

Unlike the hostname command, hostnamectl displays a few more details of your system such as type of the system (laptop or desktop), Boot & machine ID, Operating system and Kernel version, Architecture etc.

To display only the Hostname, use --static flag:

$ hostnamectl --static
ostechnix

3. Check Linux Hostname with nmcli command

The nmcli is a command line tool to control NetworkManager and report the network status. With nmcli, we can create, display, edit, delete, activate, and deactivate network connections, as well as control and display network device status.

To check the hostame of a Linux system with nmcli tool, run this command:

$ nmcli general hostname
ostechnix

4. View Hostname using uname command

The uname is a command line tool to print all system information such as hostname, Kernel release, Kernel version, operating system, and processor architecture etc.

To view hostname with uname command, run it with -n option:

$ uname -n
ostechnix

5. Get Hostname by reading /etc/hostname file

As stated already, the hostname is stored in /etc/hostname file in most Linux distributions. To get the hostname of a Linux system, simply display the contents of /etc/hostname file:

$ cat /etc/hostname 
ostechnix

These are the most commonly used commands to find hostname in Linux. There are also a two uncommon ways to check Linux hostname. They have been given below for your reference.

The sysctl command is used to list and modify kernel parameters at runtime.

We can print a Linux system's hostname using sysctl command line below:

$ sysctl kernel.hostname 
kernel.hostname = ostechnix

7. Determine Hostname in Linux with ProcFS

Proc file system (or shortly procfs) is a virtual file system maintained by the Linux kernel. It is also sometimes referred to as a process information pseudo-file system. It doesn't contain 'real' files but runtime system information such as system memory, devices mounted, hardware configuration etc. All these information are available under a special directory named /proc in Linux.

To determine the hostname of Linux with prcfs, run:

$ cat /proc/sys/kernel/hostname 
ostechnix

You know now different methods to view a Linux system's hostname. How will you change it? The following guide explains how to set a new hostname or change the existing hostname, either from command line or from Settings section in graphical mode.

Hope this helps.

Related read:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More