Edit Hosts file in Windows OS is not very easy. The hosts file is generally used to map the IP address to a website’s address. Generally, mapping of a web page can be done using global DNS. But, an IT professional can bypass the DNS and use the hosts file to map a web address to a different IP locally.
The hosts file is located at the following location of any major Windows version (windows xp, windows vista, windows 7, windows 8, windows 8.1 and windows 10).
These are the system files and is hidden by default. But, you need to unhide them before edit hosts file in windows 10 or windows 7.
Open windows explorer. Press Alt+V and then press Alt+HH to show hidden files in windows 10/8.1/7.
Edit Hosts File in Windows 10, Windows 7, Windows XP, Windows 8
- Show all the hidden files in Windows.
- Navigate to the location of the windows hosts file mentioned above.
- Move the file to the desktop.
- Right click on the file and open it with notepad.
- Edit it that you want to.
- Save it by pressing Ctrl+S button.
- Move the modified hosts file to the previous location i.e. C:\Windows\System32\drivers\etc
- That’s it.
If your hosts file is located at the following location,
You can edit the hosts file by using the following steps:
Open the command prompt with administrative privilege.
Use the following command:
Now, edit the file and save it by pressing Ctrl+S.
Lock Windows Hosts File to Prevent Hijacking or Malware Attack
Due to some malware activity, you might notice that a website is redirected to a new address. This is called host file hijacking. To prevent this, you need to lock Windows hosts file. If you want to lock the file, just make hosts file read-only.
Right click on the hosts file, navigate to properties and tick the check box beside to read only to convert windows hosts file read only.
Block Websites with Windows Hosts
In a previous article, I have mentioned how to block a website using Chrome browser. Now, you can block any website by editing windows hosts file.
To block any website using windows hosts file, follow the below steps.
Ready the hosts file so that you can edit it.
If you want to block facebook.com, just add the following line in windows hosts file:
127.0.0.1 facebook.com 127.0.0.1 www.facebook.com 127.0.0.1 m.facebook.com
0.0.0.0 facebook.com 0.0.0.0 www.facebook.com 0.0.0.0 m.facebook.com
Reset Windows Hosts File
To reset the hosts file, login to your windows PC with an administrator account. Navigate to the following path where the hosts file is generally located i.e. %WinDir%\system32\drivers\etc.
Rename the file as hosts.bak. You may need to take ownership of the hosts file also.
Then copy the texts and paste the code to notepad.
# Copyright (c) 1993-2009 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. # # This file contains the mappings of IP addresses to host names. Each # entry should be kept on an individual line. The IP address should # be placed in the first column followed by the corresponding host name. # The IP address and the host name should be separated by at least one # space. # # Additionally, comments (such as these) may be inserted on individual # lines or following the machine name denoted by a '#' symbol. # # For example: # # 184.108.40.206 rhino.acme.com # source server # 220.127.116.11 x.acme.com # x client host # localhost name resolution is handle within DNS itself. # 127.0.0.1 localhost # ::1 localhost
Save the file as hosts and choose the save as type “all files”.
This is the default windows hosts file.
So far, I have discussed the method of editing hosts file manually. HostsMan and SysMate hosts file walker are the two third-party tool that can manage hosts file very easily. You can enable/disable, edit, modify the hosts file directly from the software window. It lets you scan the hosts file using the scanner for duplicate and malicious entries. You can flush the DNS cache using HostsMan.
If you want to edit the hosts file in Ubuntu and Mac OS, you can use the following command:
sudo vim /etc/hosts
Hopefully, this tutorial helps you to understand the hosts file and how the hosts file protects your PC. You can use it to block websites and unblock websites like facebook or any other website if the blocking is done by the Windows Hosts file.