February 4, 2019
February 4, 2019
By editing Mac hosts file, it’s possible to emulate DNS change and set the desired IP for a domain name. With the hosts file you can overwrite any DNS values set by your Internet service provider. This is helpful if your domain name is not yet registered or not pointed to a hosting account, but you want to preview your website.
In this tutorial you will learn how to edit the hosts file on Mac. For a tutorial on how to do the same on Windows click here.
IMPORTANT: Note that changes made for the hosts file will affect your computer only.
Before you begin this guide, you’ll need the following:
Follow these steps to open your hosts file on Mac:
terminalin the search field. Click on the Terminal icon.
sudo nano /private/etc/hosts
In order to emulate DNS change and point domain name to an IP address, use the following syntax:
IP_Address domain.com IP_Address www.domain.com
For example, to emulate DNS change for hostinger.com and www.hostinger.com we would have to include the following lines at the bottom of hosts file.
18.104.22.168 hostinger.com 22.214.171.124 www.hostinger.com
This will emulate change IP address of the hostinger.com and www.hostinger.com to 126.96.36.199.
Once you are done with editing hosts file, press CMD + X on your keyboard, enter Y to save changes and hit ENTER button.
Sometimes changes do not apply instantly and you may need to flush the DNS cache. On Mac, DNS cache can be easily flushed by executing the following command:
Mac OS X Snow Leopard
sudo dscacheutil -flushcache
OS X Mavericks, Mountain Lion, and Lion
sudo discoveryutil mdnsflushcache
That’s it, by finishing this short tutorial, you have learned how to edit a hosts file on Mac. This is useful if your domain name is not registered or not pointed to the server, but you want to preview your website. In addition, you have also learned how to flush the DNS cache on Mac.