
hostname Command in Linux
The hostname command in Linux is used to display the computerâs hostname and domain name (DNS) (Domain Name Service), as well as set a computerâs hostname or domain name.
- A hostname is a name assigned to a computer attached to the network that uniquely identifies over a network, allowing it to be accessed without using its IP address.
- When you run the hostname command without any arguments, it displays the current hostname of the system. You can achieve this by using the gethostname function, which retrieves the name of the system.
- If you provide one argument (e.g., hostname new-hostname), the command sets the systemâs hostname to the new value. You can use the sethostname function to perform this.
- You can also set the hostname by specifying a file that contains the desired hostname using the --file option. Besides this, only the superuser (root) can change the hostname or NIS domain name.
Table of Contents
Here is a comprehensive guide to the options available with the hostname command −
Syntax of hostname Command
The following is the general syntax for the hostname command −
hostname -[option] [file]
hostname Command Options
The following is a breakdown of options for the hostname command in Linux −
Options | Description |
---|---|
-a, --alias | Displays the alias name of the host, if one is set. This option is deprecated and should not be used anymore. |
-A, --all-fqdns |
Displays all Fully Qualified Domain Names (FQDNs) of the machine. It lists all configured network addresses on all network interfaces and translates them to DNS domain names. Addresses without a reverse IP entry are skipped. The output may contain duplicates and the order is not guaranteed. |
-b, --boot | Always sets a hostname. If the file specified by -F does not exist or is empty, the default hostname localhost is used if no other hostname is set. |
-d, --domain | Displays the DNS domain name. Do not use the domainname command for this purpose as it shows the NIS domain name instead. Use dnsdomainname instead. |
-f, --fqdn, --long | Displays the Fully Qualified Domain Name (FQDN), which includes the short hostname and the DNS domain name. You can change the FQDN and DNS domain name in the /etc/hosts file unless you are using bind or NIS for host lookups. |
-F, --file filename | Reads the hostname from the specified file. Lines starting with # are ignored. |
-i, --ip-address | Displays the network address(es) of the hostname. This works only if the hostname can be resolved. Itâs better to use hostname --all-ip-addresses. |
-I, --all-ip-addresses | Displays all network addresses of the host. it lists all configured addresses on all network interfaces, excluding the loopback interface and IPv6 link-local addresses. The output order is not guaranteed. |
-s, --short | Displays the short hostname, which is the hostname cut at the first dot. |
-V, --version | Prints version information and exits successfully. |
-y, --yp, --nis | Displays the NIS domain name. If a parameter or --file name is given, root can set a new NIS domain. |
-h, --help | Prints a usage message and exits. |
Examples of hostname Command in Linux
The following are some practical examples of the hostname command that can help you understand its usage −
- Display the Current Hostname
- Set a New Hostname
- Set a Permanent Hostname
- Display the Alias Name
- Display All FQDNs (Fully Qualified Domain Names)
- Display the Domain Name
- Display the IP Address
- Display All IP Addresses
- Display the Short Hostname
Display the Current Hostname
To display the current hostname of your system, you can use the hostname command as follows −
hostname
This command displays the hostname of your system.

Set a New Hostname
To set a new hostname on your system, you can use the following syntax −
sudo hostname new-hostname
This command changes the old hostname to a new value. However, keep in mind that this change is temporary and will revert to the original hostname after a reboot.

Set a Permanent Hostname
To set a permanent hostname on your system, you can use the hostnamectl command, which is part of systemd. This method is straightforward and ensures the hostname change persists across reboots.
sudo hostnamectl set-hostname new-hostname
This command updates the hostname immediately and makes it permanent by modifying the necessary configuration files.

If you need to verify the change, you can use −
hostnamectl
This command displays the current hostname along with other system information.

Display the Alias Name
To display the alias name of your system, you can use the hostname command with the "-a" option −
hostname -a
This command shows the alias name set for the host. If no alias is set, it returns an empty line.

Display All FQDNs (Fully Qualified Domain Names)
To display all Fully Qualified Domain Names (FQDNs) associated with your system, you can use the hostname command with the "-A" option −
hostname -A
This command lists all the FQDNs for the host. If there are multiple FQDNs configured, they are displayed separated by spaces.

Display the Domain Name
To display the domain name of your system, you can use the hostname command with the "-d" option −
hostname -d
This command outputs the domain name part of your systemâs Fully Qualified Domain Name (FQDN). If your system is not part of a domain, it returns an empty line.

Display the IP Address
You can use the hostname with the "-i" flag to display the IP address of the host. When you run this command in a terminal, it returns the IP address associated with the hostname of your machine.
hostname -i
This command is particularly useful when you need to quickly find out the IP address of your system without going through network settings.

Display All IP Addresses
You can use the hostname with the "-I" option to display all IP addresses assigned to the host. When you run this command, it returns a list of all IP addresses associated with your machine, separated by spaces.
hostname -I
This is particularly useful if your machine has multiple network interfaces or IP addresses.

Display the Short Hostname
To display the short hostname of your machine, you can use the hostname command with the "-s" flag. The short hostname is the part of the hostname up to the first dot.
hostname -s
When you run this command, it returns just the short hostname, which is useful for quickly identifying your machine without the full domain name.

Conclusion
The hostname command in Linux is a versatile tool that not only displays the current hostname and domain name but also allows you to set and manage these identifiers efficiently.
Understanding its various options such as displaying all Fully Qualified Domain Names, and retrieving IP addresses can significantly enhance system administration and network management.
By mastering the hostname command, you can streamline your workflows and maintain clarity in network environments, making it an essential aspect of Linux system management.