How to Edit your Hosts File on a Mac

Editing the hosts file on a Mac allows you to manually override the domain name system (DNS) for a domain on your computer. Here are the steps to edit the hosts file on a Mac:

  1. Open the Terminal application on your Mac. You can find it in the Utilities folder within the Applications folder, or you can use Spotlight to search for it.
  2. Type the following command to open the hosts file in the Nano text editor:
arduino
sudo nano /private/etc/hosts
  1. Enter your password when prompted. Note that when you type your password, you won’t see any characters on the screen. This is normal.
  2. Use the arrow keys to move the cursor to the line where you want to add an entry to the hosts file.
  3. Type the IP address followed by the domain name that you want to map to that IP address. For example, if you want to map “example.com” to the IP address “192.0.2.1”, you would type:
192.0.2.1 example.com
  1. Press the “Control” and “O” keys to save the changes to the hosts file.
  2. Press the “Control” and “X” keys to exit the Nano text editor.
  3. Flush your DNS cache by typing the following command in the Terminal:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
  1. Your changes to the hosts file should now take effect. You can test by opening a web browser and entering the domain name you added to the hosts file. It should resolve to the IP address you specified.

Note that editing the hosts file can have unintended consequences if not done properly, so be careful when making changes. Also, remember to remove any entries you added to the hosts file once you no longer need them.