How to Edit Your Hosts File

Posted 

3 min read

The hosts file is used to map domain names (hostnames) to IP addresses. It is a plain-text file used by all operating systems including Linux, Windows, and macOS. The hosts file allows you to set the IP address of a given domain name. This will affect only the computer on which the change is made, rather than how the domain is resolved worldwide.

The hosts file has priority over DNS. When you type in the domain name of a web site you want to visit, the domain name must be translated into its corresponding IP Address. The operating system first checks its hosts file for the corresponding domain and if there is no entry for the domain, it will query the configured DNS servers to resolve the specified domain name.

This is particularly useful if you want to test your website without changing the domain DNS settings. For example, you are migrating your website to a new server and you want to verify whether it is fully functional before pointing the domain to the new server. The hosts file can also be used to block websites on your computer.

In this article, we’ll provide instructions about how to modify the hosts file on Linux, macOS, and Windows.

Hosts File Format

Entries in the hosts file have the following format:

IPAddress DomainName [DomainAliases]

The IP address and the domain names should be separated by at least one space or tab. The lines starting with # are comments and are ignored.

To add an entry to the hosts file, simply open the file in your text editor. Below is a sample hosts file:

# Static table lookup for hostnames.
# See hosts(5) for details.

127.0.1.1 linuxize.desktop linuxize
127.0.0.1 localhost

The hosts file changes take effect immediately except in cases where the DNS entries are cached by applications.

Modify Hosts File in Linux

In Linux, the hosts file is located at: /etc/hosts. The instructions below are valid for Linux distribution, including Ubuntu, CentOS, RHEL, Debian and Linux Mint.

  • In your terminal window, open the hosts file using your favorite text editor:

    sudo nano /etc/hosts

    When prompted enter your sudo password.

  • Scroll down to the end of the file and add your new entries:

  • Save the changes.

Modify Hosts File in Windows

In Windows, the hosts file is located at: c:\Windows\System32\Drivers\etc\hosts. The instructions below are valid for Windows 10 and Windows 8.

  • Press the Windows key and type Notepad in the search field.
  • Right-click on the Notepad icon and select Run as administrator.
  • In Notepad, click File then Open. In the File name field, paste c:\Windows\System32\drivers\etc\hosts
  • Scroll down to the end of the file and add your new entries:

  • Save the changes by clicking File > Save

Modify Hosts File in macOS

In macOS, the hosts file is located at: /etc/hosts. The instructions below are valid for all macOS versions.

  • In your terminal window, open the hosts file using your favorite text editor:

    sudo nano /etc/hosts

    When prompted enter your administrative password.

  • Scroll down to the end of the file and add your new entries:

  • Save the changes and flush the DNS cache:

    dscacheutil -flushcache

Conclusion

By now you should have a good understanding of how the hosts file works and how to make modifications to the file. If you want to undo the changes, simply open the file and remove the lines you added.