Your cart is currently empty!
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
- Open Notepad as Administrator:
- Click the Start menu, type
Notepad
. - Right-click Notepad, choose Run as administrator.
- Click the Start menu, type
- 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.
- Edit and Save:
- Add new lines in the format:
127.0.0.1 example.com
- Click File > Save.
- Add new lines in the format:
- Flush DNS Cache (Optional):
- Open Command Prompt and run:
ipconfig /flushdns
- Open Command Prompt and run:
How to Edit the Hosts File on Mac
- Open Terminal:
- Press
Command + Space
, typeTerminal
, and hit Enter.
- Press
- Edit with Nano:
- Enter:
sudo nano /etc/hosts
- Enter your password when prompted.
- Enter:
- Make Your Changes:
- Add or edit lines in the format:
127.0.0.1 example.com
- Add or edit lines in the format:
- Save and Exit:
- Press
Control + O
to save, thenEnter
to confirm. - Press
Control + X
to exit Nano.
- Press
- Flush DNS Cache (Recommended):
- In Terminal, run:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
- In Terminal, run:
How to Edit the Hosts File on Linux
- Open Terminal.
- Edit with Nano or Vim:
- For Nano:
sudo nano /etc/hosts
- For Vim:
sudo vim /etc/hosts
- For Nano:
- Add or Edit Entries:
- Use the format:
127.0.0.1 example.com
- Use the format:
- Save and Exit:
- For Nano:
Control + O
,Enter
, thenControl + X
. - For Vim:
Esc
, then type:wq
and press Enter.
- For Nano:
- 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.
- Not all Linux systems require this, but you can run:
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.
Tech enthusiast and content creator passionate about making technology simple for everyone. I share practical tips, guides, and reviews on the latest in computers, software, and gadgets. Let’s explore the digital world together!