I recently got into trouble because of this.
$sudo vim /etc/motd
[sudo] password for bruce:
bruce is not in the sudoers file. This incident will be reported.
Is there a way to check if I have sudo access or not?
|
|
Run
Man page excerpt:
If your user is only allowed to run specific commands, this command will work, indicating you are allowed to run something with different privileges. While the message looks different when trying to execute a command you're not allowed to in this case (and no mail is sent to root), it's still possible you'll get into trouble if the admins read
To find out what you're allowed to run with different privileges, you can use |
|||||||||||||||||||||
|
|
This is very simple. Run |
|||||||||||||||||||||
|
|
Here is the script-friendly version:
since it won't stuck on the password input if you do not have the You can also set it in a variable like:
|
||||
|
|
|
"Sudo access" comes in flavors. Two primary flavors: First you, or a group your a member of, needs to be setup for sudo access in the /etc/sudoers file. Secondly you need to know your password, or you need to have done a sudo command recently. Recently enough that the timeout hasn't expired. (Fun fact: you can make the time out very long in your sudoer's file.) I often want to test for the second kind of access in the prolog of a script that will need to sudo some steps. When this check fails I can advise the user he needs to enable the 2nd kind of access before running the script.
The -S tells sudo to read the password from stdin. The -p sets an empty prompt. The -K clears the second time of access. Since it sends stderr to /dev/null, it will also check if the user has the first type of sudo access. |
|||
|
|
|
For me, '
was successful for me for the script.
This expression gives Explanation: |
||||
|
|
|
i've got low rank to vote and comment, but i wanted to upvote Gerald Schade's answer, as i've found that the only way previously, and thought that no1 else knows it - til now :D btw my solution:
(from the end of 2015 mwhahaaa) |
|||||
|
|
Gerald Schade's answer here, can still be improved! Use
Here's a complete example of usage in a script:
|
|||
|
|
|
Follow these steps to view the sudoers file. If you're in there, you have sudo. If not, you can add yourself.
|
|||||
|