Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Set Up Linux Hosts File (Step-by-Step)

· 4 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

The Linux hosts file maps hostnames to IP addresses before DNS is consulted. Editing it lets you block websites, redirect domains to local servers, or speed up local network resolution. You'll need root/sudo access and a text editor (nano or vim are common).

What You'll Need

  • A Linux machine with sudo privileges
  • A terminal window
  • A text editor (nano is easiest for beginners; vim is also fine)
  • Administrator password for sudo

Step-by-Step

  1. Open a terminal. Press Ctrl+Alt+T on most desktop environments, or search for “Terminal” in your app menu.
  2. Back up the current hosts file. Run sudo cp /etc/hosts /etc/hosts.backup. This creates a restore point in case you make a mistake.
  3. Open the hosts file in a text editor. Use sudo nano /etc/hosts (or replace nano with vim if you prefer). The sudo is required because the file is owned by root.
  4. Understand the file structure. The file contains lines with an IP address, whitespace, then one or more hostnames. Lines starting with # are comments and are ignored. The default file usually has entries for localhost and your machine's hostname.
  5. Add a new mapping. Move to a new line (press Enter at the end of the last line). Type the IP address, press Tab or Space, then type the hostname. For example: 192.168.1.10 myserver.local. You can add multiple hostnames on the same line separated by spaces: 192.168.1.10 myserver.local backup.local.
  6. To block a website, map it to 127.0.0.1 or 0.0.0.0. For example, 127.0.0.1 example.com will send requests for example.com to your own machine, effectively blocking it. Use 0.0.0.0 if you want a faster fail (some browsers handle it better).
  7. Save and exit. In nano, press Ctrl+O to save, then Enter to confirm the filename, then Ctrl+X to exit. In vim, press Esc, type :wq, then Enter.
  8. Flush the DNS cache (if needed). Most modern Linux systems use systemd-resolved. Run sudo systemd-resolve --flush-caches (older systems may use sudo service network-manager restart or sudo /etc/init.d/networking restart). If you're not sure, a simple reboot also works.
  9. Test your changes. Use ping myserver.local or getent hosts myserver.local to see if the IP resolves correctly. For a blocked site, try curl example.com – it should fail or connect to localhost.

Common Mistakes

  • Forgetting sudo – editing /etc/hosts without sudo will give a permission denied error.
  • Using wrong syntax – missing the IP address or putting the hostname before the IP will break the mapping.
  • Editing the wrong file – some guides mention /etc/hostname (that's for the machine's name, not mappings). Stick to /etc/hosts.
  • Not flushing DNS – changes may not take effect immediately because the system caches lookups.
  • Adding comments with extra spaces – a # must be the first character of the line to be a comment; indenting it will cause an error.

Tips

  • Use a consistent separator (Tab or a few spaces) – it doesn't matter which, but be consistent for readability.
  • Group related entries with comment headers like # Development servers.
  • For blocking ads or trackers, consider using a pre-made hosts list (e.g., from someone like Steven Black's hosts project) – but be aware that these can be large and may slow down resolution.
  • Always keep a backup before making bulk changes.
  • If you use a VPN or Docker, note that they may have their own DNS settings that override /etc/hosts.

Troubleshooting

If your changes don't work, try these in order:

  1. Check for typos – run cat /etc/hosts to see the file exactly as saved. Look for missing spaces or incorrect IPs.
  2. Flush DNS again – run the flush command from Step 8, or reboot if unsure.
  3. Test with a different tool – ping might be cached; try getent hosts example.com which uses the system resolver directly.
  4. Check if the IP is reachable – ping the IP address itself (e.g., ping 192.168.1.10). If that fails, the problem is network connectivity, not the hosts file.
  5. Check if a browser is caching DNS – close and reopen the browser, or clear its DNS cache (in Chrome, visit chrome://net-internals/#dns and click “Clear host cache”).
  6. Restore the backup – if you made a mistake, run sudo cp /etc/hosts.backup /etc/hosts to revert.

Frequently Asked Questions

Does editing /etc/hosts affect all users?

Yes, /etc/hosts is system-wide. Every user on the machine will see the same mappings.

Can I use wildcards like *.example.com in the hosts file?

No, the hosts file does not support wildcards. You must list each hostname explicitly. For wildcard support, you'd need a local DNS server like dnsmasq.

Why does my change work with ping but not in the browser?

Browsers often have their own DNS cache. Close all browser windows and reopen, or clear the browser's DNS cache as described above.

Is it safe to delete the default entries?

It's safe to remove comments, but keep the 127.0.0.1 localhost and ::1 localhost entries – they are needed for many system services.

Do I need to restart networking after editing?

Usually no, but flushing DNS (Step 8) is recommended. If that doesn't work, a reboot is a guaranteed way to apply changes.

Can I use the hosts file to redirect HTTPS sites?

Yes, but the browser will show a security warning because the certificate won't match the IP you redirected to. This is fine for local development if you use a self-signed certificate.