The Linux hostname command is a utility used to display or set the system’s hostname. A hostname is a unique identifier assigned to a device on a network, functioning much like a person’s name in a social context. It allows the device to be identified by other devices on the same network, facilitating communication and data exchange. By using the hostname command, users can view the current hostname of their machine or assign a new one, thereby ensuring proper network configuration and management. This command is essential for network administration and troubleshooting, as it helps in organizing and accessing devices in a networked environment efficiently.
If you are into network administration, and your work involves working on the Linux command line, you should be aware of several network-related command line tools. One such tool is the hostname command, which you can use to display or set the system’s hostname. In this tutorial, we will learn more about this utility using some easy-to-understand examples.
Please note that all examples here have been tested on Ubuntu 24.04 machine.
Linux hostname command
You can use hostname to show or set the system’s host name. The basic syntax of the tool is:
hostname [options]
Here’s how the man page documentation says about this utility:
Hostname is used to display the system's DNS name, and to display or set its hostname or NIS domain
name.
Following are some Q&A-styled examples that will give you a good idea on how this command works.
Q1. How hostname works?
By default, the hostname command produces the name of the host in output.
hostname
Q2. How to print the host’s alias name?
If there’s an alias name associated with the host, you can access that using the -a command line option.
hostname -a
Q3. How to print the IP address?
This can be done using the -i command line option.
hostname -i
Here’s the output of the command on my local machine:
Q4. How to print the associated domain name (if any)?
You can use the -d command line option for this.
hostname -d
The above command will fetch you the domain name where the system resides.
Q5. How to print short hostname?
This can be done using the -s command line option.
hostname -s
Q6. How to change hostname?
The hostname command lets you change the name of the host as well. You can do this in the following way:
hostname [new-host-name]
For example:
hostname howtoforge
You’ll need root privileges to carry out this operation. Also, changes made this way are temporary – for permanent changes, you’ll have to make modifications in the /etc/hosts file.
Conclusion
You can do a lot more with hostname, although what we’ve discussed here should be enough to at-least get you started. Once you are done with all this, head to the tool’s man page to get more info on it.