Windows 11|Windows Client OS

How to Easily Edit the Hosts File in Windows 11

In the early days of the Internet, there was just one ‘hosts’ file that was used to match computer names to IP addresses. All devices connected to the Internet are represented by an IP address. An IP address might look something like this: 172.217.23.238. As you can see, it would be difficult to remember the IP address of every device connected to the Internet! So, the hosts file contained a phonebook-like directory of IP addresses mapped to computer hostnames.

As the Internet developed and more devices were connected in different geographical locations, it was no longer practical to maintain a single hosts file. The Domain Name System (DNS) was created in 1984 to provide devices connected to the Internet with a way to look up hostnames by querying a set of distributed servers. And that is how the Internet works to this day.

[toc]

Where is the hosts file in Windows 11?

But the hosts file remains part of most operating systems. It can be used to override results provided by DNS servers and provide hostname lookup on a local area network (LAN) in the absence of a DNS server. Depending on the operating system, the hosts file is found in a different location. For instance, in Unix, you will find the hosts file in /etc/hosts. And on all modern versions of Windows, including Windows 11, the hosts file is in %SystemRoot%\System32\drivers\etc\hosts. The %SystemRoot% environment variable by default points to C:\WINDOWS.

Sponsored Content

Passwords Haven’t Disappeared Yet

123456. Qwerty. Iloveyou. No, these are not exercises for people who are brand new to typing. Shockingly, they are among the most common passwords that end users choose in 2021. Research has found that the average business user must manually type out, or copy/paste, the credentials to 154 websites per month. We repeatedly got one question that surprised us: “Why would I ever trust a third party with control of my network?

How the hosts file works

The hosts file contains lines of text with two columns. In the first column there is an IP address. And in the second column, there is a computer hostname. The two columns can be separated by a space or tab. So, an example hosts file might look like this:

192.168.0.10 bobpc
192.168.0.11 clairepc
192.168.0.129 printserver

Easily Edit the Hosts File in Windows 11 (Image Credit: Russell Smith)

If you add these entries to the host file on a device, then you can ping each host by name and the computer can resolve it to the corresponding IP address. For example, ping printserver would return a reply like this:

Easily Edit the Hosts File in Windows 11 (Image Credit: Russell Smith)

You can also use Fully Qualified Domain Names (FQDN) in a hosts file. For example:

172.217.23.238 google.com

The hostnames you add to the hosts file are not case sensitive. They can be in uppercase, lowercase, or a mixture of both. Be aware that the hosts file is sometimes used by hackers to redirect network traffic to malicious sites. That is why only administrators can edit the hosts file in Windows.

View the hosts file in Windows 11

By default, the hosts file in Windows 11 doesn’t contain any valid entries. By that, I mean everything is commented out using the hash # symbol. Let start by opening the hosts file in Notepad.

  1. Click Start on the taskbar.
  2. Type notepad and press ENTER.
Easily Edit the Hosts File in Windows 11 (Image Credit: Russell Smith)
  1. Press the Windows key (WIN) + E to open File Explorer.
Easily Edit the Hosts File in Windows 11 (Image Credit: Russell Smith)
  1. In the box where it says ‘Quick access’, paste the following path and press ENTER: %SystemRoot%\System32\drivers\etc\
  2. In the list of files, you will see hosts.
Easily Edit the Hosts File in Windows 11 (Image Credit: Russell Smith)
  1. Arrange File Explorer and Notepad so that are side-by-side.
  2. Drag the hosts file from File Explorer into the Notepad window.
Easily Edit the Hosts File in Windows 11 (Image Credit: Russell Smith)

You’ll see that there are two entries at the bottom that are both commented out. The first entry is the IPv4 address for localhost. And the second entry is the IPv6 address for localhost.

#            127.0.0.1       localhost
#            ::1                  localhost

Easily Edit the Hosts File in Windows 11 (Image Credit: Russell Smith)

But if you try to add an entry to the hosts file, you won’t be able to save it. The hosts file can only be edited by administrators because it is in a protected system directory.

Edit the hosts file in Windows 11

There are two ways that you can edit the host file. The first involves making a copy of the original file and then replacing it. The second uses the command line to append a line to the hosts file.

Edit the hosts file using the GUI and Notepad

The easiest way to edit the hosts file is to make a copy of it and then replace the original in the ‘etc’ folder.

  1. Open File Explorer by pressing the Windows key (WIN) + E.
  2. In the box where it says ‘Quick access’, paste the following path and press ENTER: %SystemRoot%\System32\drivers\etc\
  3. In the list of files, right click hosts while at the same time dragging the file to the desktop.
Easily Edit the Hosts File in Windows 11 (Image Credit: Russell Smith)
  1. Release the mouse button and then select Copy here from the menu.
  2. Click Start on the taskbar.
  3. Type notepad and press ENTER.
  4. Drag the hosts file from the desktop into the Notepad window.
  5. Add your entry at the bottom of the file. In this example, I will add the following entry for my local area network:

192.168.1.10     BOBPC

Easily Edit the Hosts File in Windows 11 (Image Credit: Russell Smith)
  1. Press CTRL + S to save the file.
  2. Close Notepad.
  3. Now using the mouse, drag the hosts file on the desktop back to the File Explorer window.
  4. In the Replace or Skip Files dialog, click Replace the file in the destination.
Easily Edit the Hosts File in Windows 11 (Image Credit: Russell Smith)
  1. In the Destination Folder Access Denied dialog, click Continue.
  2. You may be required to provide consent or enter an administrator username and password to complete the operation.
Easily Edit the Hosts File in Windows 11 (Image Credit: Russell Smith)
  1. Open Notepad again by clicking Start on the taskbar, typing notepad, and pressing ENTER.
  2. Drag the hosts file from File Explorer into the Notepad window.
  3. Now you can see your new entry in the hosts file.

Edit the hosts file using the command line

This method uses the command line and is much faster than the method above.

  1. Click the Start menu on the taskbar.
  2. Type command and then making sure Command Prompt is highlighted on the left, click Run as administrator in the pane on the right of the search results.
Easily Edit the Hosts File in Windows 11 (Image Credit: Russell Smith)
  1. Give consent or provide an administrator username and password as prompted.
Easily Edit the Hosts File in Windows 11 (Image Credit: Russell Smith)
  1. In the command prompt window, use the echo command as shown here to add an entry to your hosts file. In this example, I’ll add 192.168.1.12 TOMPC to the hosts file.

echo 192.168.1.12 TOMPC >> %SystemRoot%\System32\drivers\etc\hosts

Easily Edit the Hosts File in Windows 11 (Image Credit: Russell Smith)
  1. To check that the hosts file was updated, use the ‘type’ command as shown below:

type %SystemRoot%\System32\drivers\etc\hosts

Easily Edit the Hosts File in Windows 11 (Image Credit: Russell Smith)

Remove an entry from the hosts file using the command line

The easiest way to remove an entry is to open Notepad from the command line. The command line must be started with administrator privileges as described above.

notepad %SystemRoot%\System32\drivers\etc\hosts

Easily Edit the Hosts File in Windows 11 (Image Credit: Russell Smith)

Notepad will open the hosts file. You can add or remove entries and then save the file to disk.

 

 

 

Related Topics:

BECOME A PETRI MEMBER:

Don't have a login but want to join the conversation? Sign up for a Petri Account

Register
Comments (2)

2 responses to “How to Easily Edit the Hosts File in Windows 11”

  1. Don’t grant yourself permissions to the file–open Notepad as admin instead, make your changes to HOSTS, and save it. Granting standard user permissions to the HOSTS file could be used maliciously without elevation of privilege (though it’s unlikely an attacker would suspect altered permissions).

Leave a Reply

IT consultant, Contributing Editor @PetriFeed, and trainer @Pluralsight. All about Microsoft, Office 365, Azure, and Windows Server.
Live Webinar - Thursday, December 2nd! Active Directory Masterclass: AD Configuration Strategies for Stronger SecurityREGISTER NOW - Thursday, December 2, 2021 @ 1 pm ET

Active Directory (AD) is leveraged by over 90% of enterprises worldwide as the authentication and authorization hub of their IT infrastructure—but its inherent complexity leaves it prone to misconfigurations that can allow attackers to slip into your network and wreak havoc. 

Join this session with Microsoft MVP and MCT Sander Berkouwer, who will explore:

  • Whether you should upgrade your domain controllers to Windows Server
    2019 and beyond
  • Achieving mission impossible: updating DCs within 48 hours
  • How to disable legacy protocols and outdated compatibility options in
    Active Directory

Sponsored by: