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
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
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
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
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
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
Supports mounting file system.
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
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