Linux Command Reference ยท Special Files and Directories
The /etc/hosts file
Reference for /etc/hosts: the line format, IPv4 and IPv6 entries, aliases, the order of name lookup, and common uses such as local development names.
/etc/hosts is a plain-text table that maps host names to IP addresses on the local machine. It is consulted before DNS on most systems (the order is set in /etc/nsswitch.conf), so an entry here wins over what the network would answer. That makes it the simplest way to name a few machines, to point a development domain at 127.0.0.1, or to block a site by sending it to 0.0.0.0.
Each non-comment line holds an address, a canonical name and any number of aliases, separated by spaces or tabs. There is no daemon to restart: programs read the file when they resolve a name.
IP_address canonical_name [alias ...]
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
| Part | Description |
|---|---|
| address | An IPv4 or IPv6 address. |
| canonical name | The official name for the address; returned by reverse lookups. |
| aliases | Optional further names for the same address, separated by whitespace. |
# |
Starts a comment that runs to the end of the line. |
Edge cases: the same name on several lines gives several addresses, and the first matching line is used by simple lookups. Names are case-insensitive. Wildcards such as *.test are not supported; use a local DNS server such as dnsmasq for that. Only editing the file needs root; reading is open to all.
Usage notes
- Order of lookup:
/etc/nsswitch.confhas a line likehosts: files dns.filesmeans this file. Tools likedigandnslookupskip it and ask DNS directly, so usegetent hosts NAMEorpingto test what programs will really see. Seehost. - Loopback names:
127.0.0.1 localhostand::1 localhostshould always exist. Debian-family systems often add127.0.1.1 myhostname. - Containers: Docker writes the container's own name and address into this file at start, which is why it differs between runs.
- Blocking:
0.0.0.0 ads.example.commakes connections fail immediately instead of waiting for a timeout. - Port numbers are not allowed. The file maps names to addresses only.
- Stale entries cause confusion: a forgotten line can send traffic to an old server long after DNS was updated. Check it first when "the website works for everyone but me".
- Windows and macOS have the same idea (
C:\Windows\System32\drivers\etc\hosts,/etc/hosts).