Using sudo to run as another user without password prompt

Posted in: Bailey's Blog- Apr 08, 2013 No Comments

Bit of a strange one this…

Obviously, there are tons of articles around about running commands as root without a password prompt.  But I wanted to run a command as another user and also not have a password prompt.

So in sudoers I had


root ALL=(ALL) ALL
%sudo ALL = NOPASSWD : ALL

The strange thing was that if I ran:


$ sudo ls

it worked fine.  But if I ran


$ sudo -u anotheraccount ls

I was prompted for my password.

The key to working this out was the command


$ sudo -l

which gave me:


kbailey@benelli2:~$ sudo -l
Matching Defaults entries for kbailey on this host:
env_reset
User kbailey may run the following commands on this host:
(root) NOPASSWD: ALL

So it turns out that if you don’t specify a user then it defaults to root.

The answer was to add specific lines for each user – like this:


root ALL=(ALL) ALL
%sudo ALL = NOPASSWD : ALL
%sudo ALL = (beta) NOPASSWD : ALL
%sudo ALL = (deploy) NOPASSWD : ALL
%sudo ALL = (livecopy) NOPASSWD : ALL

This is not really clear anywhere in the man pages – and Googling just finds posts about running commands as root without the password prompt.

No Responses to “Using sudo to run as another user without password prompt”

Leave a Reply