hostname command in Linux with Examples


Advertisements

Name

hostname - show or set the system's host name

Synopsis

hostname [-a|--alias] [-d|--domain] [-f|--fqdn|--long] [-A|--all-fqdns] [-i|--ip-address]
[-I|--all-ip-addresses] [-s|--short] [-y|--yp|--nis]
hostname [-b|--boot] [-F|--file filename] [hostname]
hostname [-h|--help] [-V|--version]

Description

Hostname is used to display the system's DNS name, and to display or set its hostname or NIS domain name.

GET NAME: When called without any arguments, the program displays the current names: hostname will print the name of the system as returned by the gethostname(2) function.

SET NAME: When called with one argument or with the --file option, the commands set the host name or the NIS/YP domain name. hostname uses the sethostname(2) function, while all of the three domainname, ypdomainname and nisdomainname use setdomainname(2). Note, that this is effective only until the next reboot. Edit /etc/hostname for permanent change.

Note, that only the super-user can change the names.

It is not possible to set the FQDN or the DNS domain name with the dnsdomainname command (see THE FQDN below).

The host name is usually set once at system startup in /etc/init.d/hostname.sh (normally by reading the contents of a file which contains the host name, e.g. /etc/hostname).

Options

The options for hostname commands are:

-a, --alias
   Display the alias name of the host (if used). This option is deprecated and should not be used anymore.

-A, --all-fqdns
   Displays all FQDNs of the machine. This option enumerates all configured network addresses on all configured network interfaces, and translates them to DNS domain names. Addresses that cannot be translated (i.e. because they do not have an appropriate reverse IP entry) are skipped. Note that different addresses may resolve to the same name, therefore the output may contain duplicate entries. Do not make any assumptions about the order of the output.

-b, --boot
   Always set a hostname; this allows the file specified by -F to be non-existant or empty, in which case the default hostname localhost will be used if none is yet set.

-d, --domain
   Display the name of the DNS domain. Don't use the command domainname to get the DNS domain name because it will show the NIS domain name and not the DNS domain name. Use dnsdomainname instead. See the warnings in section THE FQDN above, and avoid using this option.

-f, --fqdn, --long
   Display the FQDN (Fully Qualified Domain Name). A FQDN consists of a short host name and the DNS domain name. Unless you are using bind or NIS for host lookups you can change the FQDN and the DNS domain name (which is part of the FQDN) in the /etc/hosts file. See the warnings in section THE FQDN above und use hostname --all-fqdns instead wherever possible.

-F, --file filename
   Read the host name from the specified file. Comments (lines starting with a `#') are ignored.

-i, --ip-address
   Display the network address(es) of the host name. Note that this works only if the host name can be resolved. Avoid using this option; use hostname --all-ip- addresses instead.

-I, --all-ip-addresses
   Display all network addresses of the host. This option enumerates  all configured addresses on all network interfaces. The loopback interface and IPv6 link-local addresses are omitted. Contrary to option -i, this option does not depend on name resolution. Do not make any assumptions about the order of the output.

-s, --short
   Display the short host name. This is the host name cut at the first dot.

-V, --version
   Print version information on standard output and exit successfully.

-y, --yp, --nis
   Display the NIS domain name. If a parameter is given (or --file name ) then root can also set a new NIS domain.

-h, --help
   Print a usage message and exit.

Examples

1. hostname command without any option/argument shows the hostname of the machine as returned by the gethostname(2) function.

$ hostname
erpnext

2. When called with one argument or with the --file option, the commands set the host name or the NIS/YP domain name. hostname command uses the sethostname(2) function to set the hostname of the machine. Note, that this change in hostname is effective only until the next reboot. To make the change permamnent we need to edit '/etc/hostname' file. Note, that only the super-user can change the hostnames.

$ sudo hostname expertdev.esc.in
[sudo] password for expert: 
$ hostname
expertdev.esc.in

3. Use -i option to display the ip-adress or network-address assigned to the host. Note that this works only if the host name can be resolved. Avoid using this option; use hostname --all-ip- addresses command instead to show the network-address of the host.

$ hostname -i
127.0.1.1
$
$ hostname --all-ip-address
192.168.0.4 

4. Use -I option to display all network-addresses of the host machine.

$ hostname -I
192.168.0.234 192.168.0.4 

5. Use -s option to display the short host name.

$ hostname -s
erpnext

6. Use -a option to display alias names assigned to the host machine for easy reference.

$ hostname -a
erpnext expert

7. Use -f or --fqdn option to display the long host name or Fully Qualified Domain Name (FQDN) of the host machine.

$ hostname -f
erpnext.esc.in

8. Use -d option to display the name of the DNS domain.

$ hostname -d
esc.in
Advertisements