The Format
127.0.0.1 localhost
127.0.1.1 server1.example.com server1
203.0.113.10 www.example.com example.com
10.0.0.5 db1
# IPv6
::1 localhost ip6-localhost ip6-loopbackEach line holds an IP address, then one or more names separated by spaces or tabs. The first name is the canonical name; the rest are aliases. Text after # is a comment. The file has no wildcards and no ports: *.example.com and example.com:8080 do not work. The loopback lines are explained in what is localhost.
Edit It
sudo cp -a /etc/hosts /etc/hosts.bak
sudo nano /etc/hostsAdd your line, save, and the change applies to new lookups. Programs that cache names themselves, such as a long-running application, may need a restart. Do not remove the localhost lines; several services expect them.
Test the Mapping
getent hosts www.example.com
ping -c 1 www.example.comgetent hosts resolves a name the way applications do, so it reads the hosts file. dig and nslookup ask DNS directly and ignore /etc/hosts; they will still show the DNS answer, which is a common source of confusion.
Which Comes First: Hosts File or DNS
grep hosts /etc/nsswitch.confThe hosts: line in /etc/nsswitch.conf sets the order of sources. When files comes before dns, as on standard server installs, the hosts file wins. Other entries, such as myhostname or resolve, may appear on the same line. If an entry in the hosts file seems to be ignored, check this line first.
Test a Site Before Changing DNS
When you move a site to a new server, you can check it there before you change the DNS record:
- On your own computer, add
203.0.113.10 example.com www.example.comto its hosts file, using the new server's address. On Linux and macOS the file is/etc/hosts. - Restart the browser, which keeps its own DNS cache, and open the site. You see the new server; everyone else still sees the old one.
- Remove the line when you are done, or you will keep seeing the new server after the DNS change, even if something goes wrong there.
For a single request, curl can do the same without editing any file: curl --resolve example.com:443:203.0.113.10 https://example.com.
Name Private Addresses
On a few servers connected by a private network, lines such as 10.0.0.5 db1 let applications use short names instead of addresses. For more than a handful of machines, a DNS zone is easier to keep correct, because every hosts file must be updated by hand.
The 127.0.1.1 Line
Debian and Ubuntu map the server's own host name to 127.0.1.1, so the name resolves even without network or DNS. Keep that line in step with the host name: if you rename the server, update it too, or sudo may print a warning that it cannot resolve the host. See how to change the hostname in Ubuntu.
When cloud-init Rewrites the File
On cloud images, cloud-init regenerates /etc/hosts at every boot from a template when manage_ is set to true. The file then usually starts with a comment saying so, and your edits are lost at the next reboot. Either put your lines in the template under /etc/cloud/templates/, or set manage_ in a file in /etc/cloud/cloud.cfg.d/. The value localhost only keeps the 127.0.1.1 line up to date and leaves your other lines alone. More in what is cloud-init.
What the Hosts File Affects
Only this machine. Visitors, other servers, and mail servers never read it. To change where a name points for everyone, change its DNS record; then caches may need time, as described in how to flush the DNS cache in Linux.
Updated:
Linux VPS with Full Root Access
MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.
More Guides
No Limits on Service or Support
You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

- 100% customer satisfaction goal
- Our own hardware
- 24/7 ticket and email support
- 4 countries with locations