RackNerd Billboard Banner

How to Edit Your Hosts File on Windows, Mac, or Linux

If you want to block a website, test a new domain, or redirect network traffic on your own computer, editing the hosts file is a simple and effective solution. The hosts file is a plain text file your operating system uses to map hostnames to IP addresses before reaching out to a DNS server.

Here’s how you can edit your hosts file on Windows, Mac, or Linux.


Before You Start

  • Backup the original hosts file. Always make a copy before you make changes.
  • Administrator access required. You’ll need admin (or root) privileges to save your changes.

How to Edit the Hosts File on Windows

  1. Open Notepad as Administrator:
    • Click the Start menu, type Notepad.
    • Right-click Notepad, choose Run as administrator.
  2. Open the Hosts File:
    • In Notepad, click File > Open.
    • Navigate to C:\Windows\System32\drivers\etc\.
    • In the bottom right, set the file type to “All Files (*.*)”.
    • Select hosts and open.
  3. Edit and Save:
    • Add new lines in the format:
      127.0.0.1 example.com
    • Click File > Save.
  4. Flush DNS Cache (Optional):
    • Open Command Prompt and run:
      ipconfig /flushdns

How to Edit the Hosts File on Mac

  1. Open Terminal:
    • Press Command + Space, type Terminal, and hit Enter.
  2. Edit with Nano:
    • Enter:
      sudo nano /etc/hosts
    • Enter your password when prompted.
  3. Make Your Changes:
    • Add or edit lines in the format:
      127.0.0.1 example.com
  4. Save and Exit:
    • Press Control + O to save, then Enter to confirm.
    • Press Control + X to exit Nano.
  5. Flush DNS Cache (Recommended):
    • In Terminal, run:
      sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

How to Edit the Hosts File on Linux

  1. Open Terminal.
  2. Edit with Nano or Vim:
    • For Nano:
      sudo nano /etc/hosts
    • For Vim:
      sudo vim /etc/hosts
  3. Add or Edit Entries:
    • Use the format:
      127.0.0.1 example.com
  4. Save and Exit:
    • For Nano: Control + O, Enter, then Control + X.
    • For Vim: Esc, then type :wq and press Enter.
  5. Flush DNS Cache (If Needed):
    • Not all Linux systems require this, but you can run:
      sudo systemd-resolve --flush-caches
    • Or restart your network service.

Common Use Cases

  • Block websites: Map a site to 127.0.0.1.
  • Test site migration: Point a domain to a different IP.
  • Speed up local development: Direct a domain to your local machine.

Tips

  • Each line follows: IP_ADDRESS DOMAIN_NAME
  • Use # to comment out a line.
  • Don’t delete existing entries unless you know what they do.

Editing your hosts file gives you direct control over how your system resolves hostnames. Just remember to back up the original, make your edits carefully, and enjoy the control this gives you over your local network traffic.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
RackNerd Billboard Banner
© 2025 Computer Everywhere
Your Everyday Guide to the Digital World.
Terms of Service | Privacy Policy
Copy link