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 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
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:
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
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
$ hostnamectl --static ostechnix
3. Check Linux Hostname with
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
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
$ uname -n ostechnix
5. Get Hostname by reading
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
$ 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.
6. Print Hostname in Linux with
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
Hope this helps.