Skip to content
Related Articles
Open in App
Not now

Related Articles

hostname command in Linux with examples

Improve Article
Save Article
Like Article
  • Last Updated : 21 May, 2019
Improve Article
Save Article
Like Article

hostname command in Linux is used to obtain the DNS(Domain Name System) name and set the system’s hostname or NIS(Network Information System) domain name. A hostname is a name which is given to a computer and it attached to the network. Its main purpose is to uniquely identify over a network.

Syntax :

hostname -[option] [file]

Example: We obtain the system hostname by just typing hostname without any attributes.

hostname gfg

Options:

  • -a : This option is used to get alias name of the host system(if any). It will return an empty line if no alias name is set. This option enumerates all configured addresses on all network interfaces.

    Syntax:

    hostname -a
    

    Example:

    hostname -a gfg

  • -A : This option is used to get all FQDNs(Fully Qualified Domain Name) of the host system. It enumerates all configured addresses on all network interfaces. An output may display same entries repetitively.

    Syntax :

    hostname -A
    

    Example:

    hostname -A gfg

  • -b : Used to always set a hostname. Default name is used if none specified.

    Syntax :

    hostname -b
    

    Example:

    hostname -b gfg

  • -d : This option is used to get the Domain if local domains are set. It will not return anything(not even a blank line) if no local domain is set.

    Syntax :

    hostname -d
    

    Example :

    hostname -d gfg

  • -f : This option is used to get the Fully Qualified Domain Name(FQDN). It contains short hostname and DNS domain name.

    Syntax:

    hostname -f
    

    Example:

    hostname -f

  • -F : This option is used to set the hostname specified in a file. Can be performed by the superuser(root) only.

    Syntax:

    sudo hostname -F filename
    

    Example:

    hostname -F gfg

  • -i option:This option is used to get the IP(network) addresses. This option works only if the hostname is resolvable.

    Syntax:

    hostname -i
    

    Example:

    hostname -i gfg

  • -I : This option is used to get all IP(network) addresses. The option doesn’t depend on resolvability of hostname.
    hostname -I
    

    Example:

    hostname -I gfg

  • -s : This option is used to get the hostname in short. The short hostname is the section of hostname before the first period/dot(.). If the hostname has no period, the full hostname is displayed.

    Syntax :

    hostname -s
    

    Example:

    hostname -s gfg

  • -V : Gives version number as output.

    Syntax:

    hostname -V
    

    Example:

    hostname -V gfg

Note: To set the hostname we can use the command given below:

sudo hostname NEW_HOSTNAME

Here, NEW_HOSTNAME is the new hostname the user wants to give.

Example:

set hostname

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!