Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a really basic confusion. How does sudo work?

I noticed that when I ran a script it failed, since it tried to write a file in a directory that the following permissions:

$ ls -l thedir
drwxr--r-- usr grp

But when I run the script as sudo -u usr it worked.

If I am able to do run the script successfully with sudo, why did the script fail in the first place?

I mean if I have enough rights to run as that user I should be able to run the script succesully in the first place, I guess.

share|improve this question
    
I have a few areas you could look in, but I'd like to know what platform this is on. I can see the Linux tag, but no mention of a distro. –  Joel Davis Jul 2 '13 at 20:56
    
@JoelDavis:I think it is suse. But not 100% sure as I connect via ssh and can not check right now –  Jim Jul 2 '13 at 21:01
    
cat /etc/SuSE-release should tell you the exact version of SuSE (after which it would probably be a good idea to update the original question with that info) –  Joel Davis Jul 2 '13 at 21:03
    
I think DavidG and I misunderstood the question. At first I thought you were saying that merely running the command through sudo causes it to work for the same account, but now I'm seeing a different type of question. Are you just asking how sudo does what it does and not running into an error? –  Joel Davis Jul 2 '13 at 21:09
    
@JoelDavis:I know that with sudo you can run as another user.I can not understand how in this particular case it works –  Jim Jul 2 '13 at 21:17

2 Answers 2

Just because you have the ability to act as another user, this does not give your current user the same permissions. Permissions do not cascade to your current user based on the ability of your current user account to act as another user. sudo and su allow you to "become" or "act as" another user. In other words, when you sudo you are actually becoming that user for a moment.

Even if you log into the machine as root and then sudo or su to another user, as that user you will have all the same restrictions (and permission) because to the system you are that user, not root.

share|improve this answer

I think the first thing would be to figure out if the script is running as you.

#!/bin/bash
whoami

Call it in the same way you call the script you are working on (e.g. cronjob). That might give you a clue in case the script is running as another user.

share|improve this answer

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.