hostname Command Examples in Linux

This command can get or set the hostname or the NIS domain name. You can also get the DNS domain or the FQDN (fully qualified domain name). Unless you are using bind or NIS for host lookups you can change the FQDN (Fully Qualified Domain Name) and the DNS domain name (which is part of the FQDN) in the /etc/hosts file.

Usage:

hostname [-b] {hostname|-F file}         set host name (from file)
hostname [-a|-A|-d|-f|-i|-I|-s|-y]       display formatted name
hostname                                 display host name

{yp,nis,}domainname {nisdomain|-F file}  set NIS domain name (from file)
{yp,nis,}domainname                      display NIS domain name

dnsdomainname                            display dns domain name

hostname -V|--version|-h|--help          print info and exit

Example:

# hostname
abc-redhat-host

Set the desired hostname of the system using hostname command.

# hostname xyz.example.com

To keep the hostname persistent across reboots, edit the file /etc/hostname.

# vi /etc/hostname
xyz.example.com

The hostname can be viewed in the /etc/hostname file as well.

# cat /etc/hostname

hostname Command Examples

1. To print the hostname of the system:

# hostname 

2. To display the alias host name of the system:

# hostname -a
# hostname --alias 

3. To display the name of DNS domain:

# hostname -d
# hostname --domain 

4. To read the hostname from the file:

# hostname -F hostfile.txt
# hostname --file hostfile.txt

5. To display the FQDN (Fully Qualified Domain Name):

# hostname -f
# hostname --fqdn
# hostname --long 

6. To display the IP address of the host:

# hostname -i
# hostname --ip-address 

7. To display the short hostname:

# hostname -s
# hostname --short 

8. To get the version of the hostname command:

# hostname -V
# hostname --version 

9. To have the verbose output:

# hostname --verbose 

10. To display the NIS domain name:

# hostname -y
# hostname --yp
# hostname --nis 

11. To get the help for hostname command:

# hostname --help 
Related Post