How to change Mac's Hosts file – 5 step guide

889.4K views
9 min read

If you're in the process of migrating your website to a new server or aiming to shield your system from cyber threats like malware attacks, you might find yourself considering edits to your Mac hosts file.

Luckily, if you are working on a Mac, editing your hosts file is easier than you might think. Let’s explore how to identify the host file location and demonstrate how to efficiently make the required adjustments in a matter of minutes. 

What is the hosts file on a Mac, and why is it important

Your Mac hosts file is essentially a plain text document that your operating system utilizes to correlate domain names with specific IP addresses. Instead of conducting a conventional lookup to find the IP address of a particular host or domain name, your OS refers to the IP address specified in the hosts file. The hosts file plays an important role in network administration. By bypassing DNS servers, it allows for quick and reliable domain name resolution while also enabling custom network configurations.

Why should you consider altering the hosts file?

Now, why might you want to consider editing the hosts file?

There could be scenarios where it becomes necessary. For instance, during the migration of your website to a new server, modifying the hosts file to direct to a different IP address enables you to establish a staging environment without impacting existing DNS records.

Moreover, adjusting the hosts file on your Mac can serve to block malicious websites, fortifying your system against malware and other cyber threats.

There may be cases in which you would want to edit your hosts file to point to a different TCP/IP address.

How to locate and edit the hosts file on macOS

So, where is the hosts file? The hosts file is located in the /etc/hosts folder on your Mac. 

Despite sounding daunting, identifying the hosts file location and making modifications is a swift and uncomplicated process. I'll guide you through it in five simple steps.

To locate the Hosts file on Mac:

  1. Select Go > Go to Folder… from the Mac menu bar
  2. Type in /private/etc/hosts
  3. Click Go

    locate Hosts files

You should end up within the "etc" folder and have the Mac Hosts file highlighted. 

Step 1: Prepare to edit the hosts

First, it's advisable to set up a Virtual Private Network (VPN) to improve your security measures. My recommendation is to download ClearVPN and establish a connection to a VPN in your preferred location. It's a seamless process that takes just a few seconds. Simply choose a location or let the app pick one for you, then click the activate button to connect to a new server automatically.

change location with ClearVPN

You also want to make backups of the Mac hosts file before editing in case errors occur. That way, you can safely revert to the previous version and undo any mistakes. Just copy the document onto an external hard drive or make a local backup, and then you can confidently experiment with the Hosts file without the fear of breaking anything.

I like to use Get Backup Pro for this because it’s easy to create encrypted backups of all your sensitive data – or just a few selected files that you need right now. And even if something happens to your machine, you can still recover your files on any Mac that has the software installed.

Step 2: Access your Mac terminal

Once you have a secure connection, you’ll want to identify your host file location by accessing Terminal on your Mac.

You can use a built-in one, but for constant work, I recommend using Core Shell. This app comes with built-in Secure File Transfer Protocol (SFTP) capabilities so you can manage all your hosts remotely from within the app. You can also restore your session if your connection fails so you don’t lose all your work. In Core Shell, all your saved hosts files are right at your fingertips. Just select the one you want to edit.

CoreShell Terminal alternative tool

Step 3: Add the host entry

Here is how to add the host entry in Mac:

  1. Open Terminal.
  2. Type sudo nano /etc/hosts and press the Return key.
  3. Enter your admin password.

Now you'll be taken to the text editor, where you can edit your Mac's hosts file. 

nano terminal enter hosts file

Step 4: Edit the host file

The next steps will depend on what exactly you are trying to do with the Mac hosts file. But for now, I will assume you want to add a new domain.

  1. Use the Return key to create some space below the existing records.
  2. Type in the TCP/IP address.
  3. Press the Tab key.
  4. Enter the domain name.
  5. Save the file by pressing Ctrl + O.
  6. Exit with Ctrl + X.

And that’s it! You have successfully altered your Mac hosts file. But just to make sure your edits are being saved properly, I want to add one final step to this process: clearing your DNS cache.

Step 5: Clear your DNS cache

Changes won’t always go live even after they’ve been saved. I recommend clearing your DNS cache to remove saved IP records after you’ve finished editing the host file. 

To do so, just enter the following command into the Terminal or within Core Shell before exiting: sudo killall -HUP mDNSResponder and hit the Return key to clear your Mac's DNS cache and prevent any clashes between DNS and the Hosts file.

nano dns tcp ip address change

The changes should take effect immediately. Using the Mac Hosts file this way you can literally input any TCP/IP you need, from other websites to your own development server to your Mac (just type in 127.0.0.1), which can be useful when you want to avoid using social media or other distractions. You could also outright block malicious websites with a 0.0.0.0 address.

SSH Config lets you manage multiple connections with different settings and easily switch between them. This helps to reduce the potential for errors when you find yourself switching between different hosts or managing complex SSH settings.

SSH Config Editor

How to fix a malfunctioning Mac hosts file

As I mentioned, you may find that your Mac hosts file is malfunctioning. There are lots of reasons why this can happen but the important part is that you can fix it, so don’t dwell on the reasons why for too long. 

Sometimes when you add too many entries to your Hosts file Mac might not process them correctly. It could be that some of the TCP/IP addresses are responding to the same domain name or you've deleted some other essential information.

Let’s focus on the fixes: 

  • Check the file permissions to ensure that the hosts file has the correct read and write permissions. 

  • Check syntax and formatting to ensure each entry follows the correct format: IP_address hostname. 

  • Clear the DNS cache to ensure that the system recognizes the updates.

  • Check the network configuration to ensure that your network settings are correctly configured and that there are no conflicts that might override the hosts file.

  • Ensure that your router or firewall settings are not blocking access to specific domains or IP addresses.

  • Reset hosts file to default by opening it in a text editor and restore it to its default configuration:

    ##
    # Host Database
    #
    #
    # localhost is used to configure the lookback interface
    # when the system is booting. Do not change this entry.
    ##
    127.0.0.1   localhost
    255.255.255.255   broadcasthost
    ::1    localhost

    While this might or might not work, there is a better preventative way of ensuring you don't somehow crush your system — creating consistent backups.
  • Scan for malware by using tools like CleanMyMac X to target adware and spyware specifically.

  • Review and restore backups. If you have a backup of the hosts file, restore it to a previous functional state.

If something does go wrong while editing the Mac hosts file and you accidentally delete or corrupt the file, you can use Disk Drill to recover your data. All you have to do is open the Disk Drill app, choose the disk where the backup file is located (typically your main macOS drive), and perform a scan for deleted files. 

Disk Drill storage devices

Tips to protect your hosts file on Mac

You always want to protect your Mac hosts file from unauthorized changes. This helps to reduce the risk of phishing attacks or malware infections. 

The first thing you want to do is make sure that only administrative accounts have permission to edit the hosts file. This prevents unauthorized changes by standard user accounts or by hackers who have taken over those accounts. 

Next, you want to create regular backups of your host file to ensure it can be restored if it becomes corrupted or modified by an unauthorized account. As I mentioned earlier, you can create automatic backups using software such as Get Backup Pro.

As an extra precaution, you want to use antivirus and anti-malware software to monitor changes to the hosts file. Schedule regular system scans with CleanMyMac X to detect and remove any malicious software that could alter the hosts file.

CleanMyMacX makes it easy to run instant system cleanups and remove malware with just a few clicks. Just select Malware Removal Scan from the sidebar and the software will detect and remove any threats on your machine. I recommend doing this at least once a week.

CleanMyMac X Malware removal scan complete

Modify hosts file in Mac easily with top apps

Making changes to your Mac hosts file doesn’t have to be a massive undertaking. You have a few different options for tackling this head on and you can use software like Core Shell, Get Backup Pro and CleanMyMac X to make parts of the process even easier.

If you want to give these tools a shot, they are available through Setapp. You can start with a 7-day trial to explore the different features and capabilities and upgrade later on if you find them useful.

FAQs

Where can I find the host file?

The hosts file is located in the /etc/hosts folder on your Mac. You can access it by opening the Core Shell app and selecting the hosts file you want to edit.

Is it dangerous to edit your hosts file?

It is not dangerous to edit your hosts file. Any changes made can be easily reversed by restoring the file to its last functional state. However, even in this case, there are some risks involved. For example, you might mistakenly run your test scripts against production on a new Mac. Or, you could use someone else's Mac for testing, not knowing that they don't have their Hosts file modified or vice versa. So make sure you're very careful here. 

When your sole purpose for tinkering with the Hosts file on Mac is being haunted by adware, spyware, or something of that type — relax — there's a much better way to get rid of those intruders.

Do you need to reboot after changing the hosts file?

No. You do not need to reboot after editing the hosts file.

How to clear the cache in the hosts file?

In Terminal, type sudo killall -HUP mDNSResponder. Then hit the Return key to clear your Mac’s DNS cache.

250+ apps for $9.99
per month

Sign up to Setapp and try them for free.

Security-tested