OSX 10 Lion hosts file issue

Discussion in 'Mac OS X Lion (10.7)' started by ostatesman, Sep 10, 2011.

  1. ostatesman, Sep 10, 2011
    Last edited: Sep 10, 2011

    ostatesman macrumors newbie

    Sep 10, 2011
    I am having what appears to be a permissions issue with the hosts file. I can open it via text editor or terminal but can't write it to save the changes I have made.

    I have tried using the recommended procedures for dealing with the issue but can't seem to get any to work.

    In terminal I get the error [ Error writing /etc/hosts: Operation not permitted ]

    despite the fact I used the sudo command to open the file in the first place.

    Any ideas?
  2. Janneman59 macrumors newbie


    Sep 18, 2009
    Have you tried
    sudo nano /private/etc/hosts
  3. axu539 macrumors 6502a

    Dec 31, 2010
    It seems to be an issue with Lion. Just use Gas Mask to edit the hosts file. Much easier.
  4. costabunny macrumors 68020


    May 15, 2008
    Weymouth, UK
    works on on both my lions. (just saying so it isnt all lions)

    Have you tried opeing a terminal window. 'sudo su -' (change to root user) then 'cd /etc/hosts' followed by 'vi hosts' (replace vi with your chosen editor if you like)...

    Does that allow you to save the file?

    can you (as root) run an 'ls -la /etc/hosts' and see what it shows?

    it should let you open, edit and write with 'sudo nano /etc/hosts' but may changing to root and then editing as superuser will work.

    Have you got any AV, Spyware, etc running that might be blocking changes to the hosts file?
  5. killerrobot macrumors 68020


    Jun 7, 2007
    +1. In Lion you can no longer edit the file using TextEdit.

    As costabunny says, make sure you're using an admin account.
  6. Asclepio macrumors 6502a


    Jul 11, 2011
    +1. In Lion you can no longer edit the file using TextEdit.

    Yes you can, just save and duplicate the file without extension.
  7. killerrobot macrumors 68020


    Jun 7, 2007
    Let me rephrase that - you can no longer use the terminal command to open the host file in textedit, edit it, then save it in one easy process as you could in SL. :)
  8. threesecondmemo macrumors newbie

    Sep 22, 2011
    I am having this exact same issue. Any ideas on how to get around it? Tried everything posted so far. Same error.

    Could someone talk me through how to do it in GasMask?
  9. r0k macrumors 68040


    Mar 3, 2008
    I never noticed the problem. I use VI.

    cd /private/etc
    sudo vi hosts
    enter your password when prompted

    Inside vi, you start off in command mode. Type i and you are in insert mode. Hit escape after typing (or not typing) and you are back in command mode. :$ puts you at the end of the file. O opens a line above and o opens a line below. i inserts at the cursor and I inserts at the beginning of the current line. A inserts at the end of the current line and a inserts at cursor. :q quits. :q! quits regardless of the fact you might not have saved your work. :wq writes and quits. That's it. You are now a vi user and you can rest assured vi is installed on every Unix box on this planet. No spazzing out while you look for nano, pico or textedit or cough-cough wordpad. VI is just there. Learning a half dozen : commands and learning to tell whether you are in insert or command mode isn't such a high price to pay when you consider you will always have vi.
  10. phyrexia macrumors 6502a

    Sep 3, 2010
    works just fine.

    pico and nano are much less powerful than vi but IMO much easier to use for the newb.
  11. Lokheed macrumors regular

    Jun 10, 2009
    +1. If you can't use command line, you really have no business in there (or at the very least, sudo).

    And you don't need anything to open it using Text Editor. Just run TE as root using:

    sudo "/Applications/TextEdit.app/Contents/MacOS/TextEdit"
    It'll launch. Go to File > Open and you'll be greeted with a nice and open Finder window that doesn't even hide the core folders. So go to etc and then select "hosts". Boom done.
  12. maflynn Moderator


    Staff Member

    May 3, 2009
    I use sudo pico on the host file and that does the job.
  13. DompiE macrumors newbie

    Sep 29, 2011

    I tried everything but i can't edit my hosts file anymore (it worked once 3 weeks ago).
    I tried:
    - everything through terminal (sudo nano/vi/pico)
    - terminal --> /etc/hosts and /private/etc/hosts
    - logging in as root and even root can't edit the file

    tried costabunny's approach, result:
    -rw-r--r--@ 1 root wheel 3678 Sep 4 20:30 /etc/hosts

    Is this a privilige question or did i something the first time I edited the hosts file?

    Thanks in advance!
  14. bp1000 macrumors 65816

    Jul 7, 2011
    As mentioned you need to open the file under sudo

    It is just a security feature

    sudo vi /etc/hosts
    Keyboard only....

    Press i (this will enter insert mode)

    Make your changes / add your ip's hosts etc

    then press escape to come out of insert mode

    Then just type (you will see at the bottom)


    that will write and quit the file

    to quite without saving its, :q!

    to write it's just :w
  15. sidewinder macrumors 68020


    Dec 10, 2008
    Northern California

    You can't edit the "/etc/hosts" file with TextEdit anymore.

    You have to use "nano" or my personal preference, "vi".

  16. DompiE macrumors newbie

    Sep 29, 2011
    As I said i tried everything with sudo. (vi, nano, pico)

    but I tried again:

    bp1000: vi says: can't write to file (after nagging about its read only). :(
  17. bp1000 macrumors 65816

    Jul 7, 2011
    You typed it in terminal right?
  18. DompiE macrumors newbie

    Sep 29, 2011
    Of course :)

    I first thought it's a problem of permissions, but if I'm root (which has al write and read permissions) I can't edit hosts either.

    So know I think that the file is maybe in use or something like that, so I can't edit.
    Is there a way to see what processes are using hosts atm?

    I really don't know it anymore :)
  19. DompiE macrumors newbie

    Sep 29, 2011
    Well problem solved...

    I did the permission recovery in the disk utility, then the permission check and restart.
    then the sudo nano /etc/hosts and it worked.

    Really don't know what the problem was... :)

    Greetz and thanks for answers

Share This Page