A hostname is a unique identifier that is assigned to a device (host) connected to the network. It allows to distinguish one device from another on a specific network. This tutorial provides 5 methods to get hostname in Linux.
Method 1 - hostname command
The hostname
command allows to display hostname in Linux. Run this command without any arguments:
hostname
Output example:
john-pc
Method 2 - uname command
The uname
command displays information about the system. The -n
option prints the hostname.
uname -n
Method 3 - hostnamectl command
The hostnamectl
command allows to get short information about the system. Command also can be used to get or set the hostname.
hostnamectl --static
Method 4 - /proc/sys/kernel/hostname file
The /proc/sys/kernel/hostname
file contains the hostname. Get it with the cat
command:
cat /proc/sys/kernel/hostname
Method 5 - /etc/hostname
In most Linux distributions, the hostname is stored in the /etc/hostname
file. Display it's content:
cat /etc/hostname
Leave a Comment
Cancel reply