The general syntax of the “hostname” command in Linux is as below:
$ hostname [parameter]
Where,
- parameter: any flags or options used for the hostname.
Some of those flags or options are also described in the examples below.
For learning the usage of the “hostname” command in Linux, you can go through the following examples:
# 1: Check the Hostname of your Linux System:
The command most widely used for checking the hostname of your Linux system is shown below:
$ hostname
Output:
rootadminz: $ hostname server
rootadminz: $ hostname
server
# 2: Check the IP Address against the Hostname of your Linux System:
You can also check the IP address that is assigned to your Linux machine against its hostname by using the “hostname” command with the “-i” parameter as follows:
$ hostname –i
Output:
rootadminz: $ hostname -i
153.147.12.3
rootadminz:
# 3: Check the Fully Qualified Domain Name of your Linux System:
If you want to check the fully qualified domain name of your Linux system, then you can use the “-f” flag with the “hostname” command as follows:
$ hostname –f
Output:
rootadminz: $ hostname -f
rootadminz
rootadminz:
# 4: Check the Alias of the Hostname of your Linux System:
At times, users also set aliases against the hostnames of their systems. If you want to check the alias against the hostname of your system (if it exists), then you can use the “-a” parameter with the “hostname” command as follows:
$ hostname –a
# 5: Check the Short Hostname of your Linux System:
If there is a dot in your hostname, then the short hostname refers to the portion of your hostname before that dot. You can display the short hostname of your Linux system by using the “-s” flag with the “hostname” command as follows:
$ hostname –s
Example # 6: Change the Hostname of your Linux System:
If you want to change the hostname of your Linux system, then you can also do that while using the “hostname” command in the following manner:
$ sudo hostname NewHostName
Output:
rootadminz: $ sudo hostname jarvis
rootadminz: $ hostname
jarvis
rootadminz:
Done!!