This is particularly useful when you wish to see how a website will look when hosted on a different server without having to wait for a DNS change to propagate, or avoiding any DNS changes at all.

How to open the hosts file

Depending on the operating system you are using, the hosts file location may differ, and

The path to the hosts file, depending on the operating system you are using, is:

Windows

Assuming the system root is C:, the hosts file is located at the following path:

C:\Windows\System32\drivers\etc\hosts

You can use any text editor you like, such as Notepad, to edit the hosts file. However the editor must be run as administrator, by right clicking the corresponding icon and selecting Run as administrator.

windows-notepad-run-as-administrator

You may also need to change the permissions of the hosts file. The following steps are required to do that:

  1. Right click on the hosts file and click Properties.
  2. Switch to the Security tab and click on the Advanced button.
  3. Navigate to the Owner tab and click Edit.
  4. Highlight your user account or the Administrators group and click OK twice to quit the Advanced Security Settings dialog box.
  5. On the Security tab under the Properties dialog box click Edit.
  6. Highlight the Administrators group, check the box for Full control under Allow and click OK.

macOS

In macOS, the hosts file is located at /etc/hosts. You can edit it directly in the Terminal using any text editor, such as Vim. It is important to open the hosts file as root. Open a terminal window and type the following:

sudo vim /etc/hosts

You will be prompted to enter your password to run the command above as root.

Linux

As in macOS, the hosts file is located at /etc/hosts in Unix and Unix-like operating systems. Similarly, the hosts file can be edited in the terminal with your text editor of preference as root, like this:

sudo vim /etc/hosts

You will be prompted to enter your password to run the command above as root.

Editing the hosts file to resolve domains to IP addresses

The hosts file contains lines of text consisting of an IP address in the first text field followed by one or more hostnames. Each field is separated by white space.

In our example, let’s resolve yourdomain.com to the IP address 1.2.3.4. In this case the entry you should add to your hosts file would be:

1.2.3.4 yourdomain.com www.yourdomain.com

Save the hosts file when done. The hostname yourdomain.com should be mapped to the IP address 1.2.3.4.