Go to main content

man pages section 5: File Formats

Exit Print View

Updated: Wednesday, February 9, 2022
 
 

hosts(5)

Name

hosts - host name database

Synopsis

/etc/inet/hosts
/etc/hosts
/etc/inet/ipnodes

Description

The hosts file is a local database that associates the names of hosts with their Internet Protocol (IP) addresses. An IP address can be in either IPv4 or IPv6 format. The hosts file can be used in conjunction with, or instead of, other hosts databases, including the Domain Name System (DNS), the NIS hosts map, or information from an LDAP server. Programs use library interfaces to access information in the hosts file.

Note that /etc/hosts and /etc/inet/ipnodes are symbolic links to /etc/inet/hosts.

The hosts file has one entry for each IP address of each host. If a host has more than one IP address, it will have one entry for each, on consecutive lines. The format of each line is:

IP-address official-host-name nicknames . . .

Items are separated by any number of SPACE and/or TAB characters. The first item on a line is the host's IP address. The second entry is the host's official name. Subsequent entries on the same line are alternative names for the same machine, or “nicknames.” Nicknames are optional.

For a host with more than one IP address, consecutive entries for these addresses may contain the same or differing nicknames. Different nicknames are useful for assigning distinct names to different addresses.

A call to gethostbyname(3C) returns a hostent structure containing the union of all IPv4 addresses and nicknames from each line containing a matching official name or nickname. A call to getipnodebyname(3C) is similar, but is capable of returning hostent structures containing IPv4 and IPv6 addresses. Applications might prefer to use the address-family independent getaddrinfo(3C) API for name-to-address lookups.

A ‘#’ indicates the beginning of a comment; characters up to the end of the line are not interpreted by routines that search the file.

Network addresses are written in one of two ways:

  • The conventional IPv4 “decimal dot” notation and interpreted using the inet_addr(3C) routine.

  • The IPv6 notation, defined in RFC 1884 and interpreted using the inet_pton(3C) routine.

This interface supports node names as defined in Internet RFC 952, which states:

A “name” (Net, Host, or Domain name) is a text string up to 24 characters drawn from the alphabet (A-Z), digits (0-9), minus sign (−), and period (.). Note that periods are only allowed when they serve to delimit components of “domain style names”. (See RFC 921, “Domain Name System Implementation Schedule,” for background). No blank or space characters are permitted as part of a name. No distinction is made between uppercase and lowercase. The first character must be an alpha character or a digit. [RFC 1123 relaxed RFC 952's limitation of the first character to only alpha characters.] The last character must not be a minus sign or period.

Host names must not consist of numbers only. A host name must contain at least one alphabetical or special character.

Single character names or nicknames are not allowed.

Examples

Example 1 Example IPv4 hosts File Entry

The following is an example of an IPv4 entry from the hosts file:

192.0.2.20    gaia.example.com    gaia     # John Smith
Example 2 Example IPv6 Address Entry

The following is an example of an IPv6 hosts entry:

2001:db8:3c4d:55:a00:20ff:fe8e:f3ad   myhost.example.com   myhost   # John Smith
Example 3 Server with multiple interfaces

The following example shows multiple interfaces with the same hostname:

192.0.2.1       server.example.com server1int1
198.51.100.1    server.example.com server1int2

See Also

getaddrinfo(3C), gethostbyname(3C), getipnodebyname(3C), inet(3C), nsswitch.conf(5), resolv.conf(5)

Braden, B., editor, RFC 1123, Requirements for Internet Hosts - Application and Support, Network Working Group, October, 1989. https://tools.ietf.org/html/rfc1123

Harrenstien, K., Stahl, M., and Feinler, E., RFC 952, DOD Internet Host Table Specification, Network Working Group, October 1985. https://tools.ietf.org/html/rfc952

Hinden, R., and Deering, S., editors, RFC 1884, IP Version 6 Addressing Architecture, Network Working Group, December, 1995. https://tools.ietf.org/html/rfc1884

Postel, Jon, RFC 921, Domain Name System Implementation Schedule (Revised), Network Working Group, October 1984. https://tools.ietf.org/html/rfc921

Notes

/etc/inet/hosts is the official SVR4 name of the hosts file. The symbolic link /etc/hosts exists for BSD compatibility.

The symbolic link /etc/net/ipnodes exists for backward compatibility with previous Solaris releases.