On this tutorial, I want to introduce you what Windows hosts file is and how to manage it.
In fact, any OS these days has such file or configuration, but I am focusing on editing them on Windows environment. Modifying hosts file varies between OS.
What is a host file?
The host file it’s like an address book. You look for a people’s name, and if available, you find the phone number. That number allows you to interact with the person, via phone calls or messaging.
The hosts file works on a similar way. You try to browse to an address (www.example.com), but that address is not the address the computer needs, it’s not the “phone number”. The computer really needs that number, the IP address. Therefore, the operating system checks the hosts file, where it finds paired values of hosts names and the matching IP address. If it finds the IP, then the computer can “call it”. Otherwise, it needs to ask ISP’s (Internet Service Provider) for the IP, so that the computer can actually “call it”. If for some reason ISP can’t tell you the IP for the host, the computer can’t solve the site address and as a consequence, it can’t load it.
It’s also true that most of time the computer always ask ISP for the IP address to find sites, because you don’t have to do that work manually on your hosts file. Imagine that you had to type every single IP for each site on your hosts file… It’s just impossible, especially nowadays with Internet expanding so fast. Every day hundreds of thousands of domains are created, deleted or transferred!
In my opinion, hosts file are more commonly used to block domains, I can’t see much more applications. And that is very simple to achieve. Your computer has an IP Address too, therefore you can configure the hosts file to map some host name, www.example.com, to your machine IP, and so, you the computer simply CAN’T load it. Keep in mind that the OS firstly checks hosts, and if doesn’t find the IP for the specific host, then it calls ISP. That’s why this little trick works.
In conclusion, you can see how powerful configuring the hosts file is, such hacking software activation methods. :p On the other hand, it is also dangerous and it’s widely used with bad intentions. There are many cases where users have hijacked hosts files! Now that you understand what is a host file, let’s see where is it and how to edit it.
Where is my hosts file?
You might wonder where is this little, yet powerful, file or, even if it exists. Yes, despite the fact the file is created when you set up Windows for the first time, it’s very common the file disappears. There is a default location for it: C:\Windows\System32\drivers\etc. The file has no extension, it’s just… a file. Although, the file is written on text plain. In other words, every program capable to read text, can open it. That’s a great thing, because it allows you to use the Notepad on Windows to manage your hosts file.
How to edit hosts file on Windows?
As I mentioned on previous section, a simple notepad allows you to view and edit the hosts file. However, there are a few things to consider before attempting to edit it.
- You need administrator privileges to edit it
- The hosts file might have Read Only attributes. That makes impossible to edit the file.
For the first requirement, well, basically requires you to log-in with an account with admin rights. That won’t be a problem for most people.
The second requirement, it’s indeed, very important to look at. You can check the file attributes right-clicking hover it, then pressing Properties on the context menu. A small dialog should pop up with several information about the file. On General tab, just make sure the Read Only box is unchecked. If it’s not, uncheck and then click OK to save changes.
Now that we have all the conditions to change the hosts file, let’s do it.
This might be a more user friendly way to edit the hosts file, although, I wouldn’t say it is the easiest and fastest method.
I use Notepad for this task, any equivalent program works, of course. As I mentioned before, you need special rights to edit the file. Therefore, you need to run Notepad, or the program you chose, with such rights. In order to do that, search Notepad on start menu, right click and then press Run as administrator. The notepad window shows up.
Then, on left top, click File, Open and browse to hosts file location. I remind you that is C:\Windows\System32\drivers\etc.
At first, you won’t see any file. That happens because, as I mentioned before, hosts file has no extension. Moreover, notepad is set to look for Text Documents, files with .txt extension. On file picker dialog, on bottom right, there is a combo selector. You just need to set it to All files (*.*). Now, hosts file should be visible. If not, that might mean your system hasn’t a host file yet. No problem. You can create your own, just check the troubleshooting section.
The default host file created by Windows on set up, starts with a bunch of informative lines starting with hash #. Those are comments and are ignored. The syntax on hosts file is very simple, and must respect three rules:
- Each entry should be kept on an individual line
- First comes the IP adress and then the corresponding host name
- IP adress and host name MUST be separated with at least one space. Usually Tab is used to make the text more readable, giving a table look with two columns.
As you finish to modify the hosts file, don’t forget to Save it. You can do it through the menus on top: File, then Save. If you followed all the steps before (running notepad as admin and remove read-only attribute), you won’t get any error and you are done! 🙂
Using batch / CMD commands