3 UNIX / Linux hostname Command Examples

What is hostname?
3 hostname examples
Syntax and Options
Related Commands

What is hostname?

You can view your system name (hostname) or assign a new name to your system using hostname command.

3 hostname Examples

1. View your hostname

hostname command without any argument will return the name of your system.

# hostname
ramesh-laptop

2. Change hostname from command line

To change the system name, do the following. Please note that this is only temporary. When you reboot the system, this hostname will not be used.

# hostname ramesh-system

# hostname
ramesh-system

To change the hostname permanently, modify the /etc/hostname file.

# vi /etc/hostname
ramesh-system

3. Change hostname (get name from a file)

Use option -F to specify a filename. The name of the host will be picked-up from this filename and set appropriately.

# cat /home/root/hostname.txt
ramesh-desktop

# hostname -F /home/root/hostname.txt 

# hostname
ramesh-desktop

Syntax and Options

Syntax:

hostname  [-v]  [-a]  [--alias]  [-d]  [--domain]  [-f]  [--fqdn]  [-A]  [--all-fqdns] [-i] [--ip-address] [-I] [--all-ip-addresses] [--long] [-s] [--short] [-y] [--yp] [--nis]

hostname [-v] [-b] [--boot] [-F filename] [--file filename] [hostname]

hostname [-v] [-h] [--help] [-V] [--version]
Short Option Long Option Option Description
-a –alias Display the alias name of the host. This is deprecated.
-b –boot Always set a hostname
-d –domain Display DNS domain name. It is recommended to use dnsdomainname command for this.
-F –file Read the host name from a file. Lines that starts with # are comments.
-f –fqdn, –long Display Fully Qualified Domain Name. i.e FQDN, which is combination 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 in the /etc/hosts file.
-A –all-fqdns Displays all FQDNs. This option enumerates all network addresses of all configured network interfaces, and translates them to DNS domain names.
-h –help Print help message and exit.
-i –ip-address Displays network ip address of the host. This depends on name resolution. So, use -I option instead.
-I –all-ip-addresses Displays all network ip addresses of the host. Displays all configured addresses on all network interfaces. loopback and IPv6 link-local addresses are not displayed. This doesn’t depend on name resolution.
-s –short Displays short host name. This displays the hostname until the first dot.
-V –version Print version information on standard output and exit successfully.
-v –verbose Be verbose
-y –yp, –nis Displays NIS domain name. You can also set a new NIS domain using this option.


Related Commands

domainname
ypdomainname
nisdomainname
dnsdomainname