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)
sudoprivileges (you’ll be prompted for your password)- A text editor installed (nano, vim, or gedit are fine)
Step-by-Step
- Open a terminal. Press Ctrl+Alt+T or search for “Terminal” in the activities menu.
- Back up the original file. Run
sudo cp /etc/hosts /etc/hosts.backup. This gives you a restore point if something goes wrong. - Open the hosts file in a text editor with sudo. For nano, run
sudo nano /etc/hosts. For vim, usesudo vim /etc/hosts. You can also usesudo gedit /etc/hostsfor a GUI editor. - Understand the existing content. The file usually has comments (lines starting with
#) and a few default entries like127.0.0.1 localhost. Don’t delete these unless you know what you’re doing. - Add your custom entries. Each line follows the format:
IP_address hostname [alias...]. For example, to mapexample.localto your local machine, add:127.0.0.1 example.local. To block a site, map it to0.0.0.0or127.0.0.1(e.g.,127.0.0.1 ads.example.com). Separate the IP and hostname with a space or tab. - Save and exit. In nano, press Ctrl+O to save, then Ctrl+X to exit. In vim, press Esc, type
:wq, and press Enter. - Flush the DNS cache. Run
sudo systemd-resolve --flush-caches(orsudo resolvectl flush-cacheson newer systems). This ensures your changes take effect immediately. - Test your changes. Use
ping example.localorgetent hosts example.localto verify the mapping works. For a blocked site,ping ads.example.comshould 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 localhostcan break system services. - Using an IP that doesn’t exist. For local testing, use
127.0.0.1or::1for IPv6. - Not flushing DNS. Changes may not appear immediately without flushing the cache.
Tips
- Use comments to organize. Add lines like
# Development sitesbefore a group of entries. - Test with
getent. The commandgetent hosts yourhostnameshows which IP the system resolves to, ignoring your browser’s cache. - For blocking, use
0.0.0.0. This often works better than127.0.0.1because some browsers treat 127.0.0.1 as a local server. - Keep a backup. Always have the
.backupfile 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.localrather than justmyapp.
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’schrome://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/hoststo 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.