ThisCalculator

Everyday

How to Edit the /etc/hosts File on Ubuntu

Updated · Standard formula, shown below

How to Edit the /etc/hosts File on Ubuntu

The /etc/hosts file on Ubuntu maps hostnames to IP addresses locally, before any DNS lookup. Editing it lets you override DNS, block sites, or set up local development domains. This guide shows you how to safely edit it with a text editor, apply changes, and fix common issues.

What You’ll Need

  • Ubuntu (any recent version, e.g., 20.04, 22.04, 24.04)
  • Access to a terminal (Ctrl+Alt+T)
  • sudo privileges (you’ll be prompted for your password)
  • A text editor installed (nano, vim, or gedit are fine)

Step-by-Step

  1. Open a terminal. Press Ctrl+Alt+T or search for “Terminal” in the activities menu.
  2. Back up the original file. Run sudo cp /etc/hosts /etc/hosts.backup. This gives you a restore point if something goes wrong.
  3. Open the hosts file in a text editor with sudo. For nano, run sudo nano /etc/hosts. For vim, use sudo vim /etc/hosts. You can also use sudo gedit /etc/hosts for a GUI editor.
  4. Understand the existing content. The file usually has comments (lines starting with #) and a few default entries like 127.0.0.1 localhost. Don’t delete these unless you know what you’re doing.
  5. Add your custom entries. Each line follows the format: IP_address hostname [alias...]. For example, to map example.local to your local machine, add: 127.0.0.1 example.local. To block a site, map it to 0.0.0.0 or 127.0.0.1 (e.g., 127.0.0.1 ads.example.com). Separate the IP and hostname with a space or tab.
  6. Save and exit. In nano, press Ctrl+O to save, then Ctrl+X to exit. In vim, press Esc, type :wq, and press Enter.
  7. Flush the DNS cache. Run sudo systemd-resolve --flush-caches (or sudo resolvectl flush-caches on newer systems). This ensures your changes take effect immediately.
  8. Test your changes. Use ping example.local or getent hosts example.local to verify the mapping works. For a blocked site, ping ads.example.com should fail or resolve to 127.0.0.1.

Common Mistakes

  • Forgetting sudo. Editing /etc/hosts without sudo will give a permission denied error.
  • Using spaces instead of tabs. Spaces are fine, but tabs are conventional. Both work.
  • Adding entries with a trailing period. Don’t add a dot at the end of hostnames (e.g., example.com. is wrong).
  • Deleting the localhost line. Removing 127.0.0.1 localhost can break system services.
  • Using an IP that doesn’t exist. For local testing, use 127.0.0.1 or ::1 for IPv6.
  • Not flushing DNS. Changes may not appear immediately without flushing the cache.

Tips

  • Use comments to organize. Add lines like # Development sites before a group of entries.
  • Test with getent. The command getent hosts yourhostname shows which IP the system resolves to, ignoring your browser’s cache.
  • For blocking, use 0.0.0.0. This often works better than 127.0.0.1 because some browsers treat 127.0.0.1 as a local server.
  • Keep a backup. Always have the .backup file until you’re sure the changes are correct.
  • Use FQDN for web servers. If you’re setting up a local web server, use the full domain like myapp.local rather than just myapp.

Troubleshooting

  • Changes not taking effect. Flush the DNS cache again with sudo resolvectl flush-caches. Also check if your browser has its own DNS cache (e.g., Chrome’s chrome://net-internals/#dns). Restart the browser if needed.
  • Permission denied. Make sure you’re using sudo. If you’re in a container or restricted environment, you may need root access.
  • Hostname resolves to wrong IP. Double-check the IP and hostname spelling. Use cat /etc/hosts to view the file. Also check if there are duplicate entries.
  • System services break after editing. If you removed the localhost lines, restore the backup: sudo cp /etc/hosts.backup /etc/hosts.
  • IPv6 issues. If you’re using IPv6, add entries like ::1 example.local. Some applications prefer IPv6.
  • Still not working after flush. Reboot the system. This clears all caches and is a last resort.

Frequently Asked Questions

Do I need to restart networking after editing /etc/hosts?

No, the file is read on demand. Flushing the DNS cache is enough. A reboot is only needed if you have persistent issues.

Can I use wildcards in /etc/hosts?

No, wildcards like *.example.com are not supported. You must list each hostname individually.

What is the difference between /etc/hosts and DNS?

/etc/hosts is a local, static file that takes precedence over DNS. DNS is a distributed network service. The hosts file is checked first.

Is it safe to block ads with /etc/hosts?

Yes, it’s a common method. However, it can break some sites that rely on ad networks. You can always remove the entries.

How do I add an IPv6 entry?

Use the IPv6 address, e.g., ::1 example.local for localhost, or a real IPv6 address like 2001:db8::1.

Can I edit /etc/hosts on Windows or Mac the same way?

The file path is different (Windows: C:\Windows\System32\drivers\etc\hosts, Mac: /etc/hosts), but the format is similar. On Windows you need to run the editor as Administrator.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.