How to Edit the Hosts File
The hosts
file is a system file that maps IP addresses to domain names. It can be useful for testing websites on a local server, blocking websites, or redirecting domains. In this guide, we’ll walk through how to edit the hosts
file on both Windows and macOS.
What is the Hosts File?
The hosts
file is used by the operating system to map domain names to IP addresses. For instance, you can set the domain example.com
to point to a local server by assigning it a specific IP address.
How to Edit the Hosts File on Windows
- Open Notepad as an administrator:
- Search for Notepad in the Start menu.
- Right-click on the Notepad icon and select “Run as administrator.”
- In Notepad, open the
hosts
file by navigating toC:\Windows\System32\drivers\etc\hosts
. - Add the required entries in the format:
IP address domain
. For example:127.0.0.1 example.com
- Save your changes by selecting “File” → “Save.”
How to Edit the Hosts File on macOS
- Open Terminal (you can find it via Spotlight or in the Applications folder).
- Enter the following command to open the
hosts
file in the nano editor:sudo nano /etc/hosts
You may be prompted to enter your administrator password.
- In the editor, add the necessary entries. Use the same format:
IP address domain
. For example:127.0.0.1 example.com
- To save, press Ctrl + O, then Enter, and exit the editor by pressing Ctrl + X.
Verifying the Changes
To ensure the changes take effect, you may need to clear your DNS cache:
On Windows:
ipconfig /flushdns
On macOS:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
The hosts
file is now updated, and the changes should take effect. You can verify this by opening the website you redirected in your browser.