What is the Hosts File and What is its Purpose?
The Hosts file is a text file that translates domain names into IP addresses.
It allows your computer to locate a website without querying a DNS server. This can speed up the loading of websites and is often used to block or redirect access to certain websites.
Advantages of the Hosts File
- Faster Website Resolution: Direct mapping of domain names to IP addresses without DNS queries.
- Network Security: Blocking malicious or unwanted websites.
- Development and Testing: Local development environments and testing websites without going live.
Disadvantages of the Hosts File
- Maintenance: Changes must be made manually.
- Limited Effectiveness: Does not affect all types of network requests, e.g., those running directly over IP addresses.
- No Wildcard Support: Unable to automatically handle dynamic subdomains.
Editing the Hosts File in Different Operating Systems
The Hosts file is a simple text file that can be edited with various text editors.
Windows (including differences between Windows 11 and older versions)
- Path: For Windows 11/10/8/7, the file is located at C:\Windows\System32\drivers\etc\hosts. The path remains the same in these versions.
- Editing: Open the file with a text editor (e.g., Notepad) as an administrator.
- Example: To block Google, add the line 0.0.0.0 www.google.com.
Linux
- Path: The Hosts file is located at /etc/hosts.
- Editing: Use a text editor like Nano or Vi with root privileges.
- Example: To redirect a local test page, add 0.0.0.0 my-test-page.local.
Android
- Path: Similar to Linux, under /system/etc/hosts.
- Editing: Root access is required. Use a file explorer with root rights.
- Example: Block ads by adding ad server domains with the IP 0.0.0.0.
Comments in Hosts Files
The hosts file facilitates the inclusion of comments to enhance clarity and comprehension, proving particularly beneficial in extensive or customized files.
Common use cases include:
- Section Delimitation:
Comments can be employed to structure the hosts file by designating specific areas and their corresponding entries through headings or descriptions. - Entry Clarification:
Comments can elucidate the function or purpose of individual entries, thereby simplifying maintenance and promoting understanding of the file. - Temporary Disablement:
Entries may be temporarily deactivated through commenting, obviating the need for complete removal from the file.
In most operating systems, comments are denoted by the '#' character at the beginning of a line; any subsequent text on that line is disregarded by the system.
# This is a comment
0.0.0.0 local.example.com # Blocks access to local.example.com
# This is a released entry
#0.0.0.0 local.example.com # The hash before the entry stops the system from blocking the address
# Block
0.0.0.0 local.example.com # With the hash missing before the entry, the address is blocked again
Note on Host File Write Protection
The hosts file is often write-protected as a security measure to prevent unauthorized modifications. Administrator privileges are typically required to edit the file. As alternatives, the hosts file can be temporarily copied to another location for editing, or the editing program can be launched with appropriate privileges.
Granting Write Permissions for the Hosts File:
- Windows: Right-click on the file, select "Properties", and uncheck "Read-only".
- Linux: Use the command sudo chmod +w /etc/hosts to grant write permissions.
- Android: Ensure you have root access. Some file explorers offer options to change file permissions.
Differences Between 127.0.0.1 and 0.0.0.0
- 127.0.0.1 is the loopback IP address, referring to your own computer. It is used to block access to a specific website.
- 0.0.0.0 is a non-routable meta-address used to mark addresses as invalid. It is more effective in blocking ads as it consumes fewer resources.
What is a Loopback Address?
A loopback address is a special IP address used in computer networks to refer back to the own computer. The most well-known loopback address is 127.0.0.1 in the IPv4 address space and ::1 in the IPv6 address space.
When data is sent to the loopback address, it does not physically leave the computer but is processed internally by the network interface. This means that the data is treated as if it were coming from the network, although it is actually being transmitted only within the system.
Your opinion is important – please leave a comment!
2 Comments

lightwo
2024-01-10 18:14:05
Tip for Linux users, here's a nifty little script that constructs a host file out of multiple lists with ability to add hosts to whitelist and blacklist:
https://github.com/hectorm/hBlock
There's probably a Windblows equivalent to this.
Since you also mentioned Android, here's an application that does things nearly exactly like hBlock, that can be used either to edit the hosts file, or provide a tunnel that blocks the specified hosts without root access:
https://github.com/AdAway/AdAway
lightwo
2024-01-10 18:22:10
BTW, on a similar note, for a future article, perhaps you could touch upon IP blocking on Linux (since there only seem to be CLI-based somewhat-not-so-straightforward ways of doing so)? iproute2 always felt silly in how it was used and configured, but I found that nftables with a simple config like this does the trick, ex. for Valve error reporting:
https://bin.disroot.org/?fcd84c2af0be0196#EFK15MNDt7dSEBZSicQben3fGyqAUHteYHmbhNPG45Xw