Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

It's a bit inconvenient to have to sudo emacs /etc/hosts whenever I want to make a change to my hosts file, especially since I don't use a Terminal-based editor primarily. Could I change the ownership of my hosts file so that my user is the owner and thus the file could be edited in any text editor I open? Would that cause any problems for OS X?

share|improve this question
    
What editor are you using instead? – Daniel Beck Nov 28 '11 at 21:47
2  
There are text editors such as TextWrangler that can automatically authenticate and edit the file with root privileges. – slhck Nov 28 '11 at 23:04
    
Have you tried other terminal-based text editors? As well as emacs, Mac OS X should have vim (cue holy war) ed (of course) and nano installed already, and many more can be acquired. Personally I find emacs far too heavyweight for small changes to configuration files, and thus prefer nano. – Scott Nov 29 '11 at 14:19
    
I'm using ActiveState Komodo Edit. I wish it did what TextWrangler and Coda did with prompting for sudo privileges. I used to use nano to edit my hosts file, but recently have been using emacs. However, it's not the primary editor I use, so I'd rather have some consistency and just use Komodo. – Weston Ruter Dec 1 '11 at 18:46
up vote 4 down vote accepted

Yes, you can1. No, you definitely should not. It won't break your OS X by itself, but...

...having system configuration files restricted from writing by non-admins is precisely how Unix systems have resisted most malware. On the other hand, in older versions of Windows you could often find popular sites redirected to a password-stealing site or a completely nonexistent server.


1 If you're going to do it, better just add yourself to the access list instead of changing ownership: sudo chmod +a "$USER allow read,write" /etc/hosts

share|improve this answer
2  
Only that won't work with almost all non terminal editors, as they perform atomic saves and need to write files to /etc. – Daniel Beck Nov 28 '11 at 21:59
    
@DanielBeck: And neither will chown, unless the user has +write permission on the whole /etc. At which point I just let the user continue with breaking their machine by themselves. – grawity Nov 28 '11 at 22:06

If you're editing the hosts file that frequently, why not just create an alias? Add something like alias changehosts="sudo emacs /etc/hosts to your login profile file, and it will be even shorter to type. You can even add a NOPASSWD directive in sudoers so you wont have to type your password (either ever, or specifically to emacs /etc/hosts). Fundamental security doesn't really have to come at the expense of productivity.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .