I want to check if a user is able to use sudo - but without the need to write the password - for bash shell scripts purposes. I did some research and I found:
For a user that does not have a configuration through in sudoers - therefore impossible to use sudo - the execution of sudo -n <command> always prints in the terminal the sudo: a password is required message. And if the user can use sudo --whether sudo was not executed in the session or the sudo's timeout expired -- it prints again the sudo: a password is required message.
Additionally, with the correct solution (but for the other scenario)
$(sudo -n uptime 2>&1 | grep "load" | wc -l)
For an user without sudo permission always returns 0. And if the user can use sudo and if the sudo's timeout is still valid - it returns 1, otherwise it returns 0
Problem: so with this approach is not possible really know through a command know if the user can use sudo - the solution is only viable to know if the sudo's timeout is still valid or not.
With sudo -v so far almost the solution happens the following:
For an user that does not have a configuration about sudoers the command returns
Sorry, user <username> may not run sudo on <hostname>.
If the user can use sudo then the sudo -v asks for the password.
If either it is valid or sudo's timeout is still valid it returns empty. So until here there is a clear difference and the solution but because I am working with Bash Shell and I need use Command Substitution
is_user_sudo=$(sudo -v) # if is empty sudo can be used otherwise not
Problem I want avoid the password prompt part. That would be a solution.
Or if exists other parameter such as sudo -k that does not ask for the password (of course it does other thing) or other command to accomplish my goal.
Goal: only the non-root users - that can use sudo - can be able to execute some bash scripts. Therefore internally in the script I need the command to identify that type of non-root user - so the non-root users that can't not use sudo must be notified and stop the script execution.
For example:
# an if statement about 'id -u' not equals a 0
can_use_sudo=$(<command> whoami) # so the non-root user
# is able to use sudo or not?
whoami can be replaced by other command, that identifies who is running the script
sudouser will have to enter a password to run the command, so why do you object to the password prompt?