Test whether a user has sudo privileges without requiring user input
I'm afraid the only thing you can test is if the user has sudo privileges without a password.
Execute
sudo -n true
If $? is 0, the user has sudo access without a password, if $? is 1, the user needs a password.
If you need verification for a specific program, change true
with your program, in a way the program doesn't do anything, like chmod --help
If you have one user with sudo access, like "root", you can use it to check other logins. As the user with access run:
sudo -n -l -U foo 2>&1 | egrep -c -i "not allowed to run sudo|unknown user"
If it returns zero, "foo" has access. Otherwise, it doesn't have sudo access.
sudo -l
That should give you enough to decide if you have the privs you want/need.
I know that this is a super old question, but I found luck with the -n
(non-interactive) flag and -v
/ -l
. But, you do have to inspect the output:
$ sudo -vn && sudo -ln #User with cached credentials
User adminuser may run the following commands on computername:
(ALL) ALL
$ sudo -vn && sudo -ln #User who _can_ sudo but isn't cached
sudo: a password is required
$ sudo -vn && sudo -ln #User who can't at all
Sorry, user nonadmin may not run sudo on computername.
Some output-redirection and grepping will get you there, probably:
if (sudo -vn && sudo -ln) 2>&1 | grep -v 'may not' > /dev/null; then
#they're cool
exit 0 #Or, whatever
fi