Can I chown /etc/hosts so my user owns it so I can edit it without sudo?

3

1

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?

Weston Ruter

Posted 2011-11-28T21:43:31.867

Reputation: 145

What editor are you using instead? – Daniel Beck – 2011-11-28T21:47:18.223

2There are text editors such as TextWrangler that can automatically authenticate and edit the file with root privileges. – slhck – 2011-11-28T23:04:02.043

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 – 2011-11-29T14:19:33.737

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 – 2011-12-01T18:46:51.110

Answers

6

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

user1686

Posted 2011-11-28T21:43:31.867

Reputation: 283 655

2Only that won't work with almost all non terminal editors, as they perform atomic saves and need to write files to /etc. – Daniel Beck – 2011-11-28T21:59:07.817

@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. – user1686 – 2011-11-28T22:06:07.367

1

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.

Joe P

Posted 2011-11-28T21:43:31.867

Reputation: 11