Linux hostname Command Tutorial for Beginners (5 Examples)
If you are into network administration, and your work involves working on the Linux command line, there are several network-related command line tools that you should be aware of. On such tool is hostname, which you can use to display or set system’s host name. 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 16.04 machine and cross tested on an Ubuntu server machine running 14.04 LTS version.
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
Needless to say, you’ll need root privileges to carry out this operation. Also, change done through this way is temporary – for permanent change, 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.
Share this page: