The hosts file is an integral component of the Mac operating system. Although it often goes unnoticed, it is crucial in maintaining seamless network communication. The hosts file is a local DNS resolver, mapping domain names to specific IP addresses. Continue reading to know the importance of hosts files in macOS, the steps to access and edit the hosts file, and the reasons why it is essential.
What is the Hosts File and its importance?
Operating systems use a hosts file to map a connection between domain names and an IP address before going to the domain name servers. It is a simple text file mapping domain names and IPs.
The importance of hosts files are:
- Privacy protection – Hosts files can block trackers, advertisers, and third-party websites to protect user privacy.
- Block access to certain websites – If you do not want your children or anybody to visit certain websites, you can block them by the hosts files. It is entirely up to you what you wish to block. Hence, parents and even employers can use this technique to block some websites.
- Enhanced security – Hosts files can be used as a firewall in your local system. It can help block spyware, ransomware sites, and other malicious websites.
- Useful in development – During the development of apps, developers need to run their web applications on the localhost for verification. But there might be some limitations when websites and apps are run on the localhost. The restrictions are overcome by choosing a custom domain in the hosts file.
What is the Difference between Hosts File and DNS?
The DNS (Domain Name System) is the database corresponding to an IP address dotted quad numerical format with a site’s alphabetized web address. It is an enormous directory of which name belongs to which IP address. The DNS is much like the yellow pages of the Internet.
On the other hand, the hosts files are localized to your devices (in this case, your Mac computer). It is similar to a list of contacts on your mobile phone; the web address acts as the contact’s name. The IP address is the mobile phone number.
Steps to finding and editing Mac hosts file
Now let’s see how you can find and edit Mac’s hosts file. It can be done in four simple steps.
- Launch Terminal
Launch the Terminal application on your Mac by clicking Finder > Go > Utilities > Terminal. Alternatively, press F4 to open the launcher and enter “Terminal” in the search field. Click the icon when it appears.
- Access the /etc/hosts folder
The next step is to find the Mac etc hosts file, which can be accessed via a text editor. You can install a text editor of your choice and then type in the Terminal application the command “sudo [text editor name] /etc/hosts” and press the Enter key on your keyboard. Then, enter the administrator password when prompted and press the Enter key.
- Edit the Mac hosts file
Once you have pressed the Enter key after typing your password, you will be brought to the text editor. Then, the next steps will depend on what you want to do within the hosts file.
For instance, let’s assume you want to add a new domain. For this, you can use the arrow keys to navigate to the file’s bottom, and on a new line, you can add the IP address of the domain. Remember to keep in mind that every single entry must have a line of its own.
If you want to cancel the edits or changes you are making, you can use the delete button on your keyboard to remove the lines you have added.
Besides adding IP addresses, it is also possible to block them to prevent your device from pointing a website their way. Once done, press Control + O to overwrite the existing file, and then press Enter. If you want to exit, press Control + X.
- Flush DNS cache
Have you made changes to your Mac hosts file? But are the changes not going live? The issue might be with the DNS cache. Hence, it is a good idea to flush the cache before you finish editing the file.
When you flush your DNS cache, it will remove all the saved IP records. Following this, your web browser will reflect on the updated IP address from the edited hosts file.
You can update the DNS records by entering the command “sudo killall -HUP mDNSResponder” into the Terminal app. After this step, the changes will go live within the web browser, and you will have successfully made changes to the Mac hosts file.
Why Consider Changing the Hosts File?
Usually, entering a web address in the web browser means wanting to reach the specific IP address. But there might be times when you would like to change or edit the hosts file. Some of the reasons are as follows:
- To block malicious websites – It is not just the risk of malware and viruses that you must be aware of, but undetected add-ons can also find their way into your Mac and start to slow things down. It is always a good option to invest in pop-up ad blockers, but you can also edit the hosts file to prevent malicious websites from being accessed. If a pop-up or malicious-looking link attempts to enter the website, your operating system will reroute and take you to a safe website.
- To test the network – Are you developing a network? The actual domain name can be rerouted to the development website to see how users are experiencing it. This is not going to impact other users who are accessing the website.
- To improve productivity – If you are getting distracted by too many video streaming or social media websites, you can block these time-wasting websites and focus on your tasks. This will help to enhance productivity.
- To enhance speed – As mentioned above, finding a site via the DNS is similar to flicking through the yellow pages. If the hosts file is localized to your Mac computer, you can speed up browsing and remove the middleman.
Conclusion
So, now you know the importance of Mac hosts files and how to change them for your benefit.