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 was wondering how to run a command as another user from a script without password. sudo -l return: (root) NOPASSWD: /bin/su - someuser

There is no problem to sudo as someuser. Command sudo su - someuser works without password. But every time I try to execute command as this user I'm prompted for password. I tried both: sudo su -c pwd someuser sudo -u someuser pwd

Are there any alternatives? What is the cause of the problem? I don't have root access.

share|improve this question
    
Do you use sudo su - someuser as someuser? That should prompt for the sudo password, as well as the others. In my computer it does. It's Linux though. –  JMCF125 Feb 25 at 19:32
    
No, I use this as my own user. There is no question about password. –  PrzeM Feb 25 at 20:03
    
I used this as my own user as well. It prompted for the sudo password. What do you mean then? –  JMCF125 Feb 25 at 20:22

3 Answers 3

up vote 2 down vote accepted

I might be stating the obvious, but I think the answer is in the config.

You've mentioned your config shows

 (root) NOPASSWD: /bin/su - someuser

So you are permissioned to run only one command ie /bin/su - someuser as root (so this avoids su prompting for a password) and the NOPASSWD: means sudo will not ask you for a password to do it.

But you want to run other commands as someuser, (directly from your own shell) sudo -u someuser somescript But that's not configured.

You want, sudo -l to show something along the lines of

(root) NOPASSWD: /bin/su - someuser
(someuser) NOPASSWD: /bin/ls, /usr/bin/whoami, /home/someuser/bin/ascript

(above output may not be 100% as it will be displayed but hope you can understand what I mean)

The way you are configured at the moment, means you must first su to someuser and then run commands as that user.

It sounds like you don't have admin control over this machine. Maybe develop the script as the someuser, then you'll have a list of commands and a script tested, to got back to your admin, to add to /etc/sudoers.

share|improve this answer
1  
That is correct answer. One line in sudoers file is missing. (someuser) NOPASSWD: /bin/ls, /usr/bin/whoami, /home/someuser/bin/ascript –  PrzeM Feb 25 at 21:05

it's possible to remove sudo password prompt for non-root users by adding (or uncommenting existing) line in /etc/sudoers file

it's also possible to create separate files for users in /etc/sudoers.d/ directory


adding passwordless sudo users:

someuser ALL=(ALL) NOPASSWD: ALL

the same thing for groups:

%somegroup ALL=(ALL) NOPASSWD: ALL

these lines can be added (appended) to /etc/sudoers file, or to separate ascii (text) files in /etc/suedoers.d/ directory

share|improve this answer
1  
Could you show which line in sudoers, and what the file in sudoers.d would look like, to help OP, What do you think is at fault in the original post. –  X Tian Feb 25 at 19:17
    
I think my user exist in the sudoers. sudo -l return: (root) NOPASSWD: /bin/su - someuser. –  PrzeM Feb 25 at 20:08

If command sudo su - someuser works without password then you should be able to do this:

sudo -u someuser command

If that does not work, then it is probably set to only allow su command without a password. Thus, try this:

sudo su -c "COMMAND1; COMMAND2" -m "USER"

e.g.

sudo su -c "cd /home/user ; mkdir testdir" -m "user" 
share|improve this answer
    
It's true. But I tried and did not work. –  PrzeM Feb 25 at 19:59
    
see updated answer please –  edvinas.me Feb 25 at 21:10
    
It prompted for the password. –  PrzeM Feb 26 at 15:15

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.