50

How does Linux system behave when I am not sudoer? Here is what happens if I try to use sudo:

server:/tmp>$ sudo cal
[sudo] password for user:
Sorry, try again.

Is it possible that I just don't know my password or does this mean that I am not sudoer? (On another machine system printed out that I'm no sudoer and the incident will be reported)

1

3 Answers 3

57

To know whether a particular user is having sudo access or not, we can use -l and -U options together.

For example,

If the user has sudo access, it will print the level of sudo access for that particular user.

   $ sudo -l -U pradeep
     User pradeep may run the following commands on this host:
     (ALL : ALL) ALL

If the user don't have sudo access, it will print that user is not allowed to run sudo on localhost.

   $ sudo -l -U pradeep.c
     User pradeep.c is not allowed to run sudo on localhost.
2
  • 9
    This does not work for me. Instead of giving me information it asks me for password again: server:/home/drasto>$ sudo -l -U drasto [sudo] password for drasto: There is Red Hat distro on that server if that helps
    – Rasto
    Oct 13, 2012 at 19:25
  • 1
    From man sudo on Ubuntu 18.04: "The security policy may restrict listing other users' privileges. The sudoers policy only allows root or a user with the ALL privilege on the current host to use this option."
    – mklement0
    Apr 19, 2021 at 14:59
13

You can use the -l flag to list your privileges.

-l[l] [command]
   If no command is specified, the -l (list) option will list the allowed (and forbidden)
   commands for the invoking user (or the user specified by the -U option) on the current
   host.  If a command is specified and is permitted by sudoers, the fully-qualified path
   to the command is displayed along with any command line arguments.  If command is
   specified but not allowed, sudo will exit with a status value of 1.  If the -l option
   is specified with an l argument (i.e. -ll), or if -l is specified multiple times, a
   longer list format is used.

If you're not in the file, you should get the "not in the sudoers file" error you saw on the other machine.

9
  • 4
    This also does not work for me. It just asks me for password: server:/home/drasto>$ sudo -l [sudo] password for drasto:
    – Rasto
    Oct 13, 2012 at 19:31
  • 2
    Put in your password
    – Kevin
    Oct 13, 2012 at 19:31
  • 1
    @Kevin @Michael My password does not work. I only know about one password for that server and that is the one I use to log in to the server. That one simply does not work when I'm asked for my sudo password (I have tried it about 100 times, capslock, numlock, etc). But the whole think does not make sense! I want to know if I have sudo privileges but to find it out I need sudo privileges?! To run sudo -l I need sudo password? So I have to be root to know if I'm root?!
    – Rasto
    Oct 13, 2012 at 23:43
  • 1
    @drasto As bill said, sudo is looking for your login password. So if you've tried that and it doesn't work, either sudo is horribly misconfigured or you're in a jail of some sort where it can't see or read the proper files. Either way, you have in effect no sudo permissions.
    – Kevin
    Oct 14, 2012 at 0:15
  • 1
    @Kevin Then I'm probably in that jail of some kind. Actually I think I should not have those sudo permissions on that machine. So I was surprised when it asked me for my sudo password at the first place. Anyway it is just as I wrote: my login password does not work.
    – Rasto
    Oct 14, 2012 at 6:55
3

You can check if you are in the sudo group, by using the command

groups

In a shell-script you may want to use this:

if groups | grep "\<sudo\>" &> /dev/null; then
   echo yes
else
   echo no
fi
2
  • 3
    The group could be sudo, admin, wheel, or something else altogether, and even with membership in a group, an individual user maybe denied sudo access via specific rules (or the other way around).
    – muru
    Mar 6, 2017 at 8:51
  • This has too many false negatives for my purpose (hardening verification). Kevin's answer is more definitive.
    – StockB
    Jan 9, 2018 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.