2

On a Unix system, what is the best practice for editing system files such as /etc/hosts and php.ini as non-root without doing the tedious sudo+password?

Can I simply chown those files to my non-root-user, or is there a better way using groups or something?

1
  • 1
    I am not sure why you'd want to, those aren't files people typically edit frequently enough to need to bypass the protections that are in place for a reason. And no, don't change the ownership...
    – nerdwaller
    Mar 29 '13 at 18:30
5

sudo is the best and proper way to do it. If you give your user account too many administrative privileges (for example, adding yourself to the root group), you open yourself up to accidentally doing all sorts of harmful things. Also, malware/trojans could use your privileges without your knowledge to do nasty things. Just think of typing sudo before a command as an extra check - "Do I really want to run this?"

You can set up sudo (it should already be this way) to wait a specified length of time before asking for your password again, so if you're doing a bunch of admin tasks all at once you don't need to keep entering it.

0
2

In addition to MattDMo's answer. If retyping your password is bothering you, you can add NOPASSWD to your privileges. This will still require you to sudo (so you can be aware that you are doing something that could be harmful), but won't bother you for typing your password.

In /etc/sudoers, you can use this line:

forthrin ALL=(ALL) NOPASSWD: ALL

1
  • This basically makes your user account like root's account and gives you far more privileges than just the ability to edit that specific file. Nov 11 '19 at 13:18
1

Another option is to open a new terminal and switch to root. Depending on how your system is set up you will be able to do this using one or more of the following ways:

  • sudo su
  • sudo -i
  • su

Once you are the root user, simply keep thyat terminal open and edit any files you want from that terminal using emacs or vim or gedit or whatever you prefer:

emacs /etc/hosts
1
  • Or even sudo -s.
    – oKtosiTe
    Mar 30 '13 at 11:10
1

sudo is what you want. As specified before, you can set custom timeouts before asking for a password again or specify NOPASSWD: This can also be set for only a specific usergroup and command like this:

%usergroup ALL=(ALL) NOPASSWD: /bin/vi

You could limit this command to a certain file by pointing to a script that runs vi /etc/hosts, but this would allow users to open other files as well from inside vi or drop into a shell from vi (:!sh or a similar command).

1
  • 1
    +1 for pointing out that once you allow sudo vi you've basically given user root access. Nov 11 '19 at 13:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.