You are on page 1of 16

hostname command in Linux with examples

 Read
 Discuss
 Courses



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 given to a computer and attached to the network. Its main purpose is to uniquely identify
over a network.

Syntax of the `hostname` command in Linux


hostname -[option] [file]

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

Options Description Syntax


This option is used to get the alias name of the host system (if
any). It will return an empty line if no alias name is set. This hostname -a
-a
option enumerates all configured addresses on all network
interfaces.
This option is used to get all FQDNs (Fully Qualified Domain
Name) of the host system. It enumerates all configured addresses hostname -A
-A
on all network interfaces. An output may display the same
entries repetitively.
Used to always set a hostname. Default name is used if none hostname -b
-b
specified.
-d This option is used to get the Domain if local domains are set. It hostname -d
will not return anything (not even a blank line) if no local
Options Description Syntax
domain is set.
This option is used to get the Fully Qualified Domain Name hostname -f
-f
(FQDN). It contains short hostname and DNS domain name.
sudo
This option is used to set the hostname specified in a file. Can be hostname -F
-F
performed by the superuser(root) only. filename
This option is used to get the IP (network) addresses. This option hostname
-i -i
works only if the hostname is resolvable.
This option is used to get all IP(network) addresses. The option hostname
-I -I
doesn’t depend on resolvability of hostname.
This option is used to get the hostname in short. The short
hostname is the section of hostname before the first hostname
-s -s
period/dot(.). If the hostname has no period, the full hostname is
displayed.
-V Gives version number as output. hostname -V

Outputs of Options available in `hostname` command


1) `-a` Option in `hostname` command in Linux

Display all aliases of the host.

Syntax:

hostname -a
hostname -a

2) `-A` Option in `hostname` command in Linux

Syntax:

hostname -A
hostname -A

3) `-b` Option in `hostname` command in Linux

Syntax:

hostname -b
hostname -b

4) `-d` Option in `hostname` command in Linux

Display the domain name of the host if any.

Syntax:

hostname -d
hostname -d

5) `-f` Option in `hostname` command in Linux

Display the fully qualified domain name (FQDN) of the host.

Syntax:

hostname -f
hostname -f

6) `-F` Option in `hostname` command in Linux

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


sudo hostname -F filename

7) `-i` Option in `hostname` command in Linux

Display the IP address of the host

Syntax:

hostname -i
hostname -i

8) `-I` Option in `hostname` command in Linux

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

hostname -I
hostname -I

9) `-s` Option in `hostname` command in Linux

Display the short hostname of the host.

Syntax:

hostname -s
hostname -s

10) `-V` Option in `hostname` command in Linux

Display the version information about the hostname

Syntax:

hostname -V
hostname -V

11) How to set hostname in Linux

Set the hostname of the system temporarily.

Syntax:

sudo hostname NEW_HOSTNAME


sudo hostname geeksforgeeks
Replace NEW_HOSTNAME with the new hostname you want to set. (Here we have changed it
to “geeksforgeeks”)

Set the hostname of the system permanently.

To set the hostame of our system permanently we have to go inside “/etc/hostname” using text
editor and change hostname to the hostname we want.

here we can change hostname permanently.


Conclusion
This command `hostname` in Linux seems so simple but at the same time it is a very powerful
command that allows users to view or set the hostname of the system. It is important for users to
know the `hostname` command to manage their Linux system effectively. One can easily
understand by this article, as we have discussed many options and also their output, options like
`-a`, `-A`, `-b`, `-d`, `-f`, `-F`, `-i`, `-I`, `-s` and `-V`.

You might also like