How to edit your Mac's Hosts file

How to edit your Mac's Hosts file


You can tell your Mac to use a Host File instead of your DNS to go online. 

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 for 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.

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 Hosts file is using the Terminal application found in your Mac's Utilities folder. You can search for Terminal using Spotlight or by selecting it in Finder.
1.Open a Finder window.
2.Select Applications from the sidebar.
3.Double-click on Utilities.
4.Double-click Terminal.
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.
1.Type sudo nano /etc/hosts and then hit return.
2.Enter your administrator password and then hit return.
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 controland 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.


留言

熱門文章