Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Linux & Mac & Bash Command Dictionary

  1. Home
  2. Linux & Mac & Bash Command Dictionary
  3. /etc/hosts (Manual Host Resolution)

/etc/hosts (Manual Host Resolution)

/etc/hosts is a text file that maps hostnames to IP addresses directly, before querying a DNS server. It is widely used as a convenient shortcut when you want to use domain names in a local development or test environment without registering them in DNS. The lookup priority — "check file first, then query DNS" — is controlled by the /etc/nsswitch.conf configuration.

Syntax

# -----------------------------------------------
#  /etc/hosts format
# -----------------------------------------------

# {IP address} {hostname} [{alias} ...]
#   → Fields are separated by spaces or tabs
#   → One IP address per line
#   → Multiple hostnames (aliases) can be added to the same IP
#   → Everything after # is a comment
#   Example: 192.168.1.10  goku.local  goku

# -----------------------------------------------
#  Loopback addresses (included by default)
# -----------------------------------------------

# 127.0.0.1   localhost
#   → IPv4 loopback address
#   → Refers to the local machine itself

# ::1         localhost
#   → IPv6 loopback address

# -----------------------------------------------
#  hosts entry in /etc/nsswitch.conf
# -----------------------------------------------

# hosts: files dns
#   → "files" comes first, so /etc/hosts takes priority over DNS
#   → "dns" uses the DNS server specified in /etc/resolv.conf
#   Example: hosts: files mdns4_minimal [NOTFOUND=return] dns
#         → Configuration example when also consulting mDNS (Avahi)

# -----------------------------------------------
#  Note: Wildcards are not supported
# -----------------------------------------------

# Wildcard entries such as *.local are not valid in /etc/hosts
# Each hostname must be listed on its own line

Syntax Reference

ItemDescription
IP addressAn IPv4 or IPv6 address written at the start of each line. This is the destination that the hostname resolves to.
Hostname (FQDN)Written after the IP address. It is common to specify a fully qualified domain name such as goku.local.
Alias (short name)Additional names added after the hostname, separated by spaces. Used to assign multiple names to the same IP address.
CommentEverything after # is treated as a comment and ignored. Comments can also appear at the end of a line.
127.0.0.1 loopbackThe IPv4 loopback address. Assigned to localhost and included in the file by default.
::1 loopbackThe IPv6 loopback address. Assigned to localhost and included in the file by default.
No wildcardsWildcard entries such as *.local are not supported. Each hostname you want to match must be listed on its own line.
Priority in /etc/nsswitch.confWith the setting hosts: files dns, /etc/hosts (files) is consulted before DNS. Changing this order may cause /etc/hosts to be bypassed.
Required permissionsRoot privileges are required. Edit the file with sudo vi /etc/hosts or sudo nano /etc/hosts.
Immediate effectChanges take effect as soon as the file is saved. No OS restart or service restart is required. However, applications that cache DNS lookups may need to be restarted.

Example

/etc/hosts
# -----------------------------------------------
#  /etc/hosts example for a local development environment
# -----------------------------------------------

# Loopback (default settings)
127.0.0.1   localhost
::1         localhost

# -----------------------------------------------
#  Service hosts for the Dragon Ball development team
# -----------------------------------------------

# Goku's frontend development server
127.0.0.1   goku.local

# Vegeta's API server (with alias)
127.0.0.1   vegeta.local   vegeta-api.local

# Piccolo's database admin panel
127.0.0.1   piccolo.local

# Krillin's staging environment (tested with a production-like setup)
192.168.1.20   krillin-staging.local   staging.local

# Trunks's future environment test server
192.168.1.30   trunks-future.local

# -----------------------------------------------
#  Blocking (redirect ads and trackers to loopback)
# -----------------------------------------------

# To block access to a specific domain, point it to 0.0.0.0 or 127.0.0.1
0.0.0.0   ads.example.com

Run the following command:

$ ping -c 2 goku.local
PING goku.local (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.041 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.045 ms

--- goku.local ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.041/0.043/0.045/0.002 ms
$ curl -I http://vegeta-api.local/
HTTP/1.1 200 OK
Server: nginx/1.24.0
Content-Type: application/json
$ getent hosts krillin-staging.local
192.168.1.20    krillin-staging.local

Overview

/etc/hosts is the most fundamental name resolution mechanism that Linux reads at startup. It is used in a variety of situations — such as environments without a DNS server, environments without network access, or verifying a configuration before registering a domain in production DNS — to map domain names directly to IP addresses. When /etc/nsswitch.conf contains hosts: files dns, the entries in /etc/hosts take priority over DNS, so you can point an existing domain to a different IP locally. Note that wildcards are not supported, so if you need multiple subdomains, each one must be listed on its own line. For configuring network interface IP addresses, see network_interface. For file-based management of user authentication information, see passwd / shadow.

If you find any errors or copyright issues, please .