Cannot modify '/etc/hosts' as administrator

Hi all,


I know there're a lot of discussions and solutions to this question, but my case is very strange. I have tried all the methods I've seen on the internet, including

  1. sudo nano /private/etc/hosts
  2. sudo vi /private/etc/hosts
  3. sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts (I encountered an error by running this command: TextEdit[5872:3e07] NSTrackPersistentURLs: LSSharedFileListInsertItemURL() failed at inserting URL file://localhost/private/etc/hosts (/private/etc/hosts) )
  4. Manually change permissions of private and etc foloder through Finder
  5. Use the system preference pane 'Hosts'
  6. Use Gas Mask


All methods I used above have problem overwriting the file (1, 2 said my hosts file is read only and I'm not permitted to overwrite, 5,6 didn't make a change on hosts file after I edited and saved). The account I'm using is an admin account, so I should have the permission to overwrite the hosts file. I used to be able to edit the hosts file and this problem only comes out early this year. I doubt if my hosts file is somehow damaged? Has someone here experienced it and is able to help me figure it out? I would really appreciate it.

Mac Pro, Mac OS X (10.7.5)

Posted on Nov 27, 2012 6:52 PM

Reply
Question marked as ⚠️ Top-ranking reply

Posted on Nov 28, 2012 2:35 PM

This is how I do it.



1 - drag /etc/hosts file to Desktop - with normal permissions this will result in a copy on the Desktop


2 - edit with BBEdit Lite - or any other clean text editor


3 - drag edited version to /etc folder


4 - agree to Authenticate and then Replace - admin password is required for this


5 - Restart to use modified hosts file.



Good luck

18 replies
Sort By: 
Question marked as ⚠️ Top-ranking reply

Nov 28, 2012 2:35 PM in response to hakerjack

This is how I do it.



1 - drag /etc/hosts file to Desktop - with normal permissions this will result in a copy on the Desktop


2 - edit with BBEdit Lite - or any other clean text editor


3 - drag edited version to /etc folder


4 - agree to Authenticate and then Replace - admin password is required for this


5 - Restart to use modified hosts file.



Good luck

Reply

Nov 30, 2012 1:59 PM in response to hakerjack

For reasons only you know, you have locked the hosts file, as well as adding an inappropriate ACL to the etc directory. I'll limit myself to fixing the hosts file. Enter the following command in the same way as before -- NO TYPING:


sudo chflags nouchg /etc/hosts


You'll be prompted for your password, which won't be displayed when you type it. Type nothing in the window except your password. Then quit Terminal.

Reply

Nov 27, 2012 11:43 PM in response to hakerjack

Simplest is to copy the current hosts file to something like host.bak, then drag a copy to the Desktop. Open it in TextEdit, make the changes you want to make to it, save it as plain text, retaining the name hosts. Then, change its permissions with this command:


sudo chown root:wheel "path to file"?hosts


and then run this command:


sudo cp "path to file"/hosts /private/etc/hosts


Restart and your revised hosts file should be in play.


More info in http://hints.macworld.com/article.php?story=20100423134359860


Also, check the More like this box on this page.


FWIW, this is what the original looks like:


##

# Host Database

#

# localhost is used to configure the loopback interface

# when the system is booting. Do not change this entry.

##

127.0.0.1 localhost

255.255.255.255 broadcasthost

::1 localhost

fe80::1%lo0 localhost

Reply

Nov 28, 2012 5:59 AM in response to hakerjack

You should NOT change the permissions on /private and /private/etc. I suggest you run Disk Utility Repair Permissions and hope that it restores the permissions to what they should be. This is what the permissions should be:


/bin/ls -ldeo@ /private /private/etc

drwxr-xr-x@ 6 root wheel hidden 204 Oct 8 2011 /private

com.apple.FinderInfo 32

drwxr-xr-x 104 root wheel - 3536 Nov 22 13:09 /private/etc


"sudo nano /private/etc/hosts" should have worked, as well as the sudo vi command. Can you explain why it did not, as that might indicate either a simple misunderstanding, or an underlying problem.


And finally, can you explain why you think you need to change the 'hosts' file? Maybe there is a better alternative.

Reply

Nov 28, 2012 10:07 AM in response to hakerjack

I've always used BBEdit or TextWrangler to edit the hosts file, since they take care of things such as navigating to those normally invisible places, line endings, etc, and will prompt for authorization.


By the way, an admin account does not automatically have access to everything (the hosts file is ownwed by root).

Reply

Nov 30, 2012 12:46 PM in response to Linc Davis

Hi Linc,


This is what I got:


drwxr-xr-x+ 105 root wheel - 3570 Nov 27 18:44 /private/etc

0: group:admin allow list,add_file,search,add_subdirectory,delete_child,readattr,writeattr,readextat tr,writeextattr,readsecurity


-rw-r--r--@ 1 root wheel uchg 1051 Oct 3 2011 /private/etc/hosts

Kaiang-Jias-MacBook-Pro:~ KJ$ /bin/ls -Ideo@ /private /private/etc


What can you tell from these?


Thanks

Reply

Nov 30, 2012 1:07 PM in response to Neville Hillyer

Hi Neville,


I did that before but it didn't work. After read your comment I tried that again, and this time I paid attention to the error, so when I was trying to replace the file with original hosts file I get error:

The Finder can’t complete the operation because some data in “hosts” can’t be read or written.

(Error code -36)


Do you have any clue about that?


Thanks

Reply

Nov 30, 2012 1:46 PM in response to hakerjack

I don't know how you got that ACL on the /etc folder, but your hosts file is locked (that uchg is the user immutable flag). Only the owner or root can change that flag (the owner in this case is root), so as an admin user you can use sudo:

sudo chflags nouchg private/etc/hosts

Reply

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Cannot modify '/etc/hosts' as administrator

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.