How to edit the hosts file in Mac OS X 10.8 – Mountain Lion
The hosts file is a text file that maps hostnames to IP addresses. Upon typing a url address in the browser, the system first checks if there is a relevant entry in the hosts file and if exists gets the corresponding IP address. If no entries exists it resolves the IP via the active connection’s DNS servers.
The hosts file can be edited to block certain hostsnames, like ad-serving/malicious hosts, or used for web development purposes, i.e. to redirect domains to local addresses.
Step 1 – Open the Terminal.app
Either start typing Terminal in the Spotlight search, or goto Applications > Utilities > Terminal.
Step 2 – Open the host file
Open the hosts file by typing the following in the Terminal window:
$ sudo nano /private/etc/hosts
Type your user password when prompted.
Step 3 – Edit the hosts file
The hosts file contains some comments (lines starting with the # symbol), as well as some default hostname mappings (e.g. 127.0.0.1 – local host). Simply append your new mappings underneath the default ones.
Step 4 – Save the hosts file
When done editing the hosts file, press Control+o to save the file.
Press Enter on the filename prompt, and
Control-x to exit the editor.
Step 5 – Flush the DNS cache
You can use a simple Terminal command to flush the DNS cache, and have your host file changes take immediate effect. Using the open Terminal window, then the following command:
$ dscacheutil -flushcache
Your new mappings should now take effect.
Previous Mac OS X operating systems:
How to edit the hosts file in Mac OS X 10.6 – Snow Leopard
How to edit the hosts file in Mac OS X 10.7 – Lion
24 comments
Pingback by How to edit the hosts file in Mac OS X 10.7 – Lion | Grapii
on August 2, 2012 at 2:52 pm
[...] Mac OS X operating systems: How to edit the hosts file in Mac OS X 10.8 – Mountain Lion How to edit the hosts file in Mac OS X 10.6 – Snow [...]
Pingback by How to edit the hosts file in Mac OS X 10.6 – Snow Leopard | Grapii
on August 2, 2012 at 2:54 pm
[...] Mac OS X operating systems: How to edit the hosts file in Mac OS X 10.8 – Mountain Lion How to edit the hosts file in Mac OS X 10.7 – [...]
Comment by Sam
on August 15, 2012 at 4:07 pm
thanks! BTW I had “$” symbol already in terminals default string, thus there is no necessarily to type “$” for whole sudo string
Comment by Steve
on September 8, 2012 at 1:29 am
Worked perfectly. Thanks!
Comment by Jan
on September 20, 2012 at 8:46 am
Thank! Minor note, you should use ‘sudo -e’ rather than ‘sudo nano’.
From man sudo:
-e The -e (edit) option indicates that, instead of running a command, the user wishes to
edit one or more files. In lieu of a command, the string “sudoedit” is used when
consulting the sudoers file. If the user is authorized by sudoers the following steps
are taken:
1. Temporary copies are made of the files to be edited with the owner set to the
invoking user.
2. The editor specified by the SUDO_EDITOR, VISUAL or EDITOR environment variables is
run to edit the temporary files. If none of SUDO_EDITOR, VISUAL or EDITOR are set,
the first program listed in the editor sudoers variable is used.
3. If they have been modified, the temporary files are copied back to their original
location and the temporary versions are removed.
If the specified file does not exist, it will be created. Note that unlike most commands
run by sudo, the editor is run with the invoking user’s environment unmodified. If, for
some reason, sudo is unable to update a file with its edited version, the user will
receive a warning and the edited copy will remain in a temporary file.
Comment by @jcambrohio
on September 21, 2012 at 4:31 pm
Thanks Rajesh, this saved me from a big headache today and got me to use terminal for something new.
Comment by Mike Venables
on October 11, 2012 at 4:31 am
I much prefer GasMask, a free app that allows you to switch back and forth at the click of a mouse.
Comment by aank budi santoso
on November 20, 2012 at 5:01 pm
What exactly are you trying which doesn’t work? Cause it seems to be working just fine here.
Comment by tony
on November 28, 2012 at 4:34 am
Hi,
How do you undo this? I edited the hosts file and the website is blocked, but when I go back to terminal to remove the ip address and website, it won’t save.
What I mean is, “control + o” then “control + x” won’t work anymore.
I have also flushed the dns cache too.
Unfortunately, I can’t undo the hosts file anymore.
Also, what key is the “M” in “M-A append” in the hosts file?
Thanks
Comment by Rajesh Patel
on November 28, 2012 at 5:58 am
Not sure what you’re doing or not doing exactly, but I just edited the hosts file (essentially undoing) and works fine. Can you define your steps more clearly?
Comment by BMC
on January 2, 2013 at 11:19 am
I am having trouble – have edited hosts under 10.7 on my old mac and worked fine… having trouble on new macbook pro with 10.8
When I PING mydomain.com and http://www.mydomain.com the correct IP address (the one I am redirecting to) is returned. However from ALL browsers (Safari, firefox and chrome) I still get the old IP – I have flushed the cache and reset safari etc but can’t seem to resolve this issue.
Tried GASMASK also – still no go
Ideas?
Comment by BMC
on January 2, 2013 at 11:28 am
*** sorry correction – on my new mac under 10.8 PINGing “www.urbanhealth.com.au” doesn’t redirect (still goes to the old IP) but the url without the “www” goes to the intended/new IP…
No matter what I type in the browser – all go to old IP – grrrr!!!
I have listed both variants when editing the hosts file.
Comment by Rohan
on January 29, 2013 at 8:42 am
Thanks a lot for this post. You should post this on Citirx site for those who are using Citrix receiver on Mac.
They need to edit the hosts file in order for citrx to work on mac.
It saved my life my not making me install Windows
Comment by @ObjT
on February 18, 2013 at 3:29 pm
Thanks! Exactly what I needed.
Comment by Simon
on March 12, 2013 at 9:29 pm
Hi, I’m having issues trying to write to the host file, when I get into it step 2 it’s blank… I try to write some codes & I get an the following error: error writing /private/ etc /hosts no such file or directory, I’m using Mountain Lion. Can you help??? Thanks
Pingback by links for 03/13/2013 | Alan Vonlanthen's blog
on March 13, 2013 at 12:35 am
[...] How to edit the hosts file in Mac OS X 10.8 – Mountain Lion | Grapii [...]
Comment by kist
on March 27, 2013 at 3:10 am
hey thanks for this. been very helpful!
Comment by Helene
on March 27, 2013 at 10:39 pm
Wow, thank you so much! After 36 hours of hell, I was able (with your help) to figure out how to edit and save the host properly. I love this!!
Thanks again,
~Helene
Comment by Øyvind
on March 31, 2013 at 10:02 am
Hi!
Thanks a lot for this post, but i am having an issue here. I am seemingly not allowed to edit my hosts file, and my MacBook keeps telling me that I “Don´t own the file”. I have the only user, except the guest user, on this Mac, and of course all administrative rights, so i see no reason for not being able to edit it. Is there a root user in OS X as there is in Windows? And if there is, how do i get access to it?
- Øyvind
Comment by Nick
on April 5, 2013 at 12:14 pm
Hey there I’m receiving the same error code as Simon: error writing /private/ etc /hosts no such file or directory though I did not see any response to this posted. =( Help would be much appreciated.
Thank you =)
Comment by Oscar
on April 20, 2013 at 12:08 am
Thanks Grapii. I finally got my host file edited (Mac OSX 10.8.3) Your step by step instructions where determinants to get it done. Trustable and updated informations
Comment by Witty
on April 29, 2013 at 11:01 am
It helped me to do some important stuffs . . thank you
Comment by Ian
on April 30, 2013 at 8:46 am
Concise and clear. Followed the instructions and enabled me to access by name a machine whose IP address had changed.
Many thanks, very useful. Have recommended.
Comment by roger
on May 8, 2013 at 12:50 pm
great, thanks!