How to Edit the Hosts File on Windows, macOS, and Linux (With Code Examples)
The hosts file is a powerful but often overlooked part of your computer’s networking system. Whether you’re a developer testing a local website, redirecting domains, or blocking ads, knowing how to edit the hosts file on Windows, macOS, and Linux is a valuable skill.
In this guide, I’ll walk you through how to safely edit the hosts file on each major OS—no fluff, just the steps that work. If you’re new to this, don’t worry—it’s easier than it sounds.
🤔 What is the Hosts File?
The hosts file maps domain names to IP addresses, bypassing your DNS server. For example, you can redirect facebook.com to 127.0.0.1 and it won’t load on that device anymore.
Location of the hosts file:
Windows: C:\Windows\System32\drivers\etc\hosts
macOS/Linux/Unix: /etc/hosts
✍️ How to Edit Hosts File on Windows
✅ Step-by-Step Guide - Method #1
- Open Notepad as Administrator
- Press Start, type notepad, then right-click and choose “Run as administrator“.
- Open the Hosts File
- In Notepad, click File > Open and navigate to:
- C:\Windows\System32\drivers\etc\
- Select “All Files” to see the hosts file.
- Edit the File
- Add a line at the end of the file, Example
127.0.0.1 example.com www.example.com
- Save and Exit
⚠️ Changes take effect immediately; no restart needed. Please make sure to flush browser caches or try in incognito/private window or different browser.
✅ Step-by-Step Guide - Method #2
- Open cmd(command prompt) as Administrator
- Type
notepad C:\Windows\System32\drivers\etc\hosts
- Hit Enter
- A Notepad window will open up
- Edit the File
- Add a line at the end of the file, Example
127.0.0.1 example.com www.example.com
- Save and Exit
⚠️ Changes take effect immediately; no restart needed. Please make sure to flush browser caches or try in incognito/private window or different browser.
How to Edit Hosts File on macOS
✅ Using Terminal
- Open Terminal
- You can find it via Spotlight (Cmd + Space, then type “Terminal”).
- Open the Hosts File in nano
sudo nano /etc/hosts
- Add the entry. For example
127.0.0.1 example.com www.example.com
- To save, hit ctrl+x
- Hit Enter
🐧 How to Edit Hosts File on Linux/Unix
✅ Terminal Method (Ubuntu/Debian/CentOS etc.)
- Open Terminal
- Type “sudo nano /etc/hosts” to edit with nano or your preferred editor. e.g vim
sudo nano /etc/hosts
- Add or Modify Entries(See above on how to add the hosts entry)
- Save and Exit by hitting ctrl+x and then pressing y and finally pressing Enter key on your keyboard
- Flush DNS (optional, depending on distro)
sudo systemd-resolve --flush-caches
🛡️ Common Use Cases
→ Local Development: Point mysite.local to 127.0.0.1
→ Block Websites: Redirect facebook.com to 0.0.0.0
→ Test DNS Changes: Preview a domain before updating DNS records
Final Thoughts
Editing the hosts file is like having your own DNS on your machine. Whether you’re debugging or tweaking your network settings, it’s a trusted tool in a developer or IT professional’s toolkit.
Just remember:
➟ Always run as administrator/root
➟ Backup the file before making major changes
➟ Flush DNS cache to apply changes immediately