How to edit your Mac’s Hosts file

How to edit your Mac’s Hosts file

If you’re setting up a new web server or another device that’s going to be connected to the Internet and you want to put it through its paces before it’s live, or if you’re having trouble with spyware and adware networks, there’s an invisible file on your Mac that can help. It’s called the Hosts file, and this is how to use it.

The Domain Name System

When you type in the domain name of a web site you want to visit there’s a lot happening behind the scenes. Every web site, every service, almost every device connected to the Internet has a unique numeric address that tells all the other devices where it is — its TCP/IP address. The Domain Name System (DNS) translates those numeric addresses into something a bit more recognizable and memorable to humans, like, “” for example.

The first time you type in a web address, your Mac pings a DNS server — typically one automatically configured for you by your Internet Service Provider — to find out the TCP/IP address of the server you’re trying to connect to. Your Mac builds up a hidden cache file to remember those details later on, when you visit the same site again.

The Hosts file

The Domain Name System and its associated cache is your Mac’s standard way of knowing how to get to where it’s going on the Internet, but there’s another file that can be very useful. It’s called the Hosts file, and it can be used to override the default DNS information.

There are some practical reasons for why you’d want to use the Hosts file instead of just letting DNS do its thing. Let’s say you’re testing a development server you’re about to deploy, and you’d like to use its domain name instead of the machine’s specific IP address. Before the system’s online and accessible to anyone using DNS, you can use the Host file instead: Enter the machine’s IP address and when you use that domain name, your Mac will go to that device instead.

You can also use the Hosts file to block or work around spyware and ad networks by “zeroing out” their IP addresses – putting in then the name of the domain you’d like to block.

How to edit the Hosts file

The best way to work with the Hosts file is using the Terminal application found in your Mac’sUtilities folder. You’ll need to know the IP address of the device you’d like to send your Mac to, or the domain names you’re trying to keep your Mac away from.

  1. Double-click Terminal.
  2. type sudo nano /etc/hosts then hit return.
  3. You’ll be asked for your password. Enter your admin password.

Terminal app

Terminal command line

You’re now in the Nano text editor. You should see something that looks like this:

Hosts file in the Nano text editor

If you want to add a new device or domain, move the cursor using the cursor keys and position it after the text you see, then begin typing. If you’re mapping a particular IP address on your local network to a domain, you can type the IP address, hit tab, then type the domain name.

Conversely, if you’d like to make sure a web URL doesn’t go to its intended site — if you’re trying to keep your Mac away from certain sites, use “” That’ll map it back to your Mac. Even if your Mac is assigned a different IP address by its router, defaults to the local machine thanks to the default settings in that hose file.

Once you’re done, hold down the control and O keys to save the file, then control and X to exit.

Back at the command line, type sudo killall -HUP mDNSResponder then type return. That will flush your Mac’s DNS cache, so it doesn’t get confused by any changes you’ve made to the Hosts file.

Don’t forget you’ve modified the Hosts file, because at some point you may need to undo the changes you’ve made in order to keep your Mac working right.

Article by PETER COHEN

Leave a Comment