Editing the Hosts File on a Mac

Editing the Hosts File on a Mac Using Terminal
  • Save
The hosts file on a mac relates hostnames (e.g., domain names) to IP addresses. When a mac user types a domain name in to a web browser’s address bar, the first place it will look for that domain’s IP address is in the hosts file. If no IP address is listed, then the browser will resolve to the IP address served up by the domain’s DNS servers, via the online connection. In other words, the browser will query the internet registry to find the server ip address of where the domain is hosted.

Since the hosts file is editable, it can be used to prevent the browser from viewing unwanted IP addresses, and it can also be used to redirect the browser to a different IP address, such as that on your local computer or a remote server. For example, you can edit the hosts file on a mac to block malicious or inappropriate websites from being viewed in a browser. This I haven’t needed to do, but here’s how you can do it if you’re interested…

Open up the hosts file by going to: Applications -> Utilities -> Terminal

Type the following in Terminal:

sudo nano /etc/hosts

When prompted, enter in your system password.

Cursor down to the first blank line after localhost, then type in:

127.0.0.1

…followed by the domain of the site which you want to block (e.g., facebook.com).

What this does is force the domain of the website to resolve to your local machine, resulting in a browser error since it won’t be able to connect to the website.

Anyway… this is something I don’t do, but some of you out there may have a need to limit or monitor access to facebook, gambling sites, pron, etc… This can be helpful for preventing employees, children, etc… from visiting inappropriate sites.

To add a private hostname to the hosts file:

When I edit the hosts file, it’s typically to test a website that I’ve migrated from one server to another. I have four separate servers that I use for hosting, so occasionally I’ll shuffle sites around as their needs change. This is usually when a site outgrows the limits of shared hosting and needs to move to a VPS.

Once I’ve re-established the site’s account on the new server, I then edit my hosts file so that I can resolve the domain to its destination home. This allows me to fully test the copied site – making sure that the migration was a success, and that all the systems are in place and functioning properly.

Open up Terminal (Applications -> Utilities -> Terminal) and type in:

sudo nano /private/etc/hosts

Enter your password when prompted.

Cursor down and add the ip address of the destination server, followed by the domain name. Once you’re finished:

  1. Hit CTRL+O to save
  2. Press enter
  3. Hit CTRL+X to exit

Once this is done you’ll need to clear your DNS cache by typing in:

dscacheutil -flushcache

Now the copied site can be accessed via its domain name through a web browser.

Once the site is ready to go live, just re-open Terminal, delete the private hostnames, save using the steps outlined above, then flush the DNS cache again.

Hopefully this tutorial helps some of you out there. ๐Ÿ™‚

2 thoughts on “Editing the Hosts File on a Mac”

  1. Thank you! This tutorial helped me so much, especially just to feel comfortable messing around with the hosts file. It really helps that you wrote it with an understanding that a lot of people need very specific instructions, rather than just being told to “add such and such to the file then flush cache”. And the screenshots were very helpful as well.

Leave a Comment

Your email address will not be published. Required fields are marked *