In the recent article, we discussed how to edit hosts file in Windows, and now it is time to check how hosts file works in macOS. Long story short, it works in the same way as on Windows. Hosts file is responsible for domain name conversion into IP-addresses. A domain name is what you type in the address bar in Safari. An IP address is a “physical” address of the website. When loading a website, system checks hosts file first. If there are no matches, it uses DNS service to find an IP address and load a website.
Most of the time an average user does not need to mess around with hosts file. Still, you need to know how to edit hosts file on Mac. For example, you want to limit access to some website. Another scenario involves editing hosts file on Mac to troubleshoot your computer. Some malware or unwanted software may use hosts file to redirect you to a specific website. For example, you try to open Google and end up on the pornographic website. In such a case you need to check hosts file first and make sure there are no unnecessary entries.
Edit Hosts File on Mac
Before you start: changing hosts file in macOS requires Administrator privileges, so make sure you know the password.
- Press Cmd + Spacebar to launch Spotlight. Enter Terminal and launch it. Optionally, open Launchpad and find Terminal in there.
- Enter sudo nano /private/etc/hosts, then press Return or Enter. This command tells Mac to launch Nano text editor and open hosts file.
- Next, enter your password. Do note that symbols are no visible when you type, so do not think that your Mac is stuck.
Now you are inside hosts file. Editing hosts file on Mac is as easy as opening it. You need to know domain name (google.com, for example) and IP-address you want to redirect it to (18.104.22.168, for example). Move the cursor to the very bottom of the window and start typing from the new line. Remember to start every new entry from the new line and separate domain name from IP-address with at least one spacebar or Tab. Also, make sure there is no # symbol in the beginning. macOS ignores everything after the # symbol. It is useful to add some comments or reference text. Just make sure you add it after the domain name and IP-address. In the end, you should have something like the following:
127.0.0.1 www.techrises.com #here is the example of a new hosts file entry
If you want to limit someone’s access to some specific websites (for example, prevent your child from going where it shouldn’t go), use 127.0.0.1 or 0.0.0.0 to block access. In this scenario, macOS will redirect the specified domain to localhost.
When you finished editing hosts file, click Control + O to save everything you typed in. Then, press Control + X to close the text editor.
Although unnecessary, we recommend enter sudo killall -HUP mDNSResponder command to flush DNS cache. This is just to be sure your changes apply instantly.
How to Reset Hosts File on Mac?
This is a very simple procedure. Open hosts file using sudo nano /private/etc/hosts Terminal command (just like in the section above) and delete all entries from the very bottom up to the following lines:
Alternatively, delete everything within hosts file, and enter these three lines:
Save all the changes using Control + O command and close editor using Control + X buttons. Flush DNS cache and you are good to go.