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:
-
Click Start, type
notepad
, and then click Run as administrator. -
Click Yes to allow the app to make changes to your device.
-
On the File menu, click Open.
-
In the Open dialog box, in the location bar, type
\%SystemRoot%\system32\drivers\etc
and then press Enter. -
In the Text Documents (*.txt) list box, select All Files (*.*).
-
In the list of files, double-click hosts.
-
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.
-
To save your changes, press Ctrl-S.
Apple macOS
To edit the hosts file on macOS, follow these steps:
-
Open a terminal window. In the dock, click the Launchpad icon, and then in the Search box, type
terminal
and press Return. -
At the command prompt, type the following command, and then press Return:
sudo nano /etc/hosts
-
Type your password.
-
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.
-
To save your changes, press Ctrl-O, and then press Return.
-
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:
-
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
-
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.
-
Save your changes to the /etc/hosts file.
Related articles
Updated 3 days ago