How to Edit the Hosts File on Windows, macOS, and Linux (With Code Examples)

I need help...
Leave the hassle to us — we’ll handle everything professionally while you sit back and relax. Contact us now, and we’ll fix your issue ASAP!

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

  1. Open Notepad as Administrator
  2. Press Start, type notepad, then right-click and choose “Run as administrator“.
  3. Open the Hosts File
  4. In Notepad, click File > Open and navigate to:
  5. C:\Windows\System32\drivers\etc\
  6. Select “All Files” to see the hosts file.
  7. Edit the File
  8. Add a line at the end of the file, Example
      127.0.0.1 example.com www.example.com
    
  1. 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

  1. Open cmd(command prompt) as Administrator
  2. Type
      notepad C:\Windows\System32\drivers\etc\hosts
    
  1. Hit Enter
  2. A Notepad window will open up
  3. Edit the File
  4. Add a line at the end of the file, Example
      127.0.0.1 example.com www.example.com
    
  1. 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

  1. Open Terminal
  2. You can find it via Spotlight (Cmd + Space, then type “Terminal”).
  3. Open the Hosts File in nano
      sudo nano /etc/hosts
    
  1. Add the entry. For example
      127.0.0.1 example.com www.example.com
    
  1. To save, hit ctrl+x
  2. Hit Enter

🐧 How to Edit Hosts File on Linux/Unix

✅ Terminal Method (Ubuntu/Debian/CentOS etc.)

  1. Open Terminal
  2. Type “sudo nano /etc/hosts” to edit with nano or your preferred editor. e.g vim
      sudo nano /etc/hosts
    
  1. Add or Modify Entries(See above on how to add the hosts entry)
  2. Save and Exit by hitting ctrl+x and then pressing y and finally pressing Enter key on your keyboard
  3. 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

I need help...
Leave the hassle to us — we’ll handle everything professionally while you sit back and relax. Contact us now, and we’ll fix your issue ASAP!
Author

Author

I am a web developer, SysAdmin, Cloud Computing Expert, Virtualisation Expert, Linux Expert, Blogger, Cyber Security Professional and a Traveller & Hiker.

Table of Contents

Share this post

Facebook
Twitter
LinkedIn
WhatsApp

Leave a Reply

Your email address will not be published. Required fields are marked *

eight − 6 =