Editing the hosts file on Windows, macOS, and Linux

A hosts file contains mappings of hostnames to IP addresses. This article explains how to edit the hosts file on Microsoft Windows, Apple macOS, and Linux.

A hosts file contains mappings of hostnames to IP addresses. It is frequently used to test a website when the domain's DNS settings have not been updated yet.

This article explains how to edit the hosts file on the following operating systems:

  • Microsoft Windows
  • Apple macOS
  • Linux

🚧

Important

The hosts file is a plain text file, so you should use a plain text editor like Notepad or nano to edit it. Do not use a word processor like Microsoft Word, which can add additional formatting and cause more problems than it solves!

Microsoft Windows

To edit the hosts file on Microsoft Windows, follow these steps:

  1. Click Start, type notepad, and then click Run as administrator.

  2. Click Yes to allow the app to make changes to your device.

  3. On the File menu, click Open.

  4. In the Open dialog box, in the location bar, type \%SystemRoot%\system32\drivers\etc and then press Enter.

  5. In the Text Documents (*.txt) list box, select All Files (*.*).

  6. In the list of files, double-click hosts.

  7. Move the cursor to the end of the file, and then type the following text. Replace xxx.xxx.xxx.xxx with the IP address you want to use, and replace example.com with the domain name:

    xxx.xxx.xxx.xxx example.com
    

    🚧

    Important

    There must be at least one space between the IP address and the domain name.

  8. To save your changes, press Ctrl-S.

Apple macOS

To edit the hosts file on macOS, follow these steps:

  1. Open a terminal window. In the dock, click the Launchpad icon, and then in the Search box, type terminal and press Return.

  2. At the command prompt, type the following command, and then press Return:

    sudo nano /etc/hosts
    

  3. Type your password.

  4. In the nano editor, move the cursor to the end of the file, and then type the following text. Replace xxx.xxx.xxx.xxx with the IP address you want to use, and replace example.com with the domain name:

    xxx.xxx.xxx.xxx example.com
    

    🚧

    Important

    There must be at least one space between the IP address and the domain name.

  5. To save your changes, press Ctrl-O, and then press Return.

  6. To exit the nano editor, press Ctrl-X.

    🚧

    Important

    To ensure the new hostname mapping works correctly, you should also clear the DNS cache. For information about how to do this, please see Clearing the DNS cache on your computer.

Linux

To edit the hosts file on Linux, follow these steps:

  1. In your preferred text editor, open the /etc/hosts file with administrator privileges. For example, at the command prompt, type the following command:

    sudo vi /etc/hosts
    
  2. Move the cursor to the end of the file, and then type the following text. Replace xxx.xxx.xxx.xxx with the IP address you want to use, and replace example.com with the domain name:

    xxx.xxx.xxx.xxx example.com
    

    🚧

    Important

    There must be at least one space between the IP address and the domain name.

  3. Save your changes to the /etc/hosts file.

Related articles