hostname command explained

Command: hostname explained

I have seen a lot of people telling people to use the command hostname to get FQDN but almost none that have explained to junior people what the command actually does more than hostname -f. So I decided to start explaining a lot of the Linux commands that are used daily if not weekly as a Linux System Administrator. So enjoy the first of many commands I will explain in detail here at geekcorner.sitedevelopments.net.

This command can read or set the hostname or the NIS domain name. You can
also read 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:

-i, –ip-address      addresses for the hostname
-I, –all-ip-addresses all addresses for the host

Check ip Address for the hostname

hostname i
hostname -i
127.0.0.1

Check all IP Addresses for the hostname

-I, –all-ip-addresses all addresses for the host

hostname -I

As you see below the command do not show the loopback IP address only sharp ip adresses.,

hostname -I
10.101.1.152

Check the aliases for hostname

-a, –alias           alias names

hostname -a

$ hostname -a
sesstl168 localhost.localdomain localhost

Check the short host name

-s, –short           short host name

hostname -s

hostname -s
sesstl168

Check the Fully Qualified Domain Name (FQDN)

-f, –fqdn, –long    long host name (FQDN)

hostname -f

hostname -f
sesstl168.sitedevelopments.local

Check all FDQDN

-A, –all-fqdns        all long host names (FQDNs)

hostname -A

hostname -A
sesstl168.sitedevelopments.net

As you see here the command hostname -A will only show the FQDN that have a real IP address it will not show loopback FQDN

Set hostname from a file

hostname [-v] {hostname|-F file}

Command Explanation:

hostname [-v]     display hostname
-F, –file            read hostname or NIS domainname from given file

In the example below I load the hostname newhostname from the file myhostname that are located in my home folder.

As you see the file contains only the hostname

 cat /home/johan/myhostname
newhostname
sudo hostname -v hostname -F /home/johan/mydomainname
[sudo] password for johan: 
>> newhostname
Setting hostname to `newhostname'

To check the result of the command we just typed type: hostname -v it will display the name.

Below is the output of hostname -v after I ran the command above.

$ hostname -v
gethostname()=`newhostname'
newhostname

hostname [-v] [-d|-f|-s|-a|-i|-y|-A|-I]  display formatted name

hostname [-v]                         display hostname

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

hostname -Vh
[c-johsor@sesstl168 ~]$ hostname -Vh
net-tools 1.60
hostname 1.100 (2001-04-14)
[c-johsor@sesstl168 ~]$

Finaly you can use hostname -h to get help

hostname -h
Usage: hostname [-v] {hostname|-F file}      set hostname (from file)
       domainname [-v] {nisdomain|-F file}   set NIS domainname (from file)
       hostname [-v] [-d|-f|-s|-a|-i|-y|-A|-I]  display formatted name
       hostname [-v]                         display hostname

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

    dnsdomainname=hostname -d, {yp,nis,}domainname=hostname -y

    -s, --short           short host name
    -a, --alias           alias names
    -i, --ip-address      addresses for the hostname
    -I, --all-ip-addresses all addresses for the host
    -f, --fqdn, --long    long host name (FQDN)
    -A, --all-fqdns        all long host names (FQDNs)
    -d, --domain          DNS domain name
    -y, --yp, --nis       NIS/YP domainname
    -F, --file            read hostname or NIS domainname from given file

   This command can read or set the hostname or the NIS domainname. You can
   also read 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.

That’s all folk.

Do you like to check out other commands that I’m explaining check out the below post:

I am a Senior System Administrator that work daily with Virtualization,Xenserver, Linux, IT automation, Ansible,Windows, Active Directory, Exchange and a lot more. Here you will find tips and trix from early junior, middle junior and senior to unique information of the topics I mentioned earlier. If you need help with something do not hesitate to contact me I will gladly help.

Tagged with: , , , , , ,