Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
1  
What were the reasons you were given as to why using sudo was "wrong"? –  EEAA Oct 12 '12 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 '12 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 '12 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. –  linuxrawkstar Oct 13 '12 at 23:12
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
    
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? –  linuxrawkstar Oct 13 '12 at 23:10
    
@linuxrawkstar That was the bottom line of my answer, yes. –  Ansgar Wiechers Oct 13 '12 at 23:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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