Methods of payment Abuse

Hosts file: why is it needed, where is it located

31.08.2023, 22:05

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.

Why do I need the hosts file?

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.

Where is it located?

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.

How to make changes to the hosts file

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.

Windows 7

Windows 8, 10

Linux, Unix

macOS

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:

  1. Open a terminal or command prompt in your operating system.
  2. Enter the command `sudo nano /etc/hosts` to open the hosts file with superuser rights.
  3. Enter the superuser password if required.
  4. Scroll through the file to the end or to the desired location.
  5. 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
  6. Save the changes and close the editor (for example, in nano, press Ctrl + O, then Enter, and Ctrl + X to save and exit).
  7. 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.