How to Change Your MacOS Hosts File

Why would any one what do change a hosts file? Better yet, what does it do?

What does a hosts file do?

A hosts file is a locally used text file that:

  • is used for hostname to IP address resolution
  • Also, has priority over using a DNS server for name to IP address resolution

Why would you want to change it?

The example that I just used it for was testing a newly migrated website, before I updated DNS records to point to the newly hosted IP address. It is also helpful when you are using an SSL certificate because it will only show trusted when you use the fully qualified domain name that matches the certificate.

How to Change Your MacOS hosts file

Note this change as done on MacOS High Sierra (10.13)

  • open a terminal window
    • cmd + space
                      
    • type terminal
    • select it
    • press enter

  • in the terminal window type:
    • sudo nano /etc/hosts
    • enter your password
  • Goto the bottom
  • make an entry that in the following format:
    • <ip address> <hostname>
    • example:
      • 1.1.1.1 fake.host.com
  • press ctrl + x
  • press y to save
  • Now, clear your locally cached DNS.
    • In the terminal window type:
      • sudo killall -HUP mDNSResponder
  • All set!

Revert your change

  • open the terminal window again
  • edit the hosts file
    • sudo nano /etc/hosts
  • remove the line that you just added
  • ctrl + x
  • Y to save

About Daniel Fredrick

Technology enthusiast, Programmer, Network Engineer CCIE# 17094

View all posts by Daniel Fredrick →

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.