How to check if I have sudo access?












84














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?










share|improve this question
























  • Ask your systems administrator?
    – mdpc
    Feb 18 '13 at 19:40






  • 1




    @mdpc: Is there another way besides that?
    – Bruce
    Feb 18 '13 at 19:45










  • You have not mentioned if you can attain root access or not.
    – mdpc
    Feb 18 '13 at 19:46






  • 38




    This has to be the first instance of seeing someone following up on "This incident will be reported".
    – slhck
    Feb 18 '13 at 19:55


















84














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?










share|improve this question
























  • Ask your systems administrator?
    – mdpc
    Feb 18 '13 at 19:40






  • 1




    @mdpc: Is there another way besides that?
    – Bruce
    Feb 18 '13 at 19:45










  • You have not mentioned if you can attain root access or not.
    – mdpc
    Feb 18 '13 at 19:46






  • 38




    This has to be the first instance of seeing someone following up on "This incident will be reported".
    – slhck
    Feb 18 '13 at 19:55
















84












84








84


21





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?










share|improve this question















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?







linux sudo sudoers






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 20 '14 at 21:10









Vldb.User

31




31










asked Feb 18 '13 at 19:36









Bruce

92241625




92241625












  • Ask your systems administrator?
    – mdpc
    Feb 18 '13 at 19:40






  • 1




    @mdpc: Is there another way besides that?
    – Bruce
    Feb 18 '13 at 19:45










  • You have not mentioned if you can attain root access or not.
    – mdpc
    Feb 18 '13 at 19:46






  • 38




    This has to be the first instance of seeing someone following up on "This incident will be reported".
    – slhck
    Feb 18 '13 at 19:55




















  • Ask your systems administrator?
    – mdpc
    Feb 18 '13 at 19:40






  • 1




    @mdpc: Is there another way besides that?
    – Bruce
    Feb 18 '13 at 19:45










  • You have not mentioned if you can attain root access or not.
    – mdpc
    Feb 18 '13 at 19:46






  • 38




    This has to be the first instance of seeing someone following up on "This incident will be reported".
    – slhck
    Feb 18 '13 at 19:55


















Ask your systems administrator?
– mdpc
Feb 18 '13 at 19:40




Ask your systems administrator?
– mdpc
Feb 18 '13 at 19:40




1




1




@mdpc: Is there another way besides that?
– Bruce
Feb 18 '13 at 19:45




@mdpc: Is there another way besides that?
– Bruce
Feb 18 '13 at 19:45












You have not mentioned if you can attain root access or not.
– mdpc
Feb 18 '13 at 19:46




You have not mentioned if you can attain root access or not.
– mdpc
Feb 18 '13 at 19:46




38




38




This has to be the first instance of seeing someone following up on "This incident will be reported".
– slhck
Feb 18 '13 at 19:55






This has to be the first instance of seeing someone following up on "This incident will be reported".
– slhck
Feb 18 '13 at 19:55












8 Answers
8






active

oldest

votes


















106














Run sudo -v. It is usually used to extend your sudo password timeout, but can be used for determining whether you have any sudo privileges.



$ sudo -v
Sorry, user [username] may not run sudo on [hostname].


Man page excerpt:




If given the -v (validate) option, sudo will update the user’s time stamp, prompting for the user’s password if necessary. This extends the sudo timeout for another 5 minutes (or whatever the timeout is set to in sudoers) but does not run a command.




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 /var/log/secure.



$ sudo ls
[sudo] password for [username]:
Sorry, user [username] is not allowed to execute '/bin/ls' as root on [hostname].


To find out what you're allowed to run with different privileges, you can use sudo -l. Note that this command requires you to enter your password.






share|improve this answer



















  • 2




    Thanks. sudo -v works for me. The man page says I can run sudo -l as well but that asks for a password. Why is that?
    – Bruce
    Feb 18 '13 at 20:00






  • 2




    @Bruce I'm guessing here, but otherwise someone (or a program you run) could find out what programs can be executed (possibly without entering password) by your current user and try to use that information maliciously.
    – Daniel Beck
    Feb 18 '13 at 20:05










  • What do you suppose it means when I get this back: patrick@<host>:~$ sudo -v sudo: unable to resolve host <host>? I entered my password and didn't get anything about unauthorized. I know I have sudo from successfully running other commands, but that unable to resolve host message has me concerned something else might be funky on the host.
    – Patrick M
    Apr 21 '14 at 3:04










  • @PatrickM It looks like a problem with the sudoers file. In there you can specify on which host a user is authorized to run a specific command (this is useful when using the same sudoers file on multiple machines). Possibly the hostname specified in that file could not be resolved. Try checking it with the host command for example.
    – Ale
    Dec 17 '14 at 23:10










  • Doesn't work for me on RHEL 6, sudo -v gave "xx is not in the sudoers file. This incident will be reported."
    – 79E09796
    Sep 1 '16 at 17:03



















39














This is very simple. Run sudo -l. This will list any sudo privileges you have.






share|improve this answer



















  • 1




    Maybe downvoted because it repeats what Daniel Beck said nearly two years ago.
    – G-Man
    Dec 18 '14 at 4:09






  • 1




    Or explains what happen, it's a comment, at best
    – Ramhound
    Dec 18 '14 at 20:47






  • 2




    @Jonathan: if u would script in ubuntu rigt now, sudo -l asks for a password if u can sudo or not. sudo -v asks only if u can, and "$(whoami)" != "root" will never ask anything in any linux.
    – bksunday
    Aug 3 '15 at 3:37










  • @bksunday You are correct. I tested now on a clean Debian Jessy and confirmed your results. My previous (deleted now) comment was probably a result of testing on a machine on which I had some sudo privs.
    – Jonathan Ben-Avraham
    Aug 3 '15 at 4:30










  • @G-Man but this simple answer helped me more than probably more precise Daniel's answer, where this command is the the very end unfortunatelly...
    – Betlista
    Jan 4 '16 at 14:48



















8














Here is the script-friendly version:



timeout 2 sudo id && echo Access granted || echo Access denied


since it won't stuck on the password input if you do not have the sudo access.



You can also set it in a variable like:



timeout 2 sudo id && sudo="true" || sudo="false"
echo "$sudo"


Note: On macOS, you need to install coreutils, e.g. brew install coreutils.






share|improve this answer























  • Any alternatives for where timeout isn't available by default, e.g. on OS X?
    – Harry
    Jun 1 at 16:27






  • 1




    You need to install coreutils, e.g. brew install coreutils.
    – kenorb
    Jun 13 at 15:08










  • This does not work for me in a script. For unexplained reason the script hangs until I kill it.
    – beruic
    Oct 11 at 11:04





















1














For me, 'sudo -v' and 'sudo -l' did not work in a script because sometimes interactive (asking me for a password, like mentioned above).
'sudo -n -l' did also not work, it gave the exit code '1' although I have sudo permissions, because of the missing password.
But extending the command to:



A=$(sudo -n -v 2>&1);test -z "$A" || echo $A|grep -q asswor


was successful for me for the script.
This expression gives 0 if the current user can call 'sudo' and 1 if not.



Explanation:

The additional parameter -n to sudo prevents interactivity.

The output $A of the command 'sudo -n -v 2>&1' may be:

- empty (in this case, sudo can be called by the current user), or:

- a note that the current user is not authorized for sudo, or:

- a question text for the password (in this case, the user is authorized).

("asswor" will fit for an english "password" as well as for a German "Passwort").






share|improve this answer































    1














    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:



    [[ "$(whereis sudo)" == *'/'* && "$(sudo -nv 2>&1)" != 'Sorry, user'* ]]


    (from the end of 2015 mwhahaaa)






    share|improve this answer

















    • 1




      Read over "Why do I need 50 reputation to comment" to ensure you understand how you can start commenting.
      – Pimp Juice IT
      Oct 1 '17 at 23:24



















    1














    Gerald Schade's answer here, can still be improved!



    Use



    prompt=$(sudo -nv 2>&1)
    if [ $? -eq 0 ]; then
    # exit code of sudo-command is 0
    echo "has_sudo__pass_set"
    elif echo $prompt | grep -q '^sudo:'; then
    echo "has_sudo__needs_pass"
    else
    echo "no_sudo"
    fi


    Here's a complete example of usage in a script:



    #!/usr/bin/env bash

    is_root () {
    return $(id -u)
    }

    has_sudo() {
    local prompt

    prompt=$(sudo -nv 2>&1)
    if [ $? -eq 0 ]; then
    echo "has_sudo__pass_set"
    elif echo $prompt | grep -q '^sudo:'; then
    echo "has_sudo__needs_pass"
    else
    echo "no_sudo"
    fi
    }

    elevate_cmd () {
    local cmd=$@

    HAS_SUDO=$(has_sudo)

    case "$HAS_SUDO" in
    has_sudo__pass_set)
    sudo $cmd
    ;;
    has_sudo__needs_pass)
    echo "Please supply sudo password for the following command: sudo $cmd"
    sudo $cmd
    ;;
    *)
    echo "Please supply root password for the following command: su -c "$cmd""
    su -c "$cmd"
    ;;
    esac
    }

    if is_root; then
    echo "Error: need to call this script as a normal user, not as root!"
    exit 1
    fi


    elevate_cmd which adduser





    share|improve this answer





















    • First script helped me, thanks!
      – JRichardsz
      Mar 10 at 17:14



















    1














    "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.



    bash-3.2$ if sudo -S -p '' echo -n < /dev/null 2> /dev/null ; then echo 'Sudo is enabled.' ; else echo 'Sudo is not enabled' ; fi
    Sudo is enabled.
    bash-3.2$ sudo -K
    bash-3.2$ if sudo -S -p '' echo -n < /dev/null 2> /dev/null ; then echo 'Sudo is enabled.' ; else echo 'Sudo is not enabled' ; fi
    Sudo is not enabled


    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.






    share|improve this answer































      -3














      Follow these steps to view the sudoers file. If you're in there, you have sudo. If not, you can add yourself.




      1. su

      2. visudo

      3. Bottom of the file, enter your_username_here ALL=(ALL) ALL

      4. Hit ESC and type :wq

      5. Type exit

      6. Re-run your command that needed sudo

      7. Enter your password (not the root's password)






      share|improve this answer

















      • 10




        The OP "got into trouble" for running sudo, so he probably isn't the system administrator, nor even one of the elite system administrators. He's probably just a user who thought he might have been granted some limited powers. What makes you suspect that he can go su?
        – Scott
        Aug 20 '14 at 20:46











      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "3"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f553932%2fhow-to-check-if-i-have-sudo-access%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      8 Answers
      8






      active

      oldest

      votes








      8 Answers
      8






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      106














      Run sudo -v. It is usually used to extend your sudo password timeout, but can be used for determining whether you have any sudo privileges.



      $ sudo -v
      Sorry, user [username] may not run sudo on [hostname].


      Man page excerpt:




      If given the -v (validate) option, sudo will update the user’s time stamp, prompting for the user’s password if necessary. This extends the sudo timeout for another 5 minutes (or whatever the timeout is set to in sudoers) but does not run a command.




      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 /var/log/secure.



      $ sudo ls
      [sudo] password for [username]:
      Sorry, user [username] is not allowed to execute '/bin/ls' as root on [hostname].


      To find out what you're allowed to run with different privileges, you can use sudo -l. Note that this command requires you to enter your password.






      share|improve this answer



















      • 2




        Thanks. sudo -v works for me. The man page says I can run sudo -l as well but that asks for a password. Why is that?
        – Bruce
        Feb 18 '13 at 20:00






      • 2




        @Bruce I'm guessing here, but otherwise someone (or a program you run) could find out what programs can be executed (possibly without entering password) by your current user and try to use that information maliciously.
        – Daniel Beck
        Feb 18 '13 at 20:05










      • What do you suppose it means when I get this back: patrick@<host>:~$ sudo -v sudo: unable to resolve host <host>? I entered my password and didn't get anything about unauthorized. I know I have sudo from successfully running other commands, but that unable to resolve host message has me concerned something else might be funky on the host.
        – Patrick M
        Apr 21 '14 at 3:04










      • @PatrickM It looks like a problem with the sudoers file. In there you can specify on which host a user is authorized to run a specific command (this is useful when using the same sudoers file on multiple machines). Possibly the hostname specified in that file could not be resolved. Try checking it with the host command for example.
        – Ale
        Dec 17 '14 at 23:10










      • Doesn't work for me on RHEL 6, sudo -v gave "xx is not in the sudoers file. This incident will be reported."
        – 79E09796
        Sep 1 '16 at 17:03
















      106














      Run sudo -v. It is usually used to extend your sudo password timeout, but can be used for determining whether you have any sudo privileges.



      $ sudo -v
      Sorry, user [username] may not run sudo on [hostname].


      Man page excerpt:




      If given the -v (validate) option, sudo will update the user’s time stamp, prompting for the user’s password if necessary. This extends the sudo timeout for another 5 minutes (or whatever the timeout is set to in sudoers) but does not run a command.




      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 /var/log/secure.



      $ sudo ls
      [sudo] password for [username]:
      Sorry, user [username] is not allowed to execute '/bin/ls' as root on [hostname].


      To find out what you're allowed to run with different privileges, you can use sudo -l. Note that this command requires you to enter your password.






      share|improve this answer



















      • 2




        Thanks. sudo -v works for me. The man page says I can run sudo -l as well but that asks for a password. Why is that?
        – Bruce
        Feb 18 '13 at 20:00






      • 2




        @Bruce I'm guessing here, but otherwise someone (or a program you run) could find out what programs can be executed (possibly without entering password) by your current user and try to use that information maliciously.
        – Daniel Beck
        Feb 18 '13 at 20:05










      • What do you suppose it means when I get this back: patrick@<host>:~$ sudo -v sudo: unable to resolve host <host>? I entered my password and didn't get anything about unauthorized. I know I have sudo from successfully running other commands, but that unable to resolve host message has me concerned something else might be funky on the host.
        – Patrick M
        Apr 21 '14 at 3:04










      • @PatrickM It looks like a problem with the sudoers file. In there you can specify on which host a user is authorized to run a specific command (this is useful when using the same sudoers file on multiple machines). Possibly the hostname specified in that file could not be resolved. Try checking it with the host command for example.
        – Ale
        Dec 17 '14 at 23:10










      • Doesn't work for me on RHEL 6, sudo -v gave "xx is not in the sudoers file. This incident will be reported."
        – 79E09796
        Sep 1 '16 at 17:03














      106












      106








      106






      Run sudo -v. It is usually used to extend your sudo password timeout, but can be used for determining whether you have any sudo privileges.



      $ sudo -v
      Sorry, user [username] may not run sudo on [hostname].


      Man page excerpt:




      If given the -v (validate) option, sudo will update the user’s time stamp, prompting for the user’s password if necessary. This extends the sudo timeout for another 5 minutes (or whatever the timeout is set to in sudoers) but does not run a command.




      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 /var/log/secure.



      $ sudo ls
      [sudo] password for [username]:
      Sorry, user [username] is not allowed to execute '/bin/ls' as root on [hostname].


      To find out what you're allowed to run with different privileges, you can use sudo -l. Note that this command requires you to enter your password.






      share|improve this answer














      Run sudo -v. It is usually used to extend your sudo password timeout, but can be used for determining whether you have any sudo privileges.



      $ sudo -v
      Sorry, user [username] may not run sudo on [hostname].


      Man page excerpt:




      If given the -v (validate) option, sudo will update the user’s time stamp, prompting for the user’s password if necessary. This extends the sudo timeout for another 5 minutes (or whatever the timeout is set to in sudoers) but does not run a command.




      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 /var/log/secure.



      $ sudo ls
      [sudo] password for [username]:
      Sorry, user [username] is not allowed to execute '/bin/ls' as root on [hostname].


      To find out what you're allowed to run with different privileges, you can use sudo -l. Note that this command requires you to enter your password.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Feb 18 '13 at 20:07

























      answered Feb 18 '13 at 19:51









      Daniel Beck

      92k12232284




      92k12232284








      • 2




        Thanks. sudo -v works for me. The man page says I can run sudo -l as well but that asks for a password. Why is that?
        – Bruce
        Feb 18 '13 at 20:00






      • 2




        @Bruce I'm guessing here, but otherwise someone (or a program you run) could find out what programs can be executed (possibly without entering password) by your current user and try to use that information maliciously.
        – Daniel Beck
        Feb 18 '13 at 20:05










      • What do you suppose it means when I get this back: patrick@<host>:~$ sudo -v sudo: unable to resolve host <host>? I entered my password and didn't get anything about unauthorized. I know I have sudo from successfully running other commands, but that unable to resolve host message has me concerned something else might be funky on the host.
        – Patrick M
        Apr 21 '14 at 3:04










      • @PatrickM It looks like a problem with the sudoers file. In there you can specify on which host a user is authorized to run a specific command (this is useful when using the same sudoers file on multiple machines). Possibly the hostname specified in that file could not be resolved. Try checking it with the host command for example.
        – Ale
        Dec 17 '14 at 23:10










      • Doesn't work for me on RHEL 6, sudo -v gave "xx is not in the sudoers file. This incident will be reported."
        – 79E09796
        Sep 1 '16 at 17:03














      • 2




        Thanks. sudo -v works for me. The man page says I can run sudo -l as well but that asks for a password. Why is that?
        – Bruce
        Feb 18 '13 at 20:00






      • 2




        @Bruce I'm guessing here, but otherwise someone (or a program you run) could find out what programs can be executed (possibly without entering password) by your current user and try to use that information maliciously.
        – Daniel Beck
        Feb 18 '13 at 20:05










      • What do you suppose it means when I get this back: patrick@<host>:~$ sudo -v sudo: unable to resolve host <host>? I entered my password and didn't get anything about unauthorized. I know I have sudo from successfully running other commands, but that unable to resolve host message has me concerned something else might be funky on the host.
        – Patrick M
        Apr 21 '14 at 3:04










      • @PatrickM It looks like a problem with the sudoers file. In there you can specify on which host a user is authorized to run a specific command (this is useful when using the same sudoers file on multiple machines). Possibly the hostname specified in that file could not be resolved. Try checking it with the host command for example.
        – Ale
        Dec 17 '14 at 23:10










      • Doesn't work for me on RHEL 6, sudo -v gave "xx is not in the sudoers file. This incident will be reported."
        – 79E09796
        Sep 1 '16 at 17:03








      2




      2




      Thanks. sudo -v works for me. The man page says I can run sudo -l as well but that asks for a password. Why is that?
      – Bruce
      Feb 18 '13 at 20:00




      Thanks. sudo -v works for me. The man page says I can run sudo -l as well but that asks for a password. Why is that?
      – Bruce
      Feb 18 '13 at 20:00




      2




      2




      @Bruce I'm guessing here, but otherwise someone (or a program you run) could find out what programs can be executed (possibly without entering password) by your current user and try to use that information maliciously.
      – Daniel Beck
      Feb 18 '13 at 20:05




      @Bruce I'm guessing here, but otherwise someone (or a program you run) could find out what programs can be executed (possibly without entering password) by your current user and try to use that information maliciously.
      – Daniel Beck
      Feb 18 '13 at 20:05












      What do you suppose it means when I get this back: patrick@<host>:~$ sudo -v sudo: unable to resolve host <host>? I entered my password and didn't get anything about unauthorized. I know I have sudo from successfully running other commands, but that unable to resolve host message has me concerned something else might be funky on the host.
      – Patrick M
      Apr 21 '14 at 3:04




      What do you suppose it means when I get this back: patrick@<host>:~$ sudo -v sudo: unable to resolve host <host>? I entered my password and didn't get anything about unauthorized. I know I have sudo from successfully running other commands, but that unable to resolve host message has me concerned something else might be funky on the host.
      – Patrick M
      Apr 21 '14 at 3:04












      @PatrickM It looks like a problem with the sudoers file. In there you can specify on which host a user is authorized to run a specific command (this is useful when using the same sudoers file on multiple machines). Possibly the hostname specified in that file could not be resolved. Try checking it with the host command for example.
      – Ale
      Dec 17 '14 at 23:10




      @PatrickM It looks like a problem with the sudoers file. In there you can specify on which host a user is authorized to run a specific command (this is useful when using the same sudoers file on multiple machines). Possibly the hostname specified in that file could not be resolved. Try checking it with the host command for example.
      – Ale
      Dec 17 '14 at 23:10












      Doesn't work for me on RHEL 6, sudo -v gave "xx is not in the sudoers file. This incident will be reported."
      – 79E09796
      Sep 1 '16 at 17:03




      Doesn't work for me on RHEL 6, sudo -v gave "xx is not in the sudoers file. This incident will be reported."
      – 79E09796
      Sep 1 '16 at 17:03













      39














      This is very simple. Run sudo -l. This will list any sudo privileges you have.






      share|improve this answer



















      • 1




        Maybe downvoted because it repeats what Daniel Beck said nearly two years ago.
        – G-Man
        Dec 18 '14 at 4:09






      • 1




        Or explains what happen, it's a comment, at best
        – Ramhound
        Dec 18 '14 at 20:47






      • 2




        @Jonathan: if u would script in ubuntu rigt now, sudo -l asks for a password if u can sudo or not. sudo -v asks only if u can, and "$(whoami)" != "root" will never ask anything in any linux.
        – bksunday
        Aug 3 '15 at 3:37










      • @bksunday You are correct. I tested now on a clean Debian Jessy and confirmed your results. My previous (deleted now) comment was probably a result of testing on a machine on which I had some sudo privs.
        – Jonathan Ben-Avraham
        Aug 3 '15 at 4:30










      • @G-Man but this simple answer helped me more than probably more precise Daniel's answer, where this command is the the very end unfortunatelly...
        – Betlista
        Jan 4 '16 at 14:48
















      39














      This is very simple. Run sudo -l. This will list any sudo privileges you have.






      share|improve this answer



















      • 1




        Maybe downvoted because it repeats what Daniel Beck said nearly two years ago.
        – G-Man
        Dec 18 '14 at 4:09






      • 1




        Or explains what happen, it's a comment, at best
        – Ramhound
        Dec 18 '14 at 20:47






      • 2




        @Jonathan: if u would script in ubuntu rigt now, sudo -l asks for a password if u can sudo or not. sudo -v asks only if u can, and "$(whoami)" != "root" will never ask anything in any linux.
        – bksunday
        Aug 3 '15 at 3:37










      • @bksunday You are correct. I tested now on a clean Debian Jessy and confirmed your results. My previous (deleted now) comment was probably a result of testing on a machine on which I had some sudo privs.
        – Jonathan Ben-Avraham
        Aug 3 '15 at 4:30










      • @G-Man but this simple answer helped me more than probably more precise Daniel's answer, where this command is the the very end unfortunatelly...
        – Betlista
        Jan 4 '16 at 14:48














      39












      39








      39






      This is very simple. Run sudo -l. This will list any sudo privileges you have.






      share|improve this answer














      This is very simple. Run sudo -l. This will list any sudo privileges you have.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Dec 18 '14 at 0:58









      MBraedley

      2,43732242




      2,43732242










      answered Dec 17 '14 at 22:55









      Brad Dausses

      50743




      50743








      • 1




        Maybe downvoted because it repeats what Daniel Beck said nearly two years ago.
        – G-Man
        Dec 18 '14 at 4:09






      • 1




        Or explains what happen, it's a comment, at best
        – Ramhound
        Dec 18 '14 at 20:47






      • 2




        @Jonathan: if u would script in ubuntu rigt now, sudo -l asks for a password if u can sudo or not. sudo -v asks only if u can, and "$(whoami)" != "root" will never ask anything in any linux.
        – bksunday
        Aug 3 '15 at 3:37










      • @bksunday You are correct. I tested now on a clean Debian Jessy and confirmed your results. My previous (deleted now) comment was probably a result of testing on a machine on which I had some sudo privs.
        – Jonathan Ben-Avraham
        Aug 3 '15 at 4:30










      • @G-Man but this simple answer helped me more than probably more precise Daniel's answer, where this command is the the very end unfortunatelly...
        – Betlista
        Jan 4 '16 at 14:48














      • 1




        Maybe downvoted because it repeats what Daniel Beck said nearly two years ago.
        – G-Man
        Dec 18 '14 at 4:09






      • 1




        Or explains what happen, it's a comment, at best
        – Ramhound
        Dec 18 '14 at 20:47






      • 2




        @Jonathan: if u would script in ubuntu rigt now, sudo -l asks for a password if u can sudo or not. sudo -v asks only if u can, and "$(whoami)" != "root" will never ask anything in any linux.
        – bksunday
        Aug 3 '15 at 3:37










      • @bksunday You are correct. I tested now on a clean Debian Jessy and confirmed your results. My previous (deleted now) comment was probably a result of testing on a machine on which I had some sudo privs.
        – Jonathan Ben-Avraham
        Aug 3 '15 at 4:30










      • @G-Man but this simple answer helped me more than probably more precise Daniel's answer, where this command is the the very end unfortunatelly...
        – Betlista
        Jan 4 '16 at 14:48








      1




      1




      Maybe downvoted because it repeats what Daniel Beck said nearly two years ago.
      – G-Man
      Dec 18 '14 at 4:09




      Maybe downvoted because it repeats what Daniel Beck said nearly two years ago.
      – G-Man
      Dec 18 '14 at 4:09




      1




      1




      Or explains what happen, it's a comment, at best
      – Ramhound
      Dec 18 '14 at 20:47




      Or explains what happen, it's a comment, at best
      – Ramhound
      Dec 18 '14 at 20:47




      2




      2




      @Jonathan: if u would script in ubuntu rigt now, sudo -l asks for a password if u can sudo or not. sudo -v asks only if u can, and "$(whoami)" != "root" will never ask anything in any linux.
      – bksunday
      Aug 3 '15 at 3:37




      @Jonathan: if u would script in ubuntu rigt now, sudo -l asks for a password if u can sudo or not. sudo -v asks only if u can, and "$(whoami)" != "root" will never ask anything in any linux.
      – bksunday
      Aug 3 '15 at 3:37












      @bksunday You are correct. I tested now on a clean Debian Jessy and confirmed your results. My previous (deleted now) comment was probably a result of testing on a machine on which I had some sudo privs.
      – Jonathan Ben-Avraham
      Aug 3 '15 at 4:30




      @bksunday You are correct. I tested now on a clean Debian Jessy and confirmed your results. My previous (deleted now) comment was probably a result of testing on a machine on which I had some sudo privs.
      – Jonathan Ben-Avraham
      Aug 3 '15 at 4:30












      @G-Man but this simple answer helped me more than probably more precise Daniel's answer, where this command is the the very end unfortunatelly...
      – Betlista
      Jan 4 '16 at 14:48




      @G-Man but this simple answer helped me more than probably more precise Daniel's answer, where this command is the the very end unfortunatelly...
      – Betlista
      Jan 4 '16 at 14:48











      8














      Here is the script-friendly version:



      timeout 2 sudo id && echo Access granted || echo Access denied


      since it won't stuck on the password input if you do not have the sudo access.



      You can also set it in a variable like:



      timeout 2 sudo id && sudo="true" || sudo="false"
      echo "$sudo"


      Note: On macOS, you need to install coreutils, e.g. brew install coreutils.






      share|improve this answer























      • Any alternatives for where timeout isn't available by default, e.g. on OS X?
        – Harry
        Jun 1 at 16:27






      • 1




        You need to install coreutils, e.g. brew install coreutils.
        – kenorb
        Jun 13 at 15:08










      • This does not work for me in a script. For unexplained reason the script hangs until I kill it.
        – beruic
        Oct 11 at 11:04


















      8














      Here is the script-friendly version:



      timeout 2 sudo id && echo Access granted || echo Access denied


      since it won't stuck on the password input if you do not have the sudo access.



      You can also set it in a variable like:



      timeout 2 sudo id && sudo="true" || sudo="false"
      echo "$sudo"


      Note: On macOS, you need to install coreutils, e.g. brew install coreutils.






      share|improve this answer























      • Any alternatives for where timeout isn't available by default, e.g. on OS X?
        – Harry
        Jun 1 at 16:27






      • 1




        You need to install coreutils, e.g. brew install coreutils.
        – kenorb
        Jun 13 at 15:08










      • This does not work for me in a script. For unexplained reason the script hangs until I kill it.
        – beruic
        Oct 11 at 11:04
















      8












      8








      8






      Here is the script-friendly version:



      timeout 2 sudo id && echo Access granted || echo Access denied


      since it won't stuck on the password input if you do not have the sudo access.



      You can also set it in a variable like:



      timeout 2 sudo id && sudo="true" || sudo="false"
      echo "$sudo"


      Note: On macOS, you need to install coreutils, e.g. brew install coreutils.






      share|improve this answer














      Here is the script-friendly version:



      timeout 2 sudo id && echo Access granted || echo Access denied


      since it won't stuck on the password input if you do not have the sudo access.



      You can also set it in a variable like:



      timeout 2 sudo id && sudo="true" || sudo="false"
      echo "$sudo"


      Note: On macOS, you need to install coreutils, e.g. brew install coreutils.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Dec 5 at 22:41

























      answered Jun 14 '16 at 0:49









      kenorb

      10.7k1577111




      10.7k1577111












      • Any alternatives for where timeout isn't available by default, e.g. on OS X?
        – Harry
        Jun 1 at 16:27






      • 1




        You need to install coreutils, e.g. brew install coreutils.
        – kenorb
        Jun 13 at 15:08










      • This does not work for me in a script. For unexplained reason the script hangs until I kill it.
        – beruic
        Oct 11 at 11:04




















      • Any alternatives for where timeout isn't available by default, e.g. on OS X?
        – Harry
        Jun 1 at 16:27






      • 1




        You need to install coreutils, e.g. brew install coreutils.
        – kenorb
        Jun 13 at 15:08










      • This does not work for me in a script. For unexplained reason the script hangs until I kill it.
        – beruic
        Oct 11 at 11:04


















      Any alternatives for where timeout isn't available by default, e.g. on OS X?
      – Harry
      Jun 1 at 16:27




      Any alternatives for where timeout isn't available by default, e.g. on OS X?
      – Harry
      Jun 1 at 16:27




      1




      1




      You need to install coreutils, e.g. brew install coreutils.
      – kenorb
      Jun 13 at 15:08




      You need to install coreutils, e.g. brew install coreutils.
      – kenorb
      Jun 13 at 15:08












      This does not work for me in a script. For unexplained reason the script hangs until I kill it.
      – beruic
      Oct 11 at 11:04






      This does not work for me in a script. For unexplained reason the script hangs until I kill it.
      – beruic
      Oct 11 at 11:04













      1














      For me, 'sudo -v' and 'sudo -l' did not work in a script because sometimes interactive (asking me for a password, like mentioned above).
      'sudo -n -l' did also not work, it gave the exit code '1' although I have sudo permissions, because of the missing password.
      But extending the command to:



      A=$(sudo -n -v 2>&1);test -z "$A" || echo $A|grep -q asswor


      was successful for me for the script.
      This expression gives 0 if the current user can call 'sudo' and 1 if not.



      Explanation:

      The additional parameter -n to sudo prevents interactivity.

      The output $A of the command 'sudo -n -v 2>&1' may be:

      - empty (in this case, sudo can be called by the current user), or:

      - a note that the current user is not authorized for sudo, or:

      - a question text for the password (in this case, the user is authorized).

      ("asswor" will fit for an english "password" as well as for a German "Passwort").






      share|improve this answer




























        1














        For me, 'sudo -v' and 'sudo -l' did not work in a script because sometimes interactive (asking me for a password, like mentioned above).
        'sudo -n -l' did also not work, it gave the exit code '1' although I have sudo permissions, because of the missing password.
        But extending the command to:



        A=$(sudo -n -v 2>&1);test -z "$A" || echo $A|grep -q asswor


        was successful for me for the script.
        This expression gives 0 if the current user can call 'sudo' and 1 if not.



        Explanation:

        The additional parameter -n to sudo prevents interactivity.

        The output $A of the command 'sudo -n -v 2>&1' may be:

        - empty (in this case, sudo can be called by the current user), or:

        - a note that the current user is not authorized for sudo, or:

        - a question text for the password (in this case, the user is authorized).

        ("asswor" will fit for an english "password" as well as for a German "Passwort").






        share|improve this answer


























          1












          1








          1






          For me, 'sudo -v' and 'sudo -l' did not work in a script because sometimes interactive (asking me for a password, like mentioned above).
          'sudo -n -l' did also not work, it gave the exit code '1' although I have sudo permissions, because of the missing password.
          But extending the command to:



          A=$(sudo -n -v 2>&1);test -z "$A" || echo $A|grep -q asswor


          was successful for me for the script.
          This expression gives 0 if the current user can call 'sudo' and 1 if not.



          Explanation:

          The additional parameter -n to sudo prevents interactivity.

          The output $A of the command 'sudo -n -v 2>&1' may be:

          - empty (in this case, sudo can be called by the current user), or:

          - a note that the current user is not authorized for sudo, or:

          - a question text for the password (in this case, the user is authorized).

          ("asswor" will fit for an english "password" as well as for a German "Passwort").






          share|improve this answer














          For me, 'sudo -v' and 'sudo -l' did not work in a script because sometimes interactive (asking me for a password, like mentioned above).
          'sudo -n -l' did also not work, it gave the exit code '1' although I have sudo permissions, because of the missing password.
          But extending the command to:



          A=$(sudo -n -v 2>&1);test -z "$A" || echo $A|grep -q asswor


          was successful for me for the script.
          This expression gives 0 if the current user can call 'sudo' and 1 if not.



          Explanation:

          The additional parameter -n to sudo prevents interactivity.

          The output $A of the command 'sudo -n -v 2>&1' may be:

          - empty (in this case, sudo can be called by the current user), or:

          - a note that the current user is not authorized for sudo, or:

          - a question text for the password (in this case, the user is authorized).

          ("asswor" will fit for an english "password" as well as for a German "Passwort").







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 11 '17 at 23:28

























          answered Feb 28 '17 at 2:27









          Gerald Schade

          1113




          1113























              1














              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:



              [[ "$(whereis sudo)" == *'/'* && "$(sudo -nv 2>&1)" != 'Sorry, user'* ]]


              (from the end of 2015 mwhahaaa)






              share|improve this answer

















              • 1




                Read over "Why do I need 50 reputation to comment" to ensure you understand how you can start commenting.
                – Pimp Juice IT
                Oct 1 '17 at 23:24
















              1














              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:



              [[ "$(whereis sudo)" == *'/'* && "$(sudo -nv 2>&1)" != 'Sorry, user'* ]]


              (from the end of 2015 mwhahaaa)






              share|improve this answer

















              • 1




                Read over "Why do I need 50 reputation to comment" to ensure you understand how you can start commenting.
                – Pimp Juice IT
                Oct 1 '17 at 23:24














              1












              1








              1






              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:



              [[ "$(whereis sudo)" == *'/'* && "$(sudo -nv 2>&1)" != 'Sorry, user'* ]]


              (from the end of 2015 mwhahaaa)






              share|improve this answer












              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:



              [[ "$(whereis sudo)" == *'/'* && "$(sudo -nv 2>&1)" != 'Sorry, user'* ]]


              (from the end of 2015 mwhahaaa)







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Oct 1 '17 at 22:31









              user629901

              111




              111








              • 1




                Read over "Why do I need 50 reputation to comment" to ensure you understand how you can start commenting.
                – Pimp Juice IT
                Oct 1 '17 at 23:24














              • 1




                Read over "Why do I need 50 reputation to comment" to ensure you understand how you can start commenting.
                – Pimp Juice IT
                Oct 1 '17 at 23:24








              1




              1




              Read over "Why do I need 50 reputation to comment" to ensure you understand how you can start commenting.
              – Pimp Juice IT
              Oct 1 '17 at 23:24




              Read over "Why do I need 50 reputation to comment" to ensure you understand how you can start commenting.
              – Pimp Juice IT
              Oct 1 '17 at 23:24











              1














              Gerald Schade's answer here, can still be improved!



              Use



              prompt=$(sudo -nv 2>&1)
              if [ $? -eq 0 ]; then
              # exit code of sudo-command is 0
              echo "has_sudo__pass_set"
              elif echo $prompt | grep -q '^sudo:'; then
              echo "has_sudo__needs_pass"
              else
              echo "no_sudo"
              fi


              Here's a complete example of usage in a script:



              #!/usr/bin/env bash

              is_root () {
              return $(id -u)
              }

              has_sudo() {
              local prompt

              prompt=$(sudo -nv 2>&1)
              if [ $? -eq 0 ]; then
              echo "has_sudo__pass_set"
              elif echo $prompt | grep -q '^sudo:'; then
              echo "has_sudo__needs_pass"
              else
              echo "no_sudo"
              fi
              }

              elevate_cmd () {
              local cmd=$@

              HAS_SUDO=$(has_sudo)

              case "$HAS_SUDO" in
              has_sudo__pass_set)
              sudo $cmd
              ;;
              has_sudo__needs_pass)
              echo "Please supply sudo password for the following command: sudo $cmd"
              sudo $cmd
              ;;
              *)
              echo "Please supply root password for the following command: su -c "$cmd""
              su -c "$cmd"
              ;;
              esac
              }

              if is_root; then
              echo "Error: need to call this script as a normal user, not as root!"
              exit 1
              fi


              elevate_cmd which adduser





              share|improve this answer





















              • First script helped me, thanks!
                – JRichardsz
                Mar 10 at 17:14
















              1














              Gerald Schade's answer here, can still be improved!



              Use



              prompt=$(sudo -nv 2>&1)
              if [ $? -eq 0 ]; then
              # exit code of sudo-command is 0
              echo "has_sudo__pass_set"
              elif echo $prompt | grep -q '^sudo:'; then
              echo "has_sudo__needs_pass"
              else
              echo "no_sudo"
              fi


              Here's a complete example of usage in a script:



              #!/usr/bin/env bash

              is_root () {
              return $(id -u)
              }

              has_sudo() {
              local prompt

              prompt=$(sudo -nv 2>&1)
              if [ $? -eq 0 ]; then
              echo "has_sudo__pass_set"
              elif echo $prompt | grep -q '^sudo:'; then
              echo "has_sudo__needs_pass"
              else
              echo "no_sudo"
              fi
              }

              elevate_cmd () {
              local cmd=$@

              HAS_SUDO=$(has_sudo)

              case "$HAS_SUDO" in
              has_sudo__pass_set)
              sudo $cmd
              ;;
              has_sudo__needs_pass)
              echo "Please supply sudo password for the following command: sudo $cmd"
              sudo $cmd
              ;;
              *)
              echo "Please supply root password for the following command: su -c "$cmd""
              su -c "$cmd"
              ;;
              esac
              }

              if is_root; then
              echo "Error: need to call this script as a normal user, not as root!"
              exit 1
              fi


              elevate_cmd which adduser





              share|improve this answer





















              • First script helped me, thanks!
                – JRichardsz
                Mar 10 at 17:14














              1












              1








              1






              Gerald Schade's answer here, can still be improved!



              Use



              prompt=$(sudo -nv 2>&1)
              if [ $? -eq 0 ]; then
              # exit code of sudo-command is 0
              echo "has_sudo__pass_set"
              elif echo $prompt | grep -q '^sudo:'; then
              echo "has_sudo__needs_pass"
              else
              echo "no_sudo"
              fi


              Here's a complete example of usage in a script:



              #!/usr/bin/env bash

              is_root () {
              return $(id -u)
              }

              has_sudo() {
              local prompt

              prompt=$(sudo -nv 2>&1)
              if [ $? -eq 0 ]; then
              echo "has_sudo__pass_set"
              elif echo $prompt | grep -q '^sudo:'; then
              echo "has_sudo__needs_pass"
              else
              echo "no_sudo"
              fi
              }

              elevate_cmd () {
              local cmd=$@

              HAS_SUDO=$(has_sudo)

              case "$HAS_SUDO" in
              has_sudo__pass_set)
              sudo $cmd
              ;;
              has_sudo__needs_pass)
              echo "Please supply sudo password for the following command: sudo $cmd"
              sudo $cmd
              ;;
              *)
              echo "Please supply root password for the following command: su -c "$cmd""
              su -c "$cmd"
              ;;
              esac
              }

              if is_root; then
              echo "Error: need to call this script as a normal user, not as root!"
              exit 1
              fi


              elevate_cmd which adduser





              share|improve this answer












              Gerald Schade's answer here, can still be improved!



              Use



              prompt=$(sudo -nv 2>&1)
              if [ $? -eq 0 ]; then
              # exit code of sudo-command is 0
              echo "has_sudo__pass_set"
              elif echo $prompt | grep -q '^sudo:'; then
              echo "has_sudo__needs_pass"
              else
              echo "no_sudo"
              fi


              Here's a complete example of usage in a script:



              #!/usr/bin/env bash

              is_root () {
              return $(id -u)
              }

              has_sudo() {
              local prompt

              prompt=$(sudo -nv 2>&1)
              if [ $? -eq 0 ]; then
              echo "has_sudo__pass_set"
              elif echo $prompt | grep -q '^sudo:'; then
              echo "has_sudo__needs_pass"
              else
              echo "no_sudo"
              fi
              }

              elevate_cmd () {
              local cmd=$@

              HAS_SUDO=$(has_sudo)

              case "$HAS_SUDO" in
              has_sudo__pass_set)
              sudo $cmd
              ;;
              has_sudo__needs_pass)
              echo "Please supply sudo password for the following command: sudo $cmd"
              sudo $cmd
              ;;
              *)
              echo "Please supply root password for the following command: su -c "$cmd""
              su -c "$cmd"
              ;;
              esac
              }

              if is_root; then
              echo "Error: need to call this script as a normal user, not as root!"
              exit 1
              fi


              elevate_cmd which adduser






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 30 '17 at 18:57









              ajneu

              111




              111












              • First script helped me, thanks!
                – JRichardsz
                Mar 10 at 17:14


















              • First script helped me, thanks!
                – JRichardsz
                Mar 10 at 17:14
















              First script helped me, thanks!
              – JRichardsz
              Mar 10 at 17:14




              First script helped me, thanks!
              – JRichardsz
              Mar 10 at 17:14











              1














              "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.



              bash-3.2$ if sudo -S -p '' echo -n < /dev/null 2> /dev/null ; then echo 'Sudo is enabled.' ; else echo 'Sudo is not enabled' ; fi
              Sudo is enabled.
              bash-3.2$ sudo -K
              bash-3.2$ if sudo -S -p '' echo -n < /dev/null 2> /dev/null ; then echo 'Sudo is enabled.' ; else echo 'Sudo is not enabled' ; fi
              Sudo is not enabled


              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.






              share|improve this answer




























                1














                "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.



                bash-3.2$ if sudo -S -p '' echo -n < /dev/null 2> /dev/null ; then echo 'Sudo is enabled.' ; else echo 'Sudo is not enabled' ; fi
                Sudo is enabled.
                bash-3.2$ sudo -K
                bash-3.2$ if sudo -S -p '' echo -n < /dev/null 2> /dev/null ; then echo 'Sudo is enabled.' ; else echo 'Sudo is not enabled' ; fi
                Sudo is not enabled


                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.






                share|improve this answer


























                  1












                  1








                  1






                  "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.



                  bash-3.2$ if sudo -S -p '' echo -n < /dev/null 2> /dev/null ; then echo 'Sudo is enabled.' ; else echo 'Sudo is not enabled' ; fi
                  Sudo is enabled.
                  bash-3.2$ sudo -K
                  bash-3.2$ if sudo -S -p '' echo -n < /dev/null 2> /dev/null ; then echo 'Sudo is enabled.' ; else echo 'Sudo is not enabled' ; fi
                  Sudo is not enabled


                  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.






                  share|improve this answer














                  "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.



                  bash-3.2$ if sudo -S -p '' echo -n < /dev/null 2> /dev/null ; then echo 'Sudo is enabled.' ; else echo 'Sudo is not enabled' ; fi
                  Sudo is enabled.
                  bash-3.2$ sudo -K
                  bash-3.2$ if sudo -S -p '' echo -n < /dev/null 2> /dev/null ; then echo 'Sudo is enabled.' ; else echo 'Sudo is not enabled' ; fi
                  Sudo is not enabled


                  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.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 7 at 14:42









                  Unamata Sanatarai

                  1034




                  1034










                  answered Aug 30 '16 at 17:58









                  Ben Hyde

                  1111




                  1111























                      -3














                      Follow these steps to view the sudoers file. If you're in there, you have sudo. If not, you can add yourself.




                      1. su

                      2. visudo

                      3. Bottom of the file, enter your_username_here ALL=(ALL) ALL

                      4. Hit ESC and type :wq

                      5. Type exit

                      6. Re-run your command that needed sudo

                      7. Enter your password (not the root's password)






                      share|improve this answer

















                      • 10




                        The OP "got into trouble" for running sudo, so he probably isn't the system administrator, nor even one of the elite system administrators. He's probably just a user who thought he might have been granted some limited powers. What makes you suspect that he can go su?
                        – Scott
                        Aug 20 '14 at 20:46
















                      -3














                      Follow these steps to view the sudoers file. If you're in there, you have sudo. If not, you can add yourself.




                      1. su

                      2. visudo

                      3. Bottom of the file, enter your_username_here ALL=(ALL) ALL

                      4. Hit ESC and type :wq

                      5. Type exit

                      6. Re-run your command that needed sudo

                      7. Enter your password (not the root's password)






                      share|improve this answer

















                      • 10




                        The OP "got into trouble" for running sudo, so he probably isn't the system administrator, nor even one of the elite system administrators. He's probably just a user who thought he might have been granted some limited powers. What makes you suspect that he can go su?
                        – Scott
                        Aug 20 '14 at 20:46














                      -3












                      -3








                      -3






                      Follow these steps to view the sudoers file. If you're in there, you have sudo. If not, you can add yourself.




                      1. su

                      2. visudo

                      3. Bottom of the file, enter your_username_here ALL=(ALL) ALL

                      4. Hit ESC and type :wq

                      5. Type exit

                      6. Re-run your command that needed sudo

                      7. Enter your password (not the root's password)






                      share|improve this answer












                      Follow these steps to view the sudoers file. If you're in there, you have sudo. If not, you can add yourself.




                      1. su

                      2. visudo

                      3. Bottom of the file, enter your_username_here ALL=(ALL) ALL

                      4. Hit ESC and type :wq

                      5. Type exit

                      6. Re-run your command that needed sudo

                      7. Enter your password (not the root's password)







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Feb 18 '13 at 19:41









                      Kruug

                      4,94221728




                      4,94221728








                      • 10




                        The OP "got into trouble" for running sudo, so he probably isn't the system administrator, nor even one of the elite system administrators. He's probably just a user who thought he might have been granted some limited powers. What makes you suspect that he can go su?
                        – Scott
                        Aug 20 '14 at 20:46














                      • 10




                        The OP "got into trouble" for running sudo, so he probably isn't the system administrator, nor even one of the elite system administrators. He's probably just a user who thought he might have been granted some limited powers. What makes you suspect that he can go su?
                        – Scott
                        Aug 20 '14 at 20:46








                      10




                      10




                      The OP "got into trouble" for running sudo, so he probably isn't the system administrator, nor even one of the elite system administrators. He's probably just a user who thought he might have been granted some limited powers. What makes you suspect that he can go su?
                      – Scott
                      Aug 20 '14 at 20:46




                      The OP "got into trouble" for running sudo, so he probably isn't the system administrator, nor even one of the elite system administrators. He's probably just a user who thought he might have been granted some limited powers. What makes you suspect that he can go su?
                      – Scott
                      Aug 20 '14 at 20:46


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Super User!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f553932%2fhow-to-check-if-i-have-sudo-access%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Plaza Victoria

                      Puebla de Zaragoza

                      Musa