1. Development
  2. How to edit 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:
  1. Launch Terminal
  2. Type sudo nano /etc/hosts and press Return
  3. 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:
  1. Use the Return key to create some space below the existing records
  2. Type in the TCP/IP address
  3. Press the Tab key
  4. Enter the domain name
  5. Save the file by pressing Ctrl + O
  6. Exit with Ctrl + X
  7. 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. 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.

Related Articles

Development

Website Security Threats Checklist

Development

Managing Cloudflare account access

Development

Linking your Bank Account to Square

Tools + Resources

Freelance Hourly Rate Calculator

Social Media Image Sizes

Need Support?

Website Maintenance and Support

Your website security is important to us! We run frequent security checks and regularly update software to guarantee the integrity of your site continues to stay reliable and intact.

Support You Can Count On

Menu