Sometimes you may find you need to access a domain which you have not yet configured DNS for. You can use your computer’s Hosts file to direct it to the correct IP address of the domain you are working on.
Modifying Your Hosts File in Windows
The Hosts file in Windows is conveniently located in C:\Windows\System32\drivers\etc\hosts.
You can open this file in Notepad or a text editor of your choice. You will need to be running Notepad as an Administrator in order to edit the file.
Right Click the Notepad icon in your Start Menu or Desktop. Click Run as administrator.
If prompted, click Yes to allow the program Administrative access.
Once in Notepad, click File → Open.
Navigate to C:\Windows\System32\drivers\etc\. You may need to select All Files in the dropdown next to the filename in order for the files to show. Select hosts and click Open.
You should now see a file that looks like this:
Add in the IP address of your server and the domain you would like to access. Add both the www and non-www versions for greater flexibility.
Click File → Save to save your changes. You should now be able to access your domain at the IP address you assigned.
Modifying Your Hosts File in MacOS / Linux
Both Linux and MacOS locate the hosts file in /etc/hosts. MacOS in particular keeps this location hidden in the graphical interface, so you will need to edit the file from a command line. Assuming Linux users know how to access their terminal, this tutorial will be written with the MacOS interface in mind.
Click the spyglass icon in the upper right corner to open Finder. Type Terminal in the search box and press Return to open it.
Once open you will see a command line. Type sudo nano /etc/hosts and enter your administrative password if prompted to open the file.
Use the arrow keys to navigate and insert text. The “^” in the commands at the bottom corresponds to the Ctrl key on the keyboard. Add in the IP address of your server and the domain you would like to access. Add both the www and non-www versions for greater flexibility. Press Ctrl+O to write the file. Press Ctrl+X to exit.
You should now be able to access your domain at the IP address you assigned.
Pro Tip: Use vim instead of nano.
Vim is a more powerful command line editor that is much faster than nano once you get the hang of it. To use it for this task type sudo vim /etc/hosts and enter your administrative password if prompted to open the file.
Use the arrow keys to navigate in vim. To insert text press the Insert key (fn+i on Mac). To replace text press Insert twice (fn+i twice on Mac). Press Esc to exit Insert/Replace mode. Type :w and press Return to write changes and :q to quit. :wq writes and quits in the same command. :q! exits without saving.
We recommend testing vim on a test file before using it to edit an important file such as hosts, however once you’ve got some practice with it, you will find you can edit files much faster than you would using nano.