With some basic technical knowledge, you can easily modify the hosts file on your PC or Mac to block access to certain websites from inexperienced users, such as children.

With some basic technical knowledge you can easily modify the

With some basic technical knowledge, you can easily modify the hosts file on your PC or Mac to block access to certain websites from inexperienced users, such as children.

You probably don’t know it exists, but the hosts file, nestled in the heart of Windows and macOS alike, plays a very important role when your computer connects to a network in general, and the Internet in particular. It is in fact this file that the operating system first consult when it has to connect to a “host” – in the clear, an external resource such as another computer, a server or a website.

What is the hosts file for?

The hosts file behaves like a directory, by matching a domain name (for example, commentcamarche.net) to an IP address (a numeric address in the form 195.248.251.112) which is used to identify a machine on a network.

In practice, the hosts file is a simple local text file that lists these matches. Its origin goes back to Arpanet, the ancestor of the Internet. Given its necessarily finite size, it quickly showed its limits and the role of the Internet’s universal directory was entrusted to DNS (Domain Name Service) servers which perform the same work, but remotely (see our practical sheet Change DNS server on PC and Mac to learn more on the subject).

But the hosts file is still present on our computers. And it’s always queried first, before DNS servers – it’s only if the operating system finds nothing there that it uses DNS. And if its role is limited, it is still of some interest because it acts as a filter. Indeed, it is possible to modify it very easily to confuse or even prohibit certain IPs. This is often useful for developers who want to test on their computer, locally, the website or the server they are working on. But it is also useful in business, to block access to certain sites. Similarly, in a family setting, it is thanks to the hosts file that it is possible, among other things, to prevent children from accessing particular websites, for example, by matching their names. domain name to a fictitious IP address. It suffices to indicate that the youtube.com site uses the IP address 127.0.0.0 (which corresponds to the computer itself) or, better still, the IP address 0.0.0.0 (which does not exist) to prohibit access to Google’s video platform! Convenient to prevent a beginner from going to inappropriate websites without resorting to a parental control system, always complex.

The hosts file is very easy to edit since it is a simple plain text file, which you can open with a simple editor. However, do not write anything there because you could prevent your computer from accessing anything. It is therefore prudent to make a backup copy before any modification. But if you know what you are doing, you can easily use it as a top filter to block certain sites or online services. Certainly, an informed user will quickly discover the trick. But he will not be able to modify the hosts file himself to restore it to its original state if he does not have administrator rights on the computer – which is often the case in companies. As for the others, they will simply be blocked, without understanding anything! You can also modify the hosts file to make jokes to friends for example by directing Google to a completely different site …

Note also that in the event of an abnormal connection problem to certain sites, it may be useful to consult the hosts file, to see if it has not been modified by malware or a pirate …

Example of an original hosts file on Windows

 Copyright (c) 1993-1999 Microsoft Corp.                 
#                 
# Ceci est un exemple de fichier HOSTS utilisé par Microsoft TCP/IP                 
# pour Windows.                 
#                 
# Ce fichier contient les correspondances des adresses IP aux noms d'hôtes.                 
# Chaque entrée doit être sur une ligne propre. L'adresse IP doit être placée                 
# dans la première colonne, suivie par le nom d'hôte correspondant. L'adresse                 
# IP et le nom d'hôte doivent être séparés par au moins un espace.                 
#                 
# De plus, des commentaires (tels que celui-ci) peuvent être insérés sur des                 
# lignes propres ou après le nom d'ordinateur. Ils sont indiqués par le                 
# symbole '#'.                 
#                 
# Par exemple :                 
#                 
#      102.54.94.97 rhino.acme.com          # serveur source                 
#       38.25.63.10 x.acme.com              # hôte client x                 

127.0.0.1 localhost

Where can I find the hosts file in Windows or macOS?

On PC as on Mac, the hosts file is well hidden. You will also need administrator rights to modify it.

Find the Host file on Windows

  • Open a File Explorer window, then successively open the folders This PC, VS:, Windows, System32, drivers and finally etc. The hosts file is located in the latter.

Find the hosts file on macOS

  • From Finder, pull down the menu Go and choose Go to folder.
23817594
  • In the window that appears, indicate the path / private / etc / hosts and validate. A Finder window opens and the hosts file is already selected.
23817596

Even if all the operations carried out on the hosts file remain reversible, it is always advisable to make a copy in order to put everything back in order and quickly, in the event of a problem. You can then modify it as you wish.

Edit the hosts file in Windows

  • Access the hosts file as described above. Press the key Ctrl keyboard, then move with the mouse, the hosts file on the Desktop in order to duplicate it there. You now have a backup copy of the hosts file.
23817598
  • Click in the Windows search box and type Notepad. Right-click on the first icon that appears and choose Execute as administrator.
23817600
  • The Notepad app opens. Expand the menu File and click on To open. Navigate through your PC’s tree view to access the etc folder which contains the original hosts file. Click on the drop-down menu Text files (* txt) at the bottom right and choose All the files to bring up the hosts file.
23817602
  • Select it and validate by To open. The hosts file appears in Windows Notepad. It includes an example of what is possible to do there, written in English.
23817603
  • Click below the last line and add the following information, for example:

    0.0.0.0

    www.youtube.com

  • Warning: separate 0.0.0.0 from www.youtube.com by a tabulation!
23817604
  • Press the keys Ctrl + S to save your changes and close Notepad. Open your usual web browser and try to access Youtube.com. You get an error message.
23817605
  • To restore normal operation, you just have to open the hosts file again and delete the line concerning YouTube.

Edit the hosts file on macOS

  • Access the hosts file as described above. Press the key Alt keyboard then move with the mouse, the hosts file on the Desktop in order to duplicate it there. You now have a backup copy of the hosts file.
23817606
  • On macOS, the hosts file cannot be handled so easily. You have to go through the Terminal. From the Finder, click on the menu Go and choose Utilities.
23817607
  • Launch the Terminal application which is located in this folder.
23817608
  • Now enter the following command and validate by pressing the key Entrance of the keyboard :

sudo nano / etc / hosts

23817617
  • The term Password is displayed. Enter your user session password (be careful, nothing is displayed while typing) and validate.
23817647
  • The contents of the hosts file are displayed. Using the key down arrow on the keyboard, move the cursor below the last line.
23817673
  • Specify, for example, the following information:

0.0.0.0 www.youtube.com

  • Warning: use the upper row of keys on the keyboard (while holding down the Uppercase to enter numbers, not the numeric keypad to the right of the keyboard. Also, separate 0.0.0.0 from www.youtube.com by a tabulation.
23817674
  • Then press the keys Ctrl + X then Y and finally Entrance to save and exit the modification.
  • Open your usual web browser and try to access Youtube.com. You get an error message.
23817675
  • To restore normal operation, you just have to open the hosts file again and delete the line concerning YouTube.

ccn5