1

When running commands as other users from the command line, what is recommended best practice? In the past I've used sudo like so: sudo -u username command [args]

I've been told (with no specific reasons why) that using sudo for this purpose is wrong. I'd like to know why. Is there some "best way" to accomplish this? For example, I've also used the su command like so: su username - -c "command [args]"

I can't imagine why either of these methods would be "bad". Your thoughts?

4
  • 1
    What were the reasons you were given as to why using sudo was "wrong"?
    – EEAA
    Oct 12, 2012 at 15:23
  • 1
    sudo is fine to me, but I would suggest you add a -i as in sudo -u username -i command. This make sudo user the user's environment. You can get weird results with some coomands if you don't do that.
    – Zoredache
    Oct 12, 2012 at 15:33
  • I would like to reiterate, sudo is fine. In fact I would recommend that instead of any other method I can think of.
    – Lipongo
    Oct 12, 2012 at 18:39
  • As originally stated, I wasn't given a reason why running commands as another user with SUDO was bad. I tossed the idea out on another website (which shall not be named), and it was downvoted into oblivion... leading me to believe that there was an abundant reason not to use sudo for this purpose. My suspicions however seem more in line with what I'm reading here at serverfault -- there's nothing wrong with using sudo for this purpose at all.
    – RaWkStAr
    Oct 13, 2012 at 23:12

1 Answer 1

1

The main difference between su username and sudo -u username is that for the former you need to supply the password of username whereas for the latter you need to supply your own password. Neither of the two approaches is inherently better or worse than the other, as others have already pointed out.

2
  • If I'm already running as root, neither the su, nor the sudo command require a password. Having said that, I guess there's no technical reason why one is superior to the other?
    – RaWkStAr
    Oct 13, 2012 at 23:10
  • @linuxrawkstar That was the bottom line of my answer, yes. Oct 13, 2012 at 23:31

You must log in to answer this question.

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