Modify Hosts File In Windows 10 Tutorial To Add and Change DNS Records


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%\System32\Drivers\Etc\ where %WinDir% is the path variable which stores windows installation path.

Hosts File Location
Hosts File Location

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 DOMAN_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:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 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 1.2.3.4.

# This is poftut.com special DNS record

1.2.3.4 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

#1.2.3.4 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 BACKUP.

Backup Hosts File
Backup Hosts File

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.

LEARN MORE  What Is My DNS Server and Hot To Change DNS Server For Windows, Linux?
Run Notepad As Administrator
Run Notepad As Administrator

We will be asked the following question where we will accept it by clicking Yes.

Run Notepad As Administrator
Run Notepad As Administrator

The notepad will be opened and shown like below. We can open the hosts file in different ways where we will click to the File->Open.

Notepad Open hosts File
Notepad Open hosts File

We will we asked for the hosts file location which is C:\Windows\System32\drivers\etc\hosts or %WinDir%\System32\drivers\etc\hosts.

Open hosts file
Open hosts file

The opened file will be like below where existing records will be shown.

hosts File with Notepad
hosts File with Notepad

We will add the following records where there are some comments. We will also add a subdomain name to the poftut.com named srv.poftut.com

#Poftut.com domain
1.2.3.4 poftut.com

#Poftut.com Server1
5.6.7.8 srv.poftut.com

100.101.102.103               mysrv2.kaleinfo.com
Add Records To The hosts File
Add Records To The hosts File

Then we can click to the File->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 %WinDir%\System32\Drivers\Etc\ or  C:\Windows\System32\Drivers\etc\ in our Windows Explorer.

etc directory
etc directory

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 CTRL+S or File->Save in order to save changes and modifications.

hosts File Save
hosts File Save

As we do not have Administrator rights it will ask us for Admin rights with the following screen. We will click to the Yes.

Notepad++ Administrator Rigths
Notepad++ Administrator Rigths

We will be asked the following question and click to Yes.

Now we have admin rights where we can save using CTRL+S shortcut or File->Save menu.

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 5.6.7.8 which we have set in the hosts file.

Check New Host and DNS Settings
Check New Host and DNS Settings

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 1.1.1.1 for the google.com domain like below will block the access to the Google for the specified domain names.

1.1.1.1 google.com

1.1.1.1 www.google.com

1.1.1.1 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 theC:\Windows\System32\drivers\etc\ or %WinDir%\System32\drivers\etc\ path in the Windows Explorer and right-click to the hostsfile and select the Properties like below.

LEARN MORE  What Is DNS Server Or Protocol Port Number?
Lock Hosts File By Making Read-Only
Lock Hosts File By Making Read-Only

Then we will see the Properties window where we will click the Read-only checkbox from the Generaltab and then click to the Apply.

Leave a Comment