DNS makes us name IP addresses into meaningful names. We can name a host with a human-readable name and use this name to access it. DNS management can be done in different ways where DNS servers are the most practical and straightforward way. But the first step before a DNS server to query a DNS is using the local
Hosts File. In this tutorial, we will learn how to manage Windows 10 Hosts File like change, secure, restore, block, etc. These settings can be also applied to all modern Windows operating systems like Windows 7, Windows 8, Windows 10, Windows Server 2008, Windows Server 2012, Windows Server 2016, Windows Server 2019.
What Is Hosts File?
hosts file is a basic text file that stores IP address and domain name matches. Every IP address has a domain name that should be compliant with the domain name standard.
hosts file usage is very popular and standard where it is used different operating systems like Microsoft Windows, Linux Distributions and MacOSX operating systems.
Hosts File Location
The hosts file is stored in different paths in different operating systems. The hosts file is generally stored in a system configuration path or location which requires special privileges like Administrator to access and change it. In Windows operating systems hosts file is store
C:\Windows\System32\Drivers\etc\ file as it can be also expressed like
%WinDir% is the path variable which stores windows installation path.
Also, we can see that its size is 1KB where it stores very little data in text format.
Hosts File Syntax
As stated previously hosts file has very basic syntax. Each line is an entry to match the IP address with the domain name.
- `IP_ADDRESS` is the IP address we want to route for the given DOMAIN_NAME
- `DOMAIN_NAME` is the domain name we want to match with the IP_ADDRESS
# 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: # # 18.104.22.168 rhino.acme.com # source server # 22.214.171.124 x.acme.com # x client host # localhost name resolution is handled within DNS itself. # 127.0.0.1 localhost # ::1 localhost
Hosts File Comment
In hosts file
# sign is used to create comments. Comments are useful to create some notes or explanations about the given DNS record. We can also use comments in order to make an active record passive. In the following example, the
poftut.com domain name will be redirected to the IP address
# This is poftut.com special DNS record 126.96.36.199 poftut.com
If we want to make this record passive without deleting it we can put a
# before the poftut.com record to make it passive.
# This is poftut.com special DNS record #188.8.131.52 poftut.com
Backup Hosts File
Before starting the adding, removing or changing the contents of the hosts file backing up is good behavior. Especially if there are a lot of records it will be very useful. As hosts file is a simple text file we can take a backup just copy and paste it into the same directory with a different name or different directory will work. Below we copied the hosts file and added the
.backup extension. We can see that the type of
hosts.backup is also shown as
Modify Hosts File with NotePad
Modifying the hosts file requires Administrative privileges. so in order to edit and save with the NotePad applications, the NotePad should be opened with the Administrative user rights. First From the
Start Menu type
notepad and right-click to the listed notepad app.
We will be asked the following question where we will accept it by clicking
The notepad will be opened and shown like below. We can open the hosts file in different ways where we will click to the
We will we asked for the hosts file location which is
The opened file will be like below where existing records will be shown.
We will add the following records where there are some comments. We will also add a subdomain name to the poftut.com named
#Poftut.com domain 184.108.40.206 poftut.com #Poftut.com Server1 220.127.116.11 srv.poftut.com 100.101.102.103 mysrv2.kaleinfo.com
Then we can click to the
Save in order to save and activated newly added records.
Modify Hosts File with NotePad++
As a simple text file, we can open the
hosts file with the Windows NotePad. We will navigate to the
etc directory by using
C:\Windows\System32\Drivers\etc\ in our Windows Explorer.
We will see the following
hosts file which is opened by the Notepad++.
We will add the following lines to the existing
hosts file and use
Save in order to save changes and modifications.
As we do not have Administrator rights it will ask us for Admin rights with the following screen. We will click to the
We will be asked the following question and click to
Now we have admin rights where we can save using
CTRL+S shortcut or
Check New Host and DNS Settings
We can check the newly added DNS records by using the ping command. We will ping the
srv.poftut.com host like below. As it does not exist the ping responses will fail but we can see that the IP address is
18.104.22.168 which we have set in the hosts file.
Block Website Using Hosts File
Well changing the hostnames, DNS records or IP addresses gives us the ability to block sites in the given host by using hosts file. For example, add the IP address
22.214.171.124 for the
google.com domain like below will block the access to the Google for the specified domain names.
126.96.36.199 google.com 188.8.131.52 www.google.com 184.108.40.206 gmail.com
Lock Hosts File By Making Read-Only
We can prevent the hosts file from accidental change or modification by making it read-only. We will open the
%WinDir%\System32\drivers\etc\ path in the Windows Explorer and right-click to the
hostsfile and select the
Properties like below.
Then we will see the Properties window where we will click the
Read-only checkbox from the
Generaltab and then click to the