Occasionally, you may need to edit the hosts file on your machine. The host file may have been edited maliciously in an attack or as a joke. Alternatively, you may only want to control access to certain websites or control network traffic.
Computers have had host files since the days of ARPANET. Host files were used to resolve host names for decades before the DNS system was implemented. Since they were used to aid in network name resolution, host files grew into mass documents.
Microsoft, Apple, and
other operating system developers have maintained the hosts file, so it varies very little between Windows, macOS, and Linux. The syntax remains pretty much the same on all platforms. Most host files will have multiple loopback entries. We can use that for the basic example for typical syntax.
The first part will be the location to redirect the address to
, the second part will be the address you want to redirect, and the third part is the comment. They can be separated by a space, but for ease of reading they are usually separated by one or two tabs, or the equivalent amount of spaces.
127.0.0.1 localhosts #loopback Now let’s see how to access hosts files on
different operating systems
. Edit the hosts file in Windows 10 or Windows 11 The
hosts file
is still present in the latest and greatest of Microsoft, Windows 10 and Windows 11, despite being old by computer standards. However, it is not widely used in modern Windows networks.
The hosts file, like other files that can be found in the Windows folder, is protected. You need administrative privileges to move, edit, or delete the file.
RELATED: How to Edit Hosts File in Windows 10 or 11
You can use any plain text editor that you want to modify the hosts file, however, Notepad is perfectly suited for the task. You don’t need to download another one just for this job.
Press the Start button, type “notepad” in the search, then right-click and press “Run as administrator”.
<img src
=”https://www.howtogeek.com/wp-content/uploads/2017/04/launch-notepad-as-administrator-windows-11.png” alt=”” />
Click “File” in the upper left corner, press “Open” and then navigate
to: C:\Windows\System32\drivers\etc
Make sure to change the selection to “All Files (*.*)” instead of “Text Documents (*.txt)”. The hosts file does not have a file extension, so it will not appear if it only shows text documents. After changing those settings, locate the “hosts” file and click open.
You can then edit the hosts file as desired. There are some obvious things to do with it, such as blocking or redirecting internet traffic.
RELATED: How to Use Your Computer’s Hosts File to Block Tons of Malware,, and Other Types of Websites
Editing the hosts file in Windows 8 or 8.1 Editing the hosts file in Windows 8
and 8.1
is basically the same as Windows 10 or 11.
Just search for Notepad,
right-click on Notepad in the list of search results, and then choose “Run as administrator.”
<img
src=”https://www.howtogeek.com/wp-content/uploads/2014/01/img_52e68b62d824b.png” alt=”” />
Once you have opened Notepad, click “File” at the top right, click “Open” and then navigate to the
/etc/ folder: C:\Windows\System32\drivers\etc\hosts
You will be able to edit the hosts file as you would any other file. You can use it to block access to websites, if you want.
Tip:
Windows 7
Access to the hosts file in Windows 7 is the same as in newer versions of Windows. You must run Notepad as an administrator, navigate to the hosts file, and then open it.
There are a few ways to do this. The first is to find Notepad, right-click on it, press “Run as administrator” and then navigate to the hosts file. It is located at
: C:\Windows\system32\drivers\etc\
Alternatively, you can press Windows + R to open a Run window and then enter the following command
: Notepad c:\windows\system32\drivers\etc\hosts
The command will run Notepad as an administrator and automatically open the specified file.
After you open Notepad, you can edit the file. In this example we will block Facebook. To do this, simply enter the following after the # mark.
0.0.0.0 www.facebook.com
Note:
Now that
you have edited your Hosts file, be sure to save it
.
<img
src=”https://www.howtogeek.com/wp-content/uploads/2010/08/sshot20100831205407.png” alt=”” />
Now notice that if we try to access Facebook in IE we cannot reach the page.
<img src="https://www.howtogeek.com/wp-content/uploads/2010/08/sshot20100831205644.png" alt=
“
” />
We couldn’t access it in Google Chrome either… (see endnotes). Also for more information on how to edit your Hosts file, see The Geek’s article on how to create a shortcut to quickly edit your Hosts file.
Edit
the hosts file in Ubuntu
On Ubuntu (and most Linux distributions) you can edit the hosts file directly in the Terminal. You can use your favorite command line editor or your favorite GUI text editor. For this example we will use Vim, since it is one of the traditional text editors in Linux. Like Windows, the Ubuntu hosts file is located in the /etc/ folder, although here it is in the root of the drive. The hosts file is protected and will not be edited by your normal user account.
RELATED: Define a Great Vim Profile Using .vimrc
You’ll need to launch Vim prefaced by sudo, which is conceptually and functionally similar to “Run as administrator” on Windows operating systems. Open a Terminal and then
enter: sudo vim /etc/hosts
You will need to enter your password before the command is executed
.
RELATED: How to Control
Sudo Access on Linux
Note:
Now that it’s open we can edit it to redirect Facebook to nothing. You’ll notice that with Ubuntu there’s also a section for IPv6.
You can ignore the IPv6 section in most cases; simply editing the IPv4 section should do what you need.
Now we can save the file and try to go to Facebook.com. When you set the address in the hosts file to 0.0.0.0, your computer cannot access Facebook and throws an error: “Unable to connect.”
Note
:
RELATED: How to Set DNS Search Order in Ubuntu 18.04 Using NetPlan
Change the hosts file in any version
macOS On macOS, accessing the hosts file is very similar to Ubuntu. Start in the terminal and use your favorite editor: it could even be a GUI-based text editor if you want, but it’s easier to use a command-line editor from the Terminal.
The file
will closely resemble the hosts file found on Windows and Linux. Again we will redirect Facebook to
0.0.0.0.
<img
src=”https://www.howtogeek.com/wp-content/uploads/2017/04/macOS-hosts-file.png” alt=”The MacOS hosts file.” /> Safari,
just like a browser on Windows and Ubuntu, will not be able to connect to Facebook.
Why don’t hosts block file connections?
All major modern browsers offer DNS over HTTPS (DOH). DOH encrypts your DNS requests so they can’t be intercepted and read by third parties who might be snooping; Overall, this is a great thing.
RELATED: How DNS over HTTPS (DoH) Will Increase Online Privacy
For our purposes here, it limits the usefulness of the hosts file. The hosts file can only be used to block Internet traffic if your browser is deferred to the DNS server specified in the Windows operating system. Many browsers will enable DNS over HTTPS automatically and use their own DNS servers. That causes the browser to completely skip the hosts file. It is possible to force your browser to use the DNS server that Windows specifies, but you will have to look in your browser settings.
This should help you start understanding the Hosts file and how it can help protect your computer. Under the right circumstances, you can use it to block sites that you don’t want a PC to be able to access.
RELATED: Stupid Geek Tricks: Create a Shortcut to Quickly Edit Your Hosts File