Skip to main content
amended title and much of the first half of the question -- still unclear what he wants
Source Link

How to passwordless check with a commandto see if ana user can use 'sudo' but without writing the password?

I want to check through a command if ana user is able to use sudo - but without the need to write the password - it for bash shell scripts purposes. I already did do asome research and I found:

For ana user that does not have a configuration through in sudoers - therefore is 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 but either if--whether sudo was not executed in the session or the sudo's timeout expired -- it prints again the sudo: a password is required message.

Even moreAdditionally, with the correct solution (but for the other scenario)

How to check with a command if an user can use 'sudo' but without writing the password?

I want check through a command if an user is able to use sudo - but without the need to write the password - it for bash shell scripts purposes. I already did do a research and I found:

For an user that does not have a configuration through in sudoers - therefore is impossible 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 but either if sudo was not executed in the session or the sudo's timeout expired it prints again the sudo: a password is required message.

Even more, with the correct solution (but for the other scenario)

passwordless check to see if a user can use 'sudo'

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)

Add an example
Source Link
Manuel Jordan
  • 2.2k
  • 4
  • 26
  • 61

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

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

Add Goal section
Source Link
Manuel Jordan
  • 2.2k
  • 4
  • 26
  • 61

I want check through a command if an user is able to use sudo - but without the need to write the password - it for bash shell scripts purposes. I already did do a research and I found:

For an user that does not have a configuration through in sudoers - therefore is impossible 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 but either if sudo was not executed in the session or the sudo's timeout expired it prints again the sudo: a password is required message.

Even more, 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.

I want check through a command if an user is able to use sudo - but without the need to write the password - it for bash shell scripts purposes. I already did do a research and I found:

For an user that does not have a configuration through in sudoers - therefore is impossible 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 but either if sudo was not executed in the session or the sudo's timeout expired it prints again the sudo: a password is required message.

Even more, 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.

I want check through a command if an user is able to use sudo - but without the need to write the password - it for bash shell scripts purposes. I already did do a research and I found:

For an user that does not have a configuration through in sudoers - therefore is impossible 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 but either if sudo was not executed in the session or the sudo's timeout expired it prints again the sudo: a password is required message.

Even more, 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.

Became Hot Network Question
Source Link
Manuel Jordan
  • 2.2k
  • 4
  • 26
  • 61
Loading