I've got a program that runs once and removes itself from sudoers when finished. And I've got some scripts that call that function from an ssh session. What I'd like is for these commands not to hang up at the password prompt when the command I want to run is not in sudoers.

Is there a way to override the SUDO_ASKPASS function to just bomb out so I can skip running things that would otherwise present me with password prompts?

My best attempt was something like:

SUDO_ASKPASS=/bin/false sudo -A /opt/sbin/mycommand

but it left me with

sudo: pam_authenticate: Conversation error

I basically want sudo to never prompt me for a password regardless of whether the command I'm running is in sudoers.

My lazybones fix is to remove the -t from my ssh command and hope that I never need to actually use the terminal.

Another fix, I guess, would be just to toast my script that is in sudoers and make it print an ASCII thumbs-up or something useful.

Is there a straightforward way around this?


You can ask sudo -l. It returns either the full path of the command to be executed, or it exits with non-zero status. Here, we ignore the output and check the status return:

if sudo -l somecommand... >/dev/null
    echo "This is permitted" >&2
    echo "You are out of luck today" >&2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.