How to Change a Mac Hosts File: A Step-By-Step Guide

Are you trying to reduce your time spent on social media to be more productive? Or are you working on a new website and need to test it on a different server? A 2020 Verizon Data Breach Investigations Report (DBIR) revealed that 43% of data breaches are linked to web application vulnerabilities.

 

If you fall into any of the categories mentioned above, this article is for you. If not, why not take this opportunity to learn more about Mac host files? Who knows—this information could come in handy in the future.

 

Before we begin

 

As a Mac owner and internet user, safety and security should be your utmost priorities. They should be more important than anything you do on your Mac because if your device is compromised, then so is your data and personal information. MacKeeper’s VPN Private Connect is a secure and reliable VPN service. It protects your privacy by masking your Mac’s IP address on the internet, thereby keeping you anonymous.  

 

Download MacKeeper’s Private Connect

 

MacKeeper’s VPN Private Connect protects you from falling victim to hackers and bad actors who wouldn’t hesitate to steal your personal information and sell it on the dark web. That’s just one of many solutions MacKeeper provides for you. Check it out and see for yourself.  

  1. Download MacKeeper on your Mac
  2. Click on VPN Private Connect in the left pane
  3. Click the Turn On button.

 

That’s all it takes to start browsing the internet safely. In this article, we’ll give you a run-down on Mac hosts files, like what they are and what they do. Then, we’ll guide you in changing a Mac hosts file on your Mac. Let’s dive right in.

What is a hosts file on Mac, and where can you find it?

A hosts file is an unassuming but essential file on your computer that makes it possible for you to view websites in your browser by mapping the connection between an IP address and domain name.

 

That’s thanks to your Mac’s Domain Management System (DNS). The DNS translates the domain you enter in your browser (for example, mackeeper.com) into the TCP/IP format and then displays the requested webpage.

 

This is all achieved in the backend. But did you know you can point the DNS to a different IP server, like one you’ve created yourself? That’s where a hosts file comes in.

 

Remember, every website on the internet has a standard numeric IP address. It typically takes on the format: 123.456.7.89 or similar. You can edit the hosts file to map the DNS to your customized IP address. You can access and change Mac’s hosts file where it’s located, in the Terminal app.

Reasons to change your hosts file

There are several reasons you’d want to change the hosts file on Mac. Below are examples of scenarios in which you would like to change your Mac’s hosts file:

  • You want to limit the websites your child can access on their Mac as a parental control measure, so you opt to block those websites, like social media channels
  • You want to block certain websites in Safari on your Mac, like pornographic websites
  • You’re developing a new website locally, which you want to test
  • You want to test a new development server
  • You want to block websites related to adware and spyware.

Whatever the reason for changing the Mac hosts file, the most important thing is that you can do it. And to that end, we’ve got you covered.  

How to edit hosts file on Mac

As we’ve established, there are various reasons to change the macOS hosts file, and you can do it all in Mac’s Terminal application.  

 

Now, before you make a U-turn, we know the thought of using Mac’s Terminal app can be daunting for some people.  

 

If you’ve ever had any reservations about using Mac’s Terminal, just relax, we’re here to guide you every step of the way. We’ll walk you through the process, and you’ll be editing your Mac hosts file in no time.  

 

So, let’s get started.

1. Open your Terminal application

Start by going to Finder > Applications > Utilities > Terminal. Alternatively, you can press the F4 key, type “Terminal” in the search box, then click on the Terminal icon. You should land on a window that looks similar to the one in the screenshot below.

mac terminal window displayed on desktop

2. Access your /etc/hosts folder

Now type the command sudo nano /etc/hosts and hit the Enter/Return key. Next, enter your admin password and hit the Enter/Return key again. Terminal will now show you the Nano text editor, where you’ll be editing your Mac’s hosts file.

nano editor in mac terminal

3. Begin editing your Mac hosts file

What you do next will depend on what you aim to achieve by modifying the hosts file on your Mac. Let’s imagine you want to add a new domain.

 

On your screen, you should see some TCP/IP addresses for localhost and broadcasthost. Go to the bottom of the list using the downward-facing arrow key, then hit the Enter/Return key. When you’re on a new line, add your own IP address, followed by the domain name.  

 

For the purpose of this article, we’ve entered a straightforward IP address and “anydomain.com” as a domain, as seen below. If you’re adding multiple entries, make sure you add each one on a new line and separate all entries by pressing the Enter/Return key.

mac nano editor with new IP address and domain added

Should you want to cancel any changes you’ve made, you can use your keyboard to delete the new line (or lines) you’ve added. Check out the list of keys at the bottom of the Nano text editor window for the ones you may need to use.

 

At this point, it might be a good idea to block the IP address you’ve added to prevent other sites from accessing it. To do this, press the Enter/Return key, then type 127.0.0.1, followed by the Enter/Return key again.

 

You can use the same IP address to block certain websites you don’t want users to access, like social media or pornographic websites. And if you want to block websites containing adware and spyware, follow the same process and type 0.0.0.0 (again, adding each entry on a new line).

 

When you’re done and ready to move on, save the file(s) you’ve created by pressing Ctrl + O. To exit, press Ctrl +.

4. Flush your DNS cache

The final step in the process of editing your Mac’s hosts file is to flush your DNS cache. To do this, simply type the command sudo killall -HUP mDNSResponder and then hit the Enter/Return key. This will prevent any hiccups with the changes you’ve made to the hosts file by clearing your Mac’s DNS cache.

flush DNS cache command on nano editor in mac terminal

That’s it! You’ve now edited your Mac’s hosts file. That wasn’t so bad, was it?  

 

There’s so much more you can do in Mac’s Terminal—it’s typically the go-to place to fix all your Mac issues. That’s why you could benefit from learning how to use it. Mastering Terminal will have you well on your way to being a tech whiz (in your own right) in no time!

 

But hold on, we’re not done yet. Below is one more fix for Mac’s hosts file.

 

Important to note: while you can block access to your newly-created IP address in the Terminal app, that doesn’t mean you’re completely protected. Your Mac’s IP address itself remains at risk of being compromised unless you add another layer of protection.

 

A reliable VPN is the answer to that. MacKeeper’s VPN Private Connect keeps your IP address hidden, allowing you to browse the internet anonymously and safely.  

MacKeeper's Private Connect VPN

This protects you from falling victim to hackers and bad actors. They’re experienced enough to quickly and easily spot and compromise exposed IP addresses, often without your knowledge.

 

Why not give MacKeeper’s VPN Private Connect a try? And there's no pressure—MacKeeper offers a 7-day free trial. This gives you enough time to get a feel of the service and more of MacKeeper’s reliable cybersecurity solutions before you can commit.

How to fix a malfunctioning Mac hosts file

As with all things technology, we can’t exactly rule out the possibility of something going wrong when changing a MacBook hosts file.

 

There are several reasons why a Mac hosts file might malfunction, like adding too many entries (which could render Terminal unable to process all the changes). Who knows? The good news is that you can fix it, so don’t worry if you encounter some glitches.

 

All you need to do is follow the instructions above to get to the Nano text editor. Once there, delete everything and type the commands/configurations below:

 

##

# 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

 

That’s it, you’re all set!

Is it dangerous to edit the hosts file on a Mac?

As with anything else you might do on Mac's Terminal app, it’s quite safe to edit the hosts file on your Mac. Just ensure you follow the instructions in this article very carefully, and pay attention as you work in the Nano text editor.

 

Having said that, some risks may be involved—like when using someone else’s Mac. Apart from just having to be especially cautious with someone else’s property, changing their hosts file could prove problematic if they don’t have it modified. In a case like that, you’d have to tread carefully.

 

However, if your primary concern is blocking adware and the like, MacKeeper’s Adware Cleaner is your best bet.  

MacKeeper adware cleaner

Summary

As you’ve seen by now, it’s easy to change the Mac hosts file. Follow this guide every time you want to do so, for whatever reason.  

 

More importantly, and this should be your starting point, get MacKeeper’s VPN Private Connect to protect your IP address and personal data. Remember to always keep it on to browse the internet safely and securely.

Use your Mac to the fullest! Sign up and get:
Effective tips on how to fix Mac issues
Reliable advice on how to stay safe online
Mac-world news and updates

Thank you!

You’ll love exploring your Mac with us.

Oops, something went wrong.

Try again or reload a page.

Here’s another sign you need to upgrade your macOS ASAP:

30% off your MacKeeper subscription

Сopy the code now and use it in the MacKeeper checkout after the upgrade.

Copy Code

Please be aware that this code cannot be combined with any other discounts, offers, or promotions.

Contents

Unlocked

Changing your Mac hosts file? Get MacKeeper’s VPN Private Connect firs

Try it now.
PC

MacKeeper - your all-in-one solution for more space and maximum security.

Try Now

Read more

What To Do If Your Mac is Frozen At the Apple Logo
What To Do If Your Mac is Frozen At the Apple Logo
A Full List of All macOs Versions to Date
A Full List of All macOs Versions to Date
arrow

Run Application

step_1

Click Continue

step_2

Click Install

step_1

Your macOS version is lower than OS 10.11. We’d like to offer you MacKeeper 4 to solve the cleaning, privacy, and security issues of your macOS.