Jump to content

How to edit your Mac's Hosts file, and why you would want to


Recommended Posts

Guest Peter Cohen
Posted

df13808ca8ebae4bb813dc0ff47f2c54.jpg

 

If you're setting up a new web server 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 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

 

 

The best way to work with the Hosts file is using the Terminal application found in your Mac's Utilities folder. 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.

 

  1. Double-click Terminal.
  2. type sudo nano /etc/hosts then hit return.
  3. You'll be asked for your password. Enter your admin password.

 

1fcf25b3130c01b709075f63d32d1a05.jpg

 

f2d919a41d55473181eb7395eed2a579.jpg

 

You're now in the Nano text editor. You should see something that looks like this:

 

6f7362cac44c88c81f22308494990bbc.jpg

 

If you want to add a new device or domain, move the cursor using the cursor keys and position it after the text you see, 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.

 

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.

 

Have any questions? Let me know.

 

14fb8ab7dbb3bca88f30539bdf0996d4.gif

 

 

8c09e6bff9d82ebea562068cfbda68c4._.png

db69b3ec3895ad171e9507279c5287a7._.png

dc6ec1d23e0365b0ae868333db00076c._.png

 

b78e1ed85060c0794bc8018d47931ca8._.gif175248feff7400fad674e575b8efbe29._.gif3abd243b7555b5488001e218719b5140._.gif

 

Continue reading...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...