Hosts file
The hosts file is used in operating systems to map domain names to IP addresses without accessing DNS servers. It can be used for local website testing, blocking unwanted resources, speeding up access, and protecting against malware.
The file's location is static and differs for Linux and Windows systems:
- path for Linux:
/etc/hosts - path for Windows:
C:\Windows\System32\Drivers\etc\hosts
The mapping is performed identically for both systems, specifying the IP address first, followed by the domain, separated by a space:
127.0.0.1 localhostWith mapping, all requests for a specific domain name will be sent by the system strictly to the specified IP address.
hosts is a system file and can only be edited with system administrator privileges!
Website Testing
Mapping the domain name of a non-hosted website to the IP address of the web server that hosts the website's content will help you test its functionality as if it were a publicly accessible website.
For example, you plan to move domain.com to a new server with the address 85.16.42.11 and want to avoid downtime: the website's content has already been copied to the new server, but domain.com is still pointed to the IP address of the current server where it operates. To test the functionality of the website for this name on the new server from your device, add a new line to the hosts file:
85.16.42.11 domain.comNow all requests for the domain will be sent to the new server, and you can test the website's functionality in the new environment. Don't forget to remove this entry once the website is moved to the new server!
Domain blocking
Since the mapping tells the system which address to send requests to, requests for unwanted domains can be sent to IP addresses unavailable in your network or to third-party IP addresses.
For example, if you want to prevent other users in your system from accidentally visiting the phishing site phishing.scam, add the following line:
0.0.0.0 phishing.scamAccess speed optimization
Mapping helps avoid requesting data from an external DNS server, which speeds up the request to the target server. This is especially noticeable if the external DNS server is slow.
For example, you know that the website localshop.sell is permanently located at 85.16.42.11, so you can add the following line:
85.16.42.11 localshop.sellMany websites use multiple IP addresses to distribute the load, and this mapping can have the opposite effect!
Malware prevention
Some malware intentionally spoofs domain IP addresses through hosts, for example, so that when you visit a familiar site you trust, you end up on a fake phishing site.
For security puproses, it's recommended not only to use antivirus software but also to manually scan the file contents for unnecessary entries periodically and if you have any suspicions.