A hosts file is a text document inside the %windir%\system32\drivers\etc
folder. You can use it to resolve hostnames before the DNS. It maps the hostname to static IP addresses, which your browser directly loads without going through the DNS server.
Developers often use it to verify their web services before launching them in the public domain. But you can also use it for blocking malicious or offensive websites.
Table of Contents
How to Edit Hosts File in Windows 11
There are mainly two ways to edit the Hosts file. You can either use a text editor or the CLI.
First, backup the hosts file. Then, apply one of the methods we have provided below.
Edit Hosts File Using Text Editor
Since Hosts is a text file, the usual method of editing it is to use a text editor. You can use the notepad to edit it. Remember that you need a text editor and not a word processor like MS Word or Wordpad.
Follow the steps below to edit Hosts using the notepad:
- Press Win + R to open Run.
- Type
notepad
and press Ctrl + Shift + Enter to run it as administrator. - Press Ctrl + O to open a file.
- Type
%windir%\system32\drivers\etc
on the address bar and press Enter. - Set the drop-down box for the file type to All Files.
- Select hosts and click Open.
Now, add your IP and domain data to the end of the file while keeping the following facts in consideration:
- The syntax is IP (Spacebar or Tab)Domain. For instance,
192.168.1.1 www.example.com
. - Your system reads lines or line segments starting with
#
as comments. - You can add multiple host domains for the same IP.
- For instance,
192.168.1.1 www.example.com example.com example.test example2.com
- You can’t use more than 150 characters or 8 hosts in a single line.
example.com
,www.example.com
, andm.example.com
are all different, even if they all refer to the same website. So, set the static IP for all of them.
Then, press Ctrl + S to save the file.
Edit Hosts File Using Command Prompt
You can also use the Command Prompt to edit the Hosts file. Here are the necessary steps for the process:
- Open Run.
- Type
cmd
and press Ctrl + Shift + Enter to open it with admin permission. - Enter
echo “IP” “DOMAIN” >> %windir%\system32\drivers\etc\hosts
while replacing “IP” and “DOMAIN”. It appends the “IP” “DOMAIN” line at the end of the hosts file. - Check the previous method for more information on the host entries.
- Enter
type %windir%\System32\drivers\etc\hosts
to verify the modifications. - You can also enter
ping “DOMAIN”
to check if it resolves to the IP you set.
Things You Can Do With the Hosts File
Some main uses of the hosts file are as follows:
Block Websites Using Hosts File
The most common use of the hosts file is to block certain websites. To do so, you must use an unreachable IP for the hosts. For instance, the localhost, which is the computer you’re using, has the IP 127.0.0.1
. So resolving any domain to this address opens a “This site can’t be reached” page.
For example, if you add 127.0.0.1 www.google.com
on the hosts file, entering www.google.com
on a browser won’t load Google. You can use any other nonexistent IP address as well.
It is especially useful in blocking malware, ad tracker, and spam links. You can find the hosts for such links on the internet.
Redirect Websites Using Hosts File
You can also redirect any URL to a server with a dedicated IP. It doesn’t work if the server uses CDN or virtual hosts. It also fails if the security certificates of the two websites do not match.
Here’s an example of the process. We are trying to open www.abc-example.com
while entering www.xyz-example.com
on a browser.
- Open Command Prompt.
- Enter
ping “DOMAIN”
. For example,ping www.abc-example.com
. - It displays the IP address for the domain.
- On the hosts file, add
“IP of abc-example” www.xyz-example.com
. - Save and exit.
Now entering www.xyz-example.com
on a browser’s address bar will open www.abc-example.com
.
Bypass Network Credential Checks
You can also bypass the check for network credentials on a server with static IP by editing the hosts file. This happens because the file forces a DNS resolution.
So, add the IP address and hostname on the hosts file to avoid the security box while accessing a remote server.
Troubleshooting Host Files Not Working
There are many potential reasons for your hosts file not working. So, we have provided similarly varied solutions for each cause. Apply them individually and check if they work.
Flush DNS Cache
The DNS cache may still contain the old hostname resolution data. So, flush the cache and enter the name on a browser again. It’s better to delete the NetBios cache as well. Follow the instructions below to delete both cache data:
- Open Command Prompt as admin.
- Enter the following commands:
ipconfig /flushdns
nbtstat -R
Check Hosts File
It is also possible that you haven’t entered the name resolution codes properly. First, check if the hosts file has an extension. On the file explorer, select View > Show and tick File name extensions. Make sure to remove its extension if it has any.
Then, edit the file in a text editor and perform the following:
- Check the syntax.
- Remove any trailing spaces. Also, make sure to use tabs or spaces but not both.
- Ensure that you haven’t commented on the necessary section by using the # symbol.
- Set the static IP for all URL forms, such as
www.example.com
,example.com
, andm.example.com
. - If there are more than 150 characters or 8 host entries on a line, divide them into multiple lines using the proper syntax.
Disable DNS over HTTPS (DoH) Encryption
With DNS over HTTPS encryption, your browser bypasses the hosts file on Windows 11. So, you need to disable this feature on your browsers to use the file. The methods for some browsers are as follows:
On Chrome
- Go to Settings > Privacy and security > Security.
- Toggle off Use Secure DNS.
On Firefox
- Go to Settings > General and click Settings under Network Settings.
- Uncheck Enable DNS over HTTPS.
On Edge
- Go to Settings > Privacy, search, and services.
- Toggle off Use secure DNS to specify how to lookup the network address for websites.
If you want to use DoH Encryption, you can enable it directly for Windows 11. Doing so allows you to use this security feature together with the hosts file’s DNS resolution. Here’s what you need to do:
- Open Settings and go to Network & internet > Hardware properties.
- Look for DNS Server Assignment and click Edit next to it.
- Set the drop-down box to Manual and toggle On IPv4.
- Enter the DNS servers you want. We recommend using public servers such as:
- Google –
8.8.8.8
and8.8.4.4
- Cloudflare –
1.1.1.1
and1.0.0.1
- Google –
- Set the DNS encryptions to Encrypted Only (DNS over HTTPS).
- Click Save.
- Then, perform the same process for IPv6. The public servers are:
- Google –
2001:4860:4860::8888
and2001:4860:4860::8844
- Cloudflare –
2606:4700:4700::64
and2606:4700:4700::6400
- Google –
- For some devices, you need to replace
::
with:0:0:0:0:
in the servers.
Check Registry Configuration
Your system uses a registry entry to check for the hosts file location. So, check this entry and ensure that it specifies the correct path. But before that, it’s better to back up your registry to account for any unexpected mistakes. Then,
- Open Run and enter
regedit
. - Navigate to
Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters
- Double-click on DataBasePath.
- Set the
Value data
to%SystemRoot%\System32\drivers\etc
.
Check Encoding
The hosts file can only have ANSI or UTF-8 without BOM encoding. The methods to edit the file that we mentioned in this article do not change its default encoding. But if you created another text file to replace the original, you may have set another encoding format.
To check, open the host file in a notepad and select File > Save as. Then, ensure that Encoding is ANSI or UTF-8 (not with BOM).
If it’s not, use the steps in the next method to replace it with another hosts file, but with proper encoding.
Reset Hosts File Permissions
If there are problems with the hosts file’s permission settings, your browsers can’t access them. You can create another host file and replace it with the original one to reset their permissions.
- Open notepad and copy the contents of the hosts file.
- Click File > Save as.
- Set the Save as type to All files and the File name to
hosts
. - Set Encoding to UTF-8 and click Save.
- Copy (not move) the file to the
%windir%\system32\drivers\etc
folder and replace the file. - Open the elevated Command Prompt and enter
icacls %SystemRoot%\System32\drivers\etc\hosts
.
Recreate Default Hosts File
Sometimes malware programs can also edit the hosts file without your consent. So, first, scan your PC with an antivirus. Then, recreate the file with default contents. Here’s how you can do so:
- Create a backup of your hosts file, just in case.
- Open notepad and enter the following:
# 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 hostname.
# The IP address and the hostname 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
- Save the file as hosts replace the original file using the steps from the previous method.
- Open the elevated Command Prompt and enter
icacls %SystemRoot%\System32\drivers\etc\hosts
.
Use Alternate TLD
Many users used to create virtual host domains with the .dev
TLD. However, it is no longer possible as Google now owns this Top Level Domain. So, it’ll redirect it https://*.dev
instead of http://*.dev
.
So you need to use an alternative TLD such as .test
for your domain.
Disable Proxies or Add Exception
Some proxies bypass the hosts file lookup. So, it’s better to disable them if you want to use the hosts file DNS resolution. To do so,
- Open Settings and go to Network & internet > Proxy server.
- Toggle off Automatically detect settings.
- Click Set up next to Use setup script.
- Toggle the switch to Off and click Save.
- Do the same for Manual proxy setup.
You can also add the exceptions for the manual proxy setup. Type the hostnames or IPs in the text box under Use the proxy server except for. Don’t forget to separate individual ones with a semicolon.