Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a local shell script that performs a number of tests on a remote host, before delivering the payload; one of these tests being whether the user has sudo privileges, checked simply with sudo -v however this requires the user to enter their password. Additionally the remote host seems to have instant sudo timeout so the password entry is required on every new connection, and this is something I don't have permission to change (as a policy).

I can of course test whether the user is part of certain groups, but then this would not be agnostic to the remote host configuration, so I was hoping there's a method that can check that doesn't need to assume the user's groups, as well as not needing user input?


UPDATE: To echo my comments, I only want to test whether a user could possibly sudo, without requiring user interaction for that test.

share|improve this question
do you need to check wether the user has the possibility to use sudo, or if he is executing the shell script via sudo? – Niko S P Feb 28 '12 at 4:51
Just whether he has the possibility. The local script will require the sudo password to be entered later on, so I just want to check early on that the user on the remote host is even a sudoer. – DanH Feb 28 '12 at 9:32
a quick check on my ubuntu box gave me sudo -l, it returns the commands the user may run, if (ALL) ALL is part of them, the user is able to use sudo for any command. Maybe that's the right angle? – Niko S P Feb 28 '12 at 11:16
sudo -l prompts me for my password. – ThatGraemeGuy Feb 28 '12 at 11:55
Sudo caches based on tty so a if a new session gives you the same tty you may not be prompted. Try executing sudo -k first. – Mark Wagner Mar 6 '12 at 20:39

I'm afraid the only thing you can test is if the user has sudo privileges without a password.


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

share|improve this answer
Note that this does not help preliminary detecting if command X is sudoable without entering a password and without accidently running it – try-catch-finally Jul 2 '14 at 23:05

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.

share|improve this answer

sudo -l

That should give you enough to decide if you have the privs you want/need.

share|improve this answer

Your Answer


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.