How to Set Up Linux Hosts File (Step-by-Step)
In this article
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
- Open a terminal. Press
Ctrl+Alt+Ton most desktop environments, or search for “Terminal” in your app menu. - 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. - Open the hosts file in a text editor. Use
sudo nano /etc/hosts(or replacenanowithvimif you prefer). Thesudois required because the file is owned by root. - 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 forlocalhostand your machine's hostname. - 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. - To block a website, map it to 127.0.0.1 or 0.0.0.0. For example,
127.0.0.1 example.comwill send requests for example.com to your own machine, effectively blocking it. Use0.0.0.0if you want a faster fail (some browsers handle it better). - Save and exit. In nano, press
Ctrl+Oto save, thenEnterto confirm the filename, thenCtrl+Xto exit. In vim, pressEsc, type:wq, thenEnter. - Flush the DNS cache (if needed). Most modern Linux systems use systemd-resolved. Run
sudo systemd-resolve --flush-caches(older systems may usesudo service network-manager restartorsudo /etc/init.d/networking restart). If you're not sure, a simple reboot also works. - Test your changes. Use
ping myserver.localorgetent hosts myserver.localto see if the IP resolves correctly. For a blocked site, trycurl 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:
- Check for typos – run
cat /etc/hoststo see the file exactly as saved. Look for missing spaces or incorrect IPs. - Flush DNS again – run the flush command from Step 8, or reboot if unsure.
- Test with a different tool –
pingmight be cached; trygetent hosts example.comwhich uses the system resolver directly. - 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. - 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”).
- Restore the backup – if you made a mistake, run
sudo cp /etc/hosts.backup /etc/hoststo 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.