Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
IP_address  canonical_name  [alias ...]

Try it

Bash Reading the hosts file and resolving names

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.

Bash Looking up a name in a hosts-style table

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.conf has a line like hosts: files dns. files means this file. Tools like dig and nslookup skip it and ask DNS directly, so use getent hosts NAME or ping to test what programs will really see. See host.
  • Loopback names: 127.0.0.1 localhost and ::1 localhost should always exist. Debian-family systems often add 127.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.com makes 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).