How to Temporarily Point a Website to a Different IP Address Using the macOS Hosts File Print

  • mac hosts file, temporary DNS override mac, macOS hosts file, edit hosts file mac, website testing mac, preview website on new server mac, point domain to IP mac, flush DNS cache mac
  • 0

The macOS hosts file allows you to temporarily direct a domain name to a specific IP address on your own computer without changing the website’s public DNS records.

This is commonly used when testing a website migration, previewing a website on a new server, or checking changes before DNS updates are made live.


What does the hosts file do?

Normally, your Mac uses DNS servers to determine which IP address a website should load from.

The hosts file allows you to override this locally on your own Mac only.

  • The change only affects your Mac.
  • Public DNS is not changed.
  • Other visitors will still see the live website normally.
  • You can remove the change at any time.

Before you begin

You will need:

  • The domain name you want to test.
  • The IP address of the server you want the domain to load from.
  • An administrator account on your Mac.

Example:

192.0.2.123 example.com
192.0.2.123 www.example.com

Step 1: Open Terminal

  1. Open Finder.
  2. Go to Applications.
  3. Open the Utilities folder.
  4. Open Terminal.

Step 2: Edit the hosts file

In Terminal, run the following command:

sudo nano /etc/hosts

Press Enter.

You will be prompted for your Mac administrator password. Type the password and press Enter.

Note: The password will not visibly appear while typing. This is normal behaviour in Terminal.


Step 3: Add the IP address and domain

Use the arrow keys to move to the bottom of the file and add your entries.

Example:

192.0.2.123 example.com
192.0.2.123 www.example.com

Replace the example IP address with the server IP address you have been provided.


Step 4: Save the file

After adding the entries:

  1. Press Control + O to save the file.
  2. Press Enter to confirm.
  3. Press Control + X to exit Nano.

Step 5: Flush the DNS cache

After saving the hosts file, clear the macOS DNS cache by running:

sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

Press Enter.

You may be prompted for your password again.


Step 6: Test the website

Open your web browser and visit the domain name.

Your Mac should now load the website from the IP address you entered in the hosts file.

If the website still loads from the old server:

  • Close and reopen your browser.
  • Try a private/incognito window.
  • Clear your browser cache.
  • Double-check the IP address and domain entries.

How to remove the hosts file entry

Once testing is complete:

  1. Open Terminal again.
  2. Run:
sudo nano /etc/hosts
  1. Remove the lines you added.
  2. Save the file using Control + O.
  3. Exit using Control + X.
  4. Flush DNS again using:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

Your Mac will then return to using the normal public DNS records.


Example hosts file entry

If your new server IP is 203.0.113.50 and your domain is yourdomain.com.au, you would add:

203.0.113.50 yourdomain.com.au
203.0.113.50 www.yourdomain.com.au

Troubleshooting

Permission denied

Make sure you use sudo when opening the hosts file.

The website still loads from the old server

Flush the DNS cache and restart your web browser.

The website shows an SSL warning

The SSL certificate may not yet be installed on the server you are testing.

The website does not load

Check that the IP address is correct and that the hosting server is configured for the domain.


Need help?

If you are unsure which IP address to use or need assistance testing your website before DNS changes, please contact our support team.


Was this answer helpful?

« Back