It has been discussed how the hosts file can be used to block access to websites (even though ive tried it several times on both a windows and linux machine and it hasn't worked), however, as far as i understand the purpose of the file is to map hosts names to addresses. How is it that adding an ip and a web address can block visits to the site?
You don't use the hosts file to block an IP address. You use it to block a domain name - well you don't block it, you redirect it. It's like changing your phones contact list. When you enter 000000 for your mom's phone number, you will get a non existing number warning when calling her via the contact list.
If you know her number (IP address) by head, you can type it in directly, and it still works. This could work for websites, but for many it won't work. Sometimes IP addresses are linked to many different websites hosted on the same machine. Then it's unclear what website to show, and it will show the default website, probably not the one you want. And sometimes websites need the domain name to work, and just the IP address results in an error.
You can use the hosts file to map domain names to IP addresses. You can use this to do the following:
- Map nytimes.com (or any other site) to 127.0.0.1, which is the local address of your computer. Now nytimes.com won't work anymore, or it will display whatever your local webserver is showing.
- Map yournewwebsite.net to 22.214.171.124 (or whatever it is). You have a new website, not public yet, and you want to see it working with the new domain name. You have not set the DNS for the domain name, or it is still mapped to the old site. This way you can test the domain, and only on your laptop you see the new site. Sometimes the website needs the real domainname for it to work, and during development and testing this is a good method to try this without disturbing the production website.
- Use a non existing domain, like test.local, and map that to your local webserver, or to some other IP.
So adding an entry in the hosts file does not block anything, it just directs it to something else, which may be just a blank page. That's a way of blocking. You can do this in the router as well.
NB: your computer has two IP addresses, one local - 127.0.0.1, and one public, usually something like 192.168.1.10 when behind a router. Public means relative to the network it's connected to. The router has the real public address.
Your operating system will first look at the hosts file before attempting to look for name resolution elsewhere. The hosts file is much quicker than querying the network so if the OS can find the answer there then it is one less query to do elsewhere.
The hosts file doesn't "block" traffic, it gives an answer to a lookup to provide a quicker answer than having to send the request elsewhere that may take much longer to provide the desired answer.
Similarly how an SSHD will store the most frequently accessed data on the SSD (as it is much quicker to access) than on the mechanical portion of the SSHD (which is much slower to access).