Back home

August 11, 2019

Emulating DNS change to test your WordPress website

The hosts file is a file on your computer, used to map hostnames to IP addresses. With the hosts file you can change the IP to which you resolve a certain domain name.

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.

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

sudo nano /etc/hosts

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

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.

From there, you can resolve yourdomain.com to the IP address of your new web server. The entry you should add to your hosts file would be like:

1.2.3.4 yourdomain.com www.yourdomain.com

Save the hosts file when done by pressing Ctrl+O and exit with Ctrl+X. The hostname yourdomain.com should be mapped to the IP address 1.2.3.4.

It might take a little for your browser to access the website, due to caching. You can always try using incognito mode or another browser.