How to Edit the hosts File in Windows, macOS, and Linux
/ 2 min read
Table of Contents
The hosts file maps domain names to IP addresses locally on your computer. Entries in this file take precedence over external DNS servers. This is useful for testing websites before updating domain DNS records or for blocking access to specific hosts.
Before You Begin
Antivirus software and Windows Defender may block changes to the hosts file. If you encounter an “access denied” error while saving, temporarily disable protection or add the file to your antivirus exclusions.
Editing in Windows (10, 11)
Method 1: Via Search and Notepad
- Press the
Winkey and type “Notepad”. - Right-click the icon and select “Run as administrator”.
- In the program menu, go to File → Open.
- Navigate to:
C:\Windows\System32\drivers\etc. - Change the file filter in the bottom-right corner from “Text Documents” to “All Files (.)”.
- Open the
hostsfile.
Method 2: Via Terminal
- Press
Win + Xand select Terminal (Admin) or PowerShell (Admin). - Run the following command:
Terminal window notepad C:\Windows\System32\drivers\etc\hosts
Editing in macOS and Linux
On UNIX-like systems, the file is located at /etc/hosts. Modifying it requires superuser (sudo) privileges.
- Open the “Terminal” application.
- Run the command to edit using the
nanoconsole editor:Terminal window sudo nano /etc/hosts - Enter your administrator password.
- Use
Ctrl+Oto save andCtrl+Xto exit the editor.
Entry Format
Add new entries at the end of the file. Each entry should be on a new line.
| IP Address | Domain Name |
|---|---|
127.0.0.1 | local.test |
0.0.0.0 | telemetry.service.com |
Example:
127.0.0.1 mysite.localVerifying Changes
To ensure the changes have taken effect, run the ping command in your terminal:
ping mysite.localThe response should come from the IP address specified in the hosts file.
Resetting the hosts File
If you experience network access issues, clear the file and leave only the standard entries:
127.0.0.1 localhost::1 localhost