The hosts file is a simple but powerful system file that maps domain names to IP addresses on your local machine. It can be used for testing websites, blocking domains, or overriding DNS settings without affecting other devices. Modifying it correctly requires administrator access and careful editing to avoid system issues.
The hosts file is a plain text file used by your operating system to resolve domain names before checking external DNS servers. For example, you can map example.com to a specific IP address so your computer will always load that address instead of the real one.
Click on the Start menu, type Notepad.
Right-click Notepad and select Run as administrator.
In Notepad, click File > Open.
Navigate to:
C:\Windows\System32\drivers\etc\Change file type to All Files (.) so you can see the hosts file.
Open the file named hosts.
Add your entry in this format:
127.0.0.1 example.comSave the file.
To apply changes, you may need to flush DNS using:
ipconfig /flushdnsOpen Terminal.
Run the command:
sudo nano /etc/hostsEnter your administrator password.
Add entries like:
127.0.0.1 example.comPress CTRL + X, then Y, and hit Enter to save.
To refresh DNS:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponderOpen Terminal.
Use a text editor with sudo privileges:
sudo nano /etc/hostsAdd your required mapping:
127.0.0.1 example.comSave and exit using CTRL + X, then Y, then Enter.
Most Linux systems automatically apply changes immediately, but restarting networking services can help if needed.
Always create a backup before editing the hosts file.
Do not remove existing system entries unless you know what they do.
Incorrect entries may block websites or cause connectivity issues.
Use comments (#) to label your changes for future reference.
Modifying the hosts file is a useful technique for developers and system administrators. Whether you're testing a website locally or blocking unwanted domains, understanding how to safely edit this file across different operating systems gives you greater control over your network behavior.