[sudo-users] Why does sudo return success for bad password?

Jeffrey Walton noloader at gmail.com
Fri Mar 20 22:30:41 MDT 2020


Hi Everyone,

Please forgive my ignorance. I don't see a discussion of return values
in the sudo(1) man page.

I'm trying to smoke test an optional user password in a script. The
script can be long running, so testing the user's password before hand
makes for a good UI experience.

The following is reporting success even with a bad password:

    if [ -n "$SUDO_PASSWORD" ]
    then
        if printf "%s\n" "$SUDO_PASSWORD" | sudo -S ls 2>&1;
        then
            :
        else
            echo "It appears the sudo password is incorrect"
            ...
        fi
    fi

As I understand things, the exit status of the pipeline is the exit
status of the last command in the pipeline.

How can I obtain an accurate result of the 'sudo ls' command?


More information about the sudo-users mailing list