The hostname command in Linux is a command-line utility that allows a user to view or set the system's hostname and domain name. When the system is rebooted, it will revert back to the hostname that is configured in its initialization files.
The changes made with the hostname command are not persistent through reboots unless additional configuration is adjusted. This behavior contrasts with the hostnamectl command (on systems using systemd), which affects changes more persistently and syncs them through various aspects of the system.
The basic syntax for the Linux hostname command:
hostname [options] [File] [new_hostname]
Some of the useful options of hostname command.
-s, --short: Displays the short hostname (up to the first
-a, --alias: Displays the alias name of the host (if used).
-i, --ip-address: Shows the IP address of the host.
-I, --all-ip-addresses: Shows all network addresses of the host.
-f, --fqdn, --long: Displays Fully Qualified Domain Name (FQDN).
-d, --domain: Shows the DNS domain name.
-y, --yp, --nis: Displays the NIS/YP domain name.
-F, --file filename: Read the hostname from the given file.
Simply use hostname with no arguments to retrieve the current hostname of the system:
Fetching Domain Name and FQDN
- The -d option displays the DNS domain name of the system.
- Note that it will return nothing if the system is not configured with a domain name.
To get the NIS domain name use -y option:
The message "hostname: Local domain name not set", will return if it's not set.
Fetching Fully Qualified Domain Name (FQDN)
- The -f option is used to display the Fully Qualified Domain Name (FQDN) of the system.
- FQDN is the complete domain name for a specific computer, or host, on the internet. The FQDN consists of two parts: the hostname and the domain name.
Use -A or --all-fqdns option, then hostname command attempts to find all network addresses of the host and translate them into DNS domain names.
Let's look into some more use case examples of hostname command.
1. Set a New Hostname
sudo hostname new-hostname
This will set the system's hostname to new-hostname temporarily.
To ensure that changes to the hostname are permanent make appropriate changes in the configuration file such as /etc/hostname with the desired hostname. If required to resolve the new hostname to an IP address make changes in /etc/hosts file as well.
2. Display IP address
To display the IP address using hostname command use the -i or --ip-address option.
Here the hostname command will try to resolve the host's name to an IP address by checking local configuration files (like /etc/hosts) and DNS resolving, depending on your system configuration.
-I option to display all network addresses of the host.
Note that this does not resolve the hostname to an IP using DNS or the /etc/hosts file, but rather shows all IP addresses assigned to network interfaces on the host.
3. Display short hostname
Using the -s or --short option helps to retrieve just the first part of the FQDN, which is often used in internal network environments and scripting to reference the host without needing to utilize the full domain or DNS structure.
There are alternative commands and methods to manage and retrieve hostname information.
Available on systems with systemd (like many modern Linux distributions)
hostnamectl: Displays the current system's hostname and related settings.
hostnamectl set-hostname newHostname: Replace newHostname with your desired hostname. This command updates the /etc/hostname file as well.
uname -n: Display the hostname of the system.
cat /proc/sys/kernel/hostname: display the current hostname by reading it directly from the kernel's parameter.