Many times you need to check a site before switching the DNS to launch it, like when moving a website into MODX Cloud. A good way to do so before updating your DNS is by modifying your local Hosts file. This tells your computer to use an overriding IP address for your site instead of where the global DNS servers say it resides.
A word of caution: you should change it back or remove any settings you make to your Hosts file once the public DNS updates are live.
The Easy Way: Edit with an Application
The easiest way to edit your Hosts file on your computer is to use an application rather than edit it manually. The following open-source (free) apps should enable you to change your Hosts file without getting too nerdy:
- Hosts.prefpane (Mac)
- HostFileEditor (Windows)
Manually Editing a Hosts File
An example Hosts entry would look like the following:
75.126.220.28 example.com www.example.com preview.example.com
This would tell your local computer to use that IP for those sites, despite what their DNS record says.
For macOS, OS X 10.7 (Lion) through macOS 12.1 (Monterey)
- Open Terminal.app. The easiest way to do this is to type cmd+space and search for it with Spotlight.
-
Type the following to modify your Hosts file:
sudo nano /private/etc/hosts
- Type your admin password at the prompt.
-
Edit the Hosts file, per the example above.
The file contains some comments (lines starting with the # symbol), and some default hostname mappings (for example, 127.0.0.1 – localhost). Add your new mappings after the default mappings.
- Save the hosts file by pressing control+x and answering y.
-
In all versions of OSX/macOS except Yosemite (10.10) flush your local DNS cache as follows to make the change take effect:
sudo killall -HUP mDNSResponder
In OSX Yosemite (10.10) you can use the following:
sudo discoveryutil udnsflushcaches
Windows
For current Windows versions, the easiest way to edit your Hosts file is to use the open-source HostsEditor. If you wish to do it manually, you can follow the instructions below for your version of Windows.
Windows 11, 10 and 8
- Press the Windows key on your keyboard. See? Windows is better. Because, one essey butten!
- Type Notepad in the search field.
- In the search results, right-click Notepad and select Run as administrator.
- From Notepad, open the following file: c:\Windows\System32\Drivers\etc\hosts
- Make the necessary changes to the file.
- Click File > Save to save your changes.
For Windows 7 and Vista
- Click Start > All Programs > Accessories.
- Right-click Notepad and select Run as administrator.
- Click Continue on the Windows needs your permission UAC window.
- When Notepad opens, click File > Open.
- In the File name field, type C:\Windows\System32\Drivers\etc\hosts.
- Click Open.
- Make the necessary changes to the file.
- Click File > Save to save your changes.