I want to ask the user for sudo password at the beggining of my script and then make a test if this password is correct before running the rest of the script.
So after searching a while, I got this test script:
SUDO_PWD=whatever
sudo -k # <- So I can test it
echo $SUDO_PWD | sudo -Svp '' 2>/dev/null
sudo_response=$(SUDO_ASKPASS=/bin/false sudo -A whoami @>/dev/null)
if [ "$sudo_response" = "root" ]; then
echo "sudo"
else
echo "no sudo"
fi
But both messages appear in console even with @>/dev/null redirection. Why ?
echo $SUDO_PWD
will show the password to anyone and everyone able to runps
?!?! – Andrew Henle Nov 14 '22 at 12:55sudo
? That way it's not you and your script that is asking the user for their password and storing it in a variable, butsudo
. – Kusalananda Nov 14 '22 at 12:58@>/dev/null
does, and for which shell? – Chris Davies Nov 14 '22 at 14:18sleep infinity
in the end andwatch ps -eF | grep <mysudopassword>
and it didn't show anything. If there some ps option that may expose the password, please let me know. :) – Nelson Teixeira Nov 15 '22 at 07:22@> /dev/null
should redirect all outputs (error and stdout) to/dev/null
. Isn't that it ? – Nelson Teixeira Nov 15 '22 at 07:28&>
. – Kusalananda Nov 15 '22 at 07:38&>
not@>
– Chris Davies Nov 15 '22 at 08:01