Apr 04, 2020 Here’s how to edit the Mac hosts file on macOS (previously known as Mac OS X). Edit Your Mac Hosts File with Text Edit. There are two primary ways to edit the hosts file in Mac OS X. The first is by using TextEdit since the hosts file is a plain text document. The Mac hosts file is an important text document on Mac OS X that allows the mapping of hostnames to specified IP address. Even though the Internet has both public and private DNS servers for mapping of IP address, the hosts file on Mac is a good way to override DNS servers.
On Mac, DNS cache can be easily flushed by executing the following command: Mac OS X Snow Leopard. Sudo dscacheutil -flushcache. OS X Mavericks, Mountain Lion, and Lion. Sudo discoveryutil mdnsflushcache Conclusion. That’s it, by finishing this short tutorial, you have learned how to edit a hosts file on Mac. This is useful if your domain. Oct 07, 2018 There are two main reasons one should think of adding mapping in hosts file: To directly map the domain to an IP so that the resolution time is fast. To block domains by defaulting it to localhost IP. In this article we discuss how to edit hosts file in mac OS X with Yosemite version 10.10.2. Edit Hosts File in Mac OS X. Jan 09, 2020 What is a Mac Hosts File? The hosts file is a text file that is comprised of a list of host names and their corresponding IP addresses. It basically serves as a database of domain names which your operating system employs to identify and locate hosts on an IP network. When your machine needs to look up an IP address it will first search the. Dec 16, 2016 Here’s how to edit the Mac hosts file on OS X. Edit Your Mac Hosts File with Text Edit. There are two primary ways to edit the hosts file in Mac OS X. The first is by using TextEdit, since the hosts file is merely a simple plain text document. However, you can’t open the file directly, since it resides in a protected area of the file system.
Source: iMore
If you're setting up a new webserver or another device that's going to be connected to the internet and you want to put it through its paces before it's live, or if you're having trouble with spyware and adware networks, there's an invisible file on your Mac that can help. It's called the Hosts file, and this is how to use it.
The Domain Name System
When you type in the domain name of a web site you want to visit there's a lot happening behind the scenes. Every web site, every service, almost every device connected to the Internet has a unique numeric address that tells all the other devices where it is — its TCP/IP address. The Domain Name System (DNS) translates those numeric addresses into something a bit more recognizable and memorable to humans, like, 'www.imore.com' for example.
The first time you type in a web address, your Mac pings a DNS server — typically one automatically configured for you by your Internet Service Provider — to find out the TCP/IP address of the server you're trying to connect to. Your Mac builds up a hidden cache file to remember those details later on when you visit the same site again.
The Hosts file
The Domain Name System and its associated cache is your Mac's standard way of knowing how to get to where it's going on the Internet, but there's another file that can be very useful. It's called the Hosts file, and it can be used to override the default DNS information.
There are some practical reasons why you'd want to use the Hosts file instead of just letting DNS do its thing. Let's say you're testing a development server you're about to deploy, and you'd like to use its domain name instead of the machine's specific IP address. Before the system's online and accessible to anyone using DNS, you can use the Host file instead: Enter the machine's IP address and when you use that domain name, your Mac will go to that device instead.
You can also use the Hosts file to block or work around spyware and ad networks by 'zeroing out' their IP addresses - putting in 0.0.0.0 then the name of the domain you'd like to block.
![Mac Mac](/uploads/1/2/6/2/126214814/469841709.jpg)
How to edit the Hosts file
You'll need to know the IP address of the device you'd like to send your Mac to, or the domain names you're trying to keep your Mac away from. The best way to work with the Host file is by using the Terminal application found in your Mac's Utilities folder. You can search for Terminal using Spotlight or by selecting it in Finder.
- Open a Finder window.
- Select Applications from the sidebar.Source: iMore
- Double-click on Utilities.
- Double-click Terminal.Source: iMore
In the Terminal window, you will need to enter a command to open the Nano text editor. You will need your administrator password, as well.
- type sudo nano /etc/hosts and then hit return.
- Enter your administrator password and then hit return.Source: iMore
You're now in the Nano text editor. You should see something that looks like this:
If you want to add a new device or domain, move the cursor using the arrow keys and position it after the text in the window. Then, begin typing.
If you're mapping a particular IP address on your local network to a domain, you can type the IP address, hit tab, then type the domain name.
Conversely, if you'd like to make sure a web URL doesn't go to its intended site — if you're trying to keep your Mac away from certain sites, use '127.0.0.1.' That'll map it back to your Mac. Even if your Mac is assigned a different IP address by its router, 127.0.0.1 defaults to the local machine thanks to the default settings in that hose file.
Once you're done, hold down the control and O keys to save the file, then control and X to exit.
One last step
Back at the command line, type sudo killall -HUP mDNSResponder then type return. That will flush your Mac's DNS cache, so it doesn't get confused by any changes you've made to the Hosts file.
Don't forget you've modified the Hosts file, because at some point you may need to undo the changes you've made in order to keep your Mac working right.
How to reset your Mac's Hosts file
When you're ready to undo your changes and restore where your Mac's Hosts file routes to, you can do so by replacing the current host file with a new set of terms. You don't need Terminal for this reset.
- Make sure Finder is selected on your Mac.
- Click on Go in the Mac menu bar.
- Click on Go to Folder.
- Enter /private/etc/hosts into the search field.
- Click Go.
- Drag the Hosts file from the Finder window onto your desktop.
- Open the Hosts file from your desktop. It should open to TextEdit by default.
- Delete the content of the Hosts file.
- Paste the following into the same file:
- Click Save.
- Drag this same Hosts file from your desktop back into the same folder. If you don't have the folder open in finder anymore, follow steps one through five to reopen it.
- Click Replace when asked whether you want to replace the file.
- Enter your Administrative password when prompted (that's the password used by the main user to unlock their Mac).
- Restart your Mac.
Have any questions? Let me know.
Updated February 2020: Updated for macOS Catalina.
macOS Catalina
Main
We may earn a commission for purchases using our links. Learn more.
A more musical homeApple's new 'Behind the Mac' vid shows James Blake making music at home
Apple's latest 'Behind the Mac' video shows James Blake making music even though he's been stuck in his home studio because of 2020.
Like Windows-based computers, Mac has a hosts file to configure how your machine connects to the websites on the Internet. This file contains references to websites and IP addresses, and you can use it in many different ways on your Mac.
One of the reasons you may want to edit the hosts file on your Mac is to block websites. It’s a really great way to block certain websites from being accessed on your machine. The second possible usage is to test out your own websites locally on your machine. You can have the hosts file redirect your chosen domain name to the network path of your local storage.
Mac Hosts File Location
Since editing the hosts file is a risky task, Apple has intentionally placed it in a private folder on your system. That’s to prevent users from modifying it without proper knowledge and causing damage to the entire system.
For the curious users out there, the path is /etc/hosts/ and you can access it using a Terminal window.
Edit Hosts File On Mac
It’s fairly easy to edit the hosts file on your Mac since there’s a built-in editor to do it. It’s located inside the Terminal and is called nano editor. You can use it to open and edit any text file including the hosts file on your machine.
Make sure you’re using an admin account to do the following steps.
- Click on Launchpad in the Dock, search for Terminal, and launch it.
- Type the following command into the Terminal and press Enter. It’ll open the hosts file in the nano editor.
sudo nano /etc/hosts
- Since it’s a sudo command, it’ll ask you to enter your password. Enter the password and continue.
- The file should now be open on your screen and you can start editing it.
Understanding The Mac Hosts File
If you haven’t edited a hosts file before, you might find the file a bit complicated to work with. However, it’s not as difficult to edit it as it looks.
One of the entries you’ll find in the file is 127.0.0.1 localhost.
The first section with numbers is the local IP address for your Mac. The second section where it has the host name is what you use to access that IP address.
Os X Hosts File
The above two parts, when combined together, redirect all the localhost queries to the IP address 127.0.0.1. When you enter localhost in your browser, your browser looks into the hosts file, obtains the specified IP address, and takes you to that IP address.
In a nutshell, the hosts file is nothing but a combination of IP addresses and domain names. You can modify these in any way you want to achieve desired results.
Mac Hosts File Not Working
Set Up Redirects With The Hosts File
One of the things you can do with the hosts file is to set up redirects. You can have a domain name point to something completely different than what it’s supposed to point to.
For example, if you’re trying to cut down on your social media usage, you can have domains like facebook.com redirect your browser to sites like, say, Wikipedia. You can use just about any domain and IP address you want.
Let’s take a look at how you can set up the above redirection using the hosts file.
- While the file is open in the nano editor, use the arrow keys to bring your cursor to where localhost ends. Then press Enter to add a new line for your entry.
- In the new line that you just added, type the IP address where you want to redirect the source domain to. We’ll use 103.102.166.224, which is Wikipedia’s IP address.
- Press the Tab key on your keyboard to get to the source domain field.
- Here, type the domain name that is to be redirected to the IP address you typed earlier. We’ll use facebook.com here.
- Once the changes are made, press the Ctrl + O keys on your keyboard to save the file.
- Press Enter to confirm the action.
- Press the Ctrl + X keys to close the nano editor.
- You now need to flush the DNS cache to bring the changes into effect. To do it, type the following command into the Terminal and press Enter.
dscacheutil -flushcache
- Open a browser, type facebook.com, and hit Enter. You’ll find it opens Wikipedia rather than Facebook.
Quick Tip: How To Find a Website’s IP Address
As you can see in the above procedure, you need the IP address of the site that you want to redirect people to. If you don’t already know it, you can use a command in the Terminal to find out the IP address of any website.
- In the Terminal window, type the following command and hit Enter. Make sure to replace wikipedia.org with the website of your choice.
ping wikipedia.org
- It’ll display an IP address on your screen. That’s what you can use in the hosts file.
Block Websites By Editing The Hosts File On Mac
The great thing about the hosts file is it lets you block sites without requiring you to install any third-party apps on your Mac. You can add an entry to the file and all the connection requests to that entry will be denied.
- Launch the hosts file in the nano editor as shown above.
- Bring your cursor where the localhost entry ends and press Enter to add a new line.
- Type in the IP address 127.0.0.1 and press Tab on your keyboard.
- Enter the domain name of the site that you want to block. For example, if you want to block Instagram, type instagram.com.
- Press Ctrl + O to save the changes.
- Press Ctrl + X to close the file.
- Type the following command and press Enter to flush the DNS cache.
dscacheutil -flushcache
Mac Update Hosts File
Now each time you try to access the blocked site, it’ll take you to the localhost which will show an error page.
Change Host File On Mac
The Mac hosts file provides you with many ways to play around with outgoing network requests, and you can block and unblock them as you wish. Have you used the hosts file on your Mac before? If so, what was it for? Let us know in the comments below.