The hosts
file is located at the following path on Windows systems:
C:\Windows\System32\drivers\etc\hosts
On Linux/macOS systems, it's located at:
/etc/hosts
This file is a plain text file without an extension.
hosts File
The hosts
file is a system file used by the operating system to map hostnames (domain names) to IP addresses, taking precedence over DNS queries. It's commonly used for local network testing, website blocking, and development debugging.
Each line in the hosts file represents a mapping with the following format:
IP_address hostname(domain) #comment (optional)
For example:
# You can specify both IPv4 and IPv6 addresses for a domain 127.0.0.1 localhost ::1 localhost
The hosts file typically only contains the 127.0.0.1 localhost
entry by default. If modifications cause issues, you can restore the original content.
Common Uses
Local Development Testing
Point domains to local servers (like
127.0.0.1
or192.168.1.123
) for easier website debugging:127.0.0.1 myapp 192.168.1.11 api.service 192.168.1.12 mysql.service
Modifying hosts file for local development testing Blocking Specific Websites
Redirect domains to
0.0.0.0
or127.0.0.1
to block access, useful for blocking ads or unwanted sites.0.0.0.0 domain-to-block.com #Block website
Bypassing DNS Resolution
Force domains to resolve to specific IPs to avoid DNS pollution or speed up access.
Modifying the hosts File
On Windows, modifying the hosts
file requires administrator privileges:
- Open Notepad or another text editor as administrator;
- Click File - Open and navigate to
C:\Windows\System32\drivers\etc\hosts
; - Make changes and save.
On Linux/macOS, you need root
privileges:
sudo vim /etc/hosts
Common Issues
Access Denied
Failed to modify hosts file Insufficient permissions - run your text editor as administrator:
Run as administrator Changes Not Taking Effect
If changes don't take effect, you may need to flush the DNS cache:
On Windows, open CMD or terminal and run
ipconfig /flushdns
;On Linux/macOS, run
sudo systemd-resolve --flush-caches
orsudo dscacheutil -flushcache
Suspicious Configurations
Malware may modify the hosts file for phishing attacks or network hijacking.