Jul 30

You can’t just open up your editor of choice to modify the /etc/hosts file on your Mac. You can open the file without any problem, you just can’t save it.

To do this you must have super user privileges. Just being an Administrator isn’t enough. Fortunately, there’s a way to temporarily get super user privileges using the SUDO command. So I fired up Terminal and typed in:

sudo textedit /etc/hosts

Ruh-roh. It wouldn’t let me save the file. Crap. I’m not a Unix guy and VI makes my skin crawl and head hurt, but I was able to use sudo vi /etc/hosts and save the file.

So why couldn’t I get any of the GUI apps to save it?

Turns out, that you need to sudo the actual executable which is inside the TextEdit.app bundle. So the command I issued is this:

sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts

Voila!!

If you happen to own TextMate, then you can save yourself a lot of typing and heartache. TextMate installs a Terminal command called “mate” that allows you to perform the above sudo command. Just type:

mate /etc/hosts

BBEdit also has this capability:

bbedit /etc/hosts

TextWrangler as well:

edit /etc/hosts

Voila, again!! When you go to save your file, you will be prompted for your password.

Written by Terry Blanchard \\ tags: , , , , , ,

8 Responses to “Using TextEdit to Modify /etc/hosts File”

  1. Benjamin Woodruff Says:

    Hi,

    Thanks for writing this neat post. I’m using Lion 10.7.2 and am having a hard time using terminal and your commands to accomplish anything.

    I’m trying to edit the hosts file so that I can update my iPad to iOS 5.0.1, which I’ve read online that this will help with. My problem is in saving the hosts file, which I’ve read elsewhere is a pain to do. Other people in the comments threads on these sites seemed to have no issues, so I’m assuming it’s an issue with Lion.

    Anyway, I try to save the edited hosts file into the /etc folder, and it tells me I can’t save the replace the file because it’s in use. Any idea on how to get around this issue? I don’t know exactly how it’s in use so that I can get it to not be in use when I go to save the file. I think that’s my issue.

    Thanks.

  2. Terry Blanchard Says:

    What are you using to edit the file? My hunch is that it’s a permissions issue. You do need to have Administrator privileges to modify that file.

  3. Brian Sassaman Says:

    I came here looking for help on my slow Apple wi-fi network, and you solved my problem! Thanks! But I recently ran across this issue about the hosts file and it turns out Apple changed something in Lion, and now to edit the hosts file you have to use this:

    sudo nano /etc/hosts

    It worked for me. And thanks for the wifi help!

  4. akio Says:

    I have been trying to use the sudo command as in the instructions but then it gives me this message below, the problem is it doesn’t allow me to type my password when I try…

    WARNING: Improper use of the sudo command could lead to data loss
    or the deletion of important system files. Please double-check your
    typing when using sudo. Type “man sudo” for more information.

    To proceed, enter your password, or type Ctrl-C to abort.

    Password:

  5. Terry Blanchard Says:

    The password is the one for your user account; the same one you log in with when you start your Mac. Also, you must have Administrator privileges.

  6. Manu Says:

    Hi there, I am having a hard time with this sudo comand. I always get the message:
    “2012-05-10 05:09:01.454 TextEdit[875:8107] NSTrackPersistentURLs: LSSharedFileListInsertItemURL() failed at inserting URL file://localhost/etc/hosts (/etc/hosts)”

    I want to change some stuff but can’t seem to open textedit.

    Help!

  7. Sinan Eldem Says:

    Copy the /etc/hosts file to desktop edit with textedit or your editor.
    Copy and paste it back to the /etc/hosts
    You are done!

    This method has always worked for me ;)

  8. trafficking in persons report Says:

    For example: Orange Juice, Vitamin C, helps teeth, bones, awareness and thinking processes.
    Fair Trade Coin Purse made in Cambodia from recycled
    mosquito netting. Is just the time for truth spoken with boldness.

Leave a Reply