The hostname Command

The hostname command is used to show or set a computer's host name and domain name. It is one of the most basic of the network administrative utilities.

A host name is a name that is assigned to a host (i.e., a computer connected to the network) that uniquely identifies it on a network and thus allows it to be addressed without using its full IP address. Domain names are user-friendly substitutes for numeric IP addresses.

The basic syntax for the hostname command is

hostname [options] [new_host_name]

When used without any options or arguments (i.e., input data), hostname displays the current host and domain names of the local machine (i.e., the computer that is currently being used). The default host and domain name is typically localhost.localdomain.

When a name is provided as an argument, it becomes the new host name. A change in the host name can only be performed by the root (i.e., administrative) account, which can be accessed using the su (i.e., substitute user) command. For example, to change the host name to computer_01, the following would be used:

hostname computer_01

The -F option changes the host and domain names to that written in a plain text file whose name is provided after this option as an argument. Comments (i.e., lines starting with a pound sign) in the file are ignored. Thus, for example, if a file named name_1 were created that contained a line that consisted of morgan.localdomain, then following would change the hostname to morgan.localdomain:

hostname -F name_1

The -s option displays the short host name, which is the part of the name to the left of the first dot (e.g., localhost or morgan in the above examples) i.e.,

hostname -s

Likewise, the -d option displays only the domain name, i.e., the section to the right of the final dot (localdomain in the above examples). The -a option displays any aliases (i.e., substitute names) that are used for the host name. The -i option displays the IP address(es) of the host, which by default is

The -v (verbose) option provides additional information. The -V (upper case V) option displays version information, and the -h option displays a brief help message.

Created December 20, 2005.
Copyright © 2005 The Linux Information Project. All Rights Reserved.