Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ostatesman

macrumors newbie
Original poster
Sep 10, 2011
1
0
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?
 
Last edited:
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?

It seems to be an issue with Lion. Just use Gas Mask to edit the hosts file. Much easier.
 
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?
 
It seems to be an issue with Lion. Just use Gas Mask to edit the hosts file. Much easier.

+1. In Lion you can no longer edit the file using TextEdit.

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

As costabunny says, make sure you're using an admin account.
 
+1. In Lion you can no longer edit the file using TextEdit.



Yes you can, just save and duplicate the file without extension.
 
+1. In Lion you can no longer edit the file using TextEdit.
Yes you can, just save and duplicate the file without extension.

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. :)
 
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?
 
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?

I never noticed the problem. I use VI.

Code:
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.
 
Have you tried
Code:
sudo nano /private/etc/hosts

+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:

Code:
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.
 
Hi

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!
 
As mentioned you need to open the file under sudo

It is just a security feature

Code:
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)

:wq

that will write and quit the file

to quite without saving its, :q!

to write it's just :w
 
+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:

Code:
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.

Lokheed,

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

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

S-
 
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). :(
 
You typed it in terminal right?
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 :)
 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.