hosts is a text file that is used in operating systems to communicate between domain names and IP addresses. It represents the local distribution of the domain system and allows redirecting requests to specific IP addresses, bypassing the usual DNS servers.
hosts is often used to configure local test servers, block access to certain sites or resources, as well as to solve problems with DNS queries.
When you enter a domain name in a web browser or other application, the operating system first checks the hosts file for the presence of the corresponding entry. If the record is found, the IP address specified in the hosts file will be used to connect to the server.
It is usually located in a system directory such as /etc/hosts in the most common Linux and macOS distributions and C:\Windows\System32\drivers\etc\hosts in Windows.
Windows XP, 2003, Vista, 7, 8, 10 — c:\windows\system32\drivers\etc\hosts
Linux, Ubuntu, Unix, BSD — /etc/hosts
macOS — /private/etc/hosts
You need to edit the etc hosts file on behalf of the administrator, otherwise you will not be able to save the changes you have made.
To make changes to the hosts file, you will need administrator or superuser rights. Below are instructions for making changes to the hosts file on various operating systems:
Windows:
Open File Explorer and follow the path C:\Windows\System32\drivers\etc
Find the file named "hosts" and open it using a text editor (for example, Notepad) with administrator rights.
Make the necessary changes by adding lines in the format "domain name IP address", for example: "127.0.0.1 localhost"
Save your changes and close the file.
Linux and macOS:
Open the terminal.
Run the command `sudo nano /etc/hosts` to open the hosts file with superuser rights and the nano editor (you can use any other text editor).
Make the necessary changes by adding lines in the format "domain name IP address", for example: "127.0.0.1 localhost"
Press Ctrl + X, then Y, and then Enter to save your changes and close the file.
After making changes to the hosts file, some operating systems may need to reboot or reset the DNS cache to apply these changes.
Open File Explorer and navigate to the following path:
C:\Windows\System32\drivers\etc
In this folder, find the file named "hosts". To open a file with administrator rights, right-click on it and select "Open with" > "Notepad".
Make the necessary changes to the file. Each entry should be on a separate line and have the following format:
IP address of the domain name
For example:
127.0.0.1 localhost
Save the changes by clicking on the "File" button in the upper left corner of notepad, selecting "Save".
To edit the hosts file in Windows 7, you will need administrator rights, so make sure that you perform this operation on behalf of a user with the appropriate privileges.
Open File Explorer and follow the path:
C:\Windows\System32\drivers\etc
In this folder, find the file named "hosts". Right-click on it and select "Open with" > "Notepad".
If Notepad is not listed, click on the "More Apps" button and select Notepad.
If you cannot save the changes due to access restrictions, copy the "hosts" file to your desktop. Edit it on your desktop using notepad.
Make the necessary changes to the file. Each entry must be in a separate line and have the following format:
IP address domain name
For example:
127.0.0.1 localhost
Save the changes. If you edited the file on the desktop, drag it back to the directory "C:\Windows\System32\drivers\etc " and agree to replace the existing file.
To edit the hosts file on Linux or Unix, you can use any text editor, such as vi, nano or gedit.
Open the terminal.
Enter the command to open the hosts file using the text editor you need. For example:
For vi: `sudo vi /etc/hosts`
For nano: `sudo nano /etc/hosts`
For gedit: `sudo gedit /etc/hosts`
Enter the superuser password (root) to access the file editing.
Add or edit entries in the hosts file. Each entry must contain an IP address and its corresponding domain name, separated by a space or tab.
For example:
127.0.0.1 localhost
192.168.0.1 example.com
Save the changes and close the editor. In the case of vi, you will need to press `Esc`, then type `:wq` and press Enter. In nano, you need to press Ctrl + X, then Y to save the changes.
Check the hosts file to see that the changes you have made have taken effect. You can use the `cat /etc/hosts` command to view the contents of the file.
To edit the hosts file, you will need superuser rights. Therefore, using the `sudo` command before opening the file will help you get the necessary privileges.
Open Finder and navigate to the "Applications" folder.
In the "Applications" folder, find and run the "Terminal" program.
In the terminal window, enter the following command: `sudo nano /etc/hosts`
Enter the password of your macOS account and press the "Enter" key.
In the nano editor that opens, you will see the contents of the hosts file.
Add or edit entries in the hosts file. Each entry must contain an IP address and its corresponding domain name, separated by a space or tab.
For example:
127.0.0.1 localhost
192.168.0.1 example.com
Press "Ctrl +O", then "Enter" to save the changes.
Then press "Ctrl +X" to close the nano editor.
To edit the hosts file, you will need superuser rights. Therefore, using the `sudo` command before opening the file will help you get the necessary privileges.
Blocking access to the site via the hosts file
To block access to a specific site through the hosts file, you need to make changes to this file by redirecting the requested web address to a local IP address or an incorrect IP address.
Here's how it can be done:
- Open a terminal or command prompt in your operating system.
- Enter the command `sudo nano /etc/hosts` to open the hosts file with superuser rights.
- Enter the superuser password if required.
- Scroll through the file to the end or to the desired location.
- Add a new line to the file, starting with the IP address followed by the domain name you want to block. You can use 127.0.0.1 (local IP address) as the IP address, or you can specify an incorrect IP address. For example: 127.0.0.1 example.com or 0.0.0.0 example.com
- Save the changes and close the editor (for example, in nano, press Ctrl + O, then Enter, and Ctrl + X to save and exit).
- Restart your browser or clear its cache to update the settings using the new hosts file.
After performing these actions, your computer will redirect requests to the specified site to a local IP address or an incorrect IP address, which will make it impossible to access this site.
The original hosts file and how to restore it
The original hosts file in the Linux, Unix or macOS operating system contains a minimal set of entries, usually only for access to local resources, without blocking access to any websites.
The contents of the original hosts file for Windows:
# Copyright (c) 1993-2006 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 handle within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
For Linux:
127.0.0.1 localhost
127.0.1.1 user
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
Mac:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
Please note that when restoring the original hosts file, all the settings you added (including blocking access to sites) will be deleted.