A Guide to Editing Your Hosts File on Windows, Mac, and Linux
Research insights
A Guide to Editing Your Hosts File on Windows, Mac, and Linux
Table of Contents
Editing your hosts file can be necessary for several reasons. Sometimes, it's done to counteract malware or pranks, while other times, it’s a way to control access to websites and network traffic on your machine. This guide explains how to access and edit the Windows, macOS, and Linux hosts files.
What Is a Hosts File?
The hosts file has existed since the early days of ARPANET, predating DNS as a way to resolve hostnames. Back then, hosts files were extensive documents used for network name resolution. While DNS largely replaced this function, host files remain part of modern operating systems, allowing users to control name resolution manually.
The host's file syntax is nearly identical in Windows, macOS, and Linux. The file typically contains several entries for loopback addresses, which serve as examples. A typical syntax line includes:
The destination IP address.
The domain name to redirect.
An optional comment, separated by a hash (#).
For example:
arduino
127.0.0.1 localhost # loopback
Editing the Hosts File on Different Operating Systems
Windows (8, 8.1, 10, or 11)
Access Notepad as Administrator:
Search for "Notepad" in the Start Menu.
Right-click on the search result and choose "Run as Administrator."
Open the Hosts File:
Once Notepad is open, navigate to the file using File > Open.
The host file is located at:
makefile
C:\Windows\System32\drivers\etc\hosts
Edit the File:
Add new entries in the format shown above. For example, to block Facebook:
127.0.0.1 www.facebook.com
Save the File:
Use File > Save to apply changes.
Windows 7
For Windows 7, the process is similar:
Open the Run dialog box (Windows + R).
Enter:
mathematica
notepad C:\Windows\System32\drivers\etc\hosts
Edit the file and save your changes.
Note: In all versions of Windows, ensure you have an empty line at the end of the file to avoid issues.
Ubuntu (or Other Linux Distributions)
The host file is located in the /etc/ directory in Linux. To edit it:
Open Terminal:
Use your preferred terminal emulator.
Edit the File with Root Privileges:
Use a text editor such as vim, nano, or GUI. For example, with vim:
bash
sudo vim /etc/hosts
Modify the File:
Add entries as needed. For example, to block Facebook:
127.0.0.1 www.facebook.com
Save and Exit:
For vim, press Esc, type :wq, and press Enter.
Linux hosts files often include IPv6 loopback addresses as well. Unless needed, you can focus only on IPv4 entries (e.g., 127.0.0.1).
macOS (All Versions)
On macOS, the hosts file is also located in /etc/. The steps are nearly identical to Linux:
Open Terminal:
Use the Terminal app, which can be found in the Applications folder under Utilities.
Edit the Hosts File:
Use your preferred editor, such as nano or vim. For example:
bash
sudo nano /etc/hosts
Add or Modify Entries:
Enter new lines to block websites, such as:
127.0.0.1 www.facebook.com
Save Changes:
In nano, press Ctrl + O to save, then Ctrl + X to exit.
macOS hosts files resemble Windows files but often lack comments. Like other platforms, ensure you add a blank line at the end of the file.
Important Notes
Browser Compatibility:
Some browsers (like Google Chrome) may not always respect hosts file changes during testing. Adding "www." to domains (e.g., www.facebook.com) often resolves this issue.
Caching Issues:
After editing the hosts file, clear your browser cache or restart your computer to ensure changes take effect.
Loopback Address Variations:
On macOS, 0.0.0.0 may direct you to the Apache test page instead of an inaccessible site. For consistency, use 127.0.0.1.
Conclusion
The hosts file is a powerful tool for managing network access and blocking websites. Editing the file is straightforward whether using Windows, macOS, or Linux. You can control traffic to specific domains and improve your system’s security with a few adjustments. If you have any tips or tricks for managing hosts files, feel free to share!