If you use your Mac every day, you know how seamless and intuitive the experience can be. It’s almost akin to play: dragging and dropping icons and files, and having the whole environment neatly arranged.
Despite the apparent ease of use, Macs are highly capable machines that could really open up their programmatic side and let you control them right from your keyboard. All this is because macOS is based on the Unix system, just like Linux and many others that are known for providing much greater levels of control compared to more closed systems, such as Windows.
Why is having a more open system such a big deal? Simply, controlling macOS programmatically speeds up lots of everyday tasks and reveals lots of functionality that is otherwise completely inaccessible through a regular graphic interface. You might have even already used the key Unix-like tool in your Mac’s arsenal — Terminal.
Terminal allows you to perform nearly every function you normally use your mouse for: navigate folders, change preferences and settings, execute commands, etc. One of the unique tasks that can be easily controlled through Terminal is configuring your Mac’s Hosts file.
What And Where Is Hosts File On Mac
Hosts file is a hidden gem on your Mac that lets you override DNS settings exactly as specified. Let’s elaborate. Any website on the internet has a standard numeric TCP/IP address (e.g. 155.135.55.81) — that’s how your browser is actually able to find the website you want. However, no one would ever want to remember all those TCP/IP numbers, and that’s why the Domain Management System (DNS) was created.
Every time you put in a URL in your browser (e.g. setapp.com), it pings the domain server, which in turn translates the requested domain into the TCP/IP format, and only then connects as to be able to display it.
Having said that, there are instances when you don’t want the domain name to go to the actual TCP/IP, but would much rather write down your own. This can be necessary when you want to test a new website you’re developing locally, or a new development server altogether. You can also use Hosts file on Mac to block adware and spyware websites, and even some time-wasting ones (e.g. social media).
Now let’s discover how we can actually change Hosts file on Mac.
How to edit Hosts file on Mac
To edit Hosts file Mac features is not a difficult although completely non-obvious process. To open Hosts file on Mac, which is hidden, you need to use Terminal:
- Launch Terminal
- Type sudo nano /etc/hosts and press Return
- Enter your admin password
Now you’ll notice you’re in the Nano text editor, from where you can actually edit Mac’s Hosts file. As you can see, some of the TCP/IP addresses are already reflected in here against localhost and broadcasthost titles. All you need to do to input your own TCP/IP is to follow the same format:
- Use the Return key to create some space below the existing records
- Type in the TCP/IP address
- Press the Tab key
- Enter the domain name
- Save the file by pressing Ctrl + O
- Exit with Ctrl + X
- In Terminal, type sudo killall -HUP mDNSResponder followed by Return to clear your Mac’s DNS cache and prevent any clashes between DNS and the Hosts file
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.
When adding multiple entries, make sure each one has its own line. To cancel the change, repeat the process above but simply delete the line you’ve added before that is no longer needed.
Fix a malfunctioning Mac Hosts file
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.
To resolve the situation, you could try following the instructions above to get to the Nano text editor and then delete everything there and retype the 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 making sure you don’t end up somehow crushing your system — creating consistent backups.