Admin password not working for sudo?

Solution 1:

For whatever reason you seem to have the targetpw option set. From man sudoers:

targetpw   If set, sudo will prompt for the password of the user specified by the -u
           option (defaults to root) instead of the password of the invoking user when
           running a command or editing a file.  Note that this flag precludes the use
           of a uid not listed in the passwd database as an argument to the -u option.
           This flag is off by default.

So you can do things like sudo -u diogopires ls by entering your own password, but you can't run sudo -u diogopires visudo because this would run visudo as diogopires (and not as root as it should).

So, to fix this

  • Boot into single user mode by pressing Cmd-S on startup
  • Run mount -uw /
  • Use visudo (without sudo) to remove the option
  • Type Ctrl-D or run reboot to reboot

Solution 2:

Idea #1 - Using your password

The entire purpose of sudo is to grant users or groups of users access to elevated functions without having to reveal the Administrator's credentials. Therefore the password you'll want to use when prompted by the sudo command is in fact your user's password.

Additionally you can use the switch -l to sudo to see what specific permissions your user ID has been granted.

For example:

$ sudo -l
Password:
Matching Defaults entries for joeuser on unagi:
    env_reset, env_keep+=BLOCKSIZE, env_keep+="COLORFGBG COLORTERM", env_keep+=__CF_USER_TEXT_ENCODING, env_keep+="CHARSET LANG LANGUAGE LC_ALL LC_COLLATE LC_CTYPE", env_keep+="LC_MESSAGES LC_MONETARY
    LC_NUMERIC LC_TIME", env_keep+="LINES COLUMNS", env_keep+=LSCOLORS, env_keep+=SSH_AUTH_SOCK, env_keep+=TZ, env_keep+="DISPLAY XAUTHORIZATION XAUTHORITY", env_keep+="EDITOR VISUAL", env_keep+="HOME
    MAIL", lecture_file=/etc/sudo_lecture

User joeuser may run the following commands on unagi:
    (ALL) ALL

The above is stating that the user joeuser has all permissions, (ALL) ALL, on the system.

Idea #2 - Lacking sudo permissions

If you're finding that your password is correct, then it's likely that your account simply doesn't have an entry in Sudo's configuration files giving the user any permissions to use. To confirm this you can try running the command visudo to view Sudo's configuration file:

$ visudo
...
##
# Cmnd alias specification
##
# Cmnd_Alias    PAGERS = /usr/bin/more, /usr/bin/pg, /usr/bin/less

##
# User specification
##

# root and users in group wheel can run anything on any machine as any user
root        ALL = (ALL) ALL
%admin      ALL = (ALL) ALL

## Read drop-in files from /private/etc/sudoers.d
## (the '#' here does not indicate a comment)
#includedir /private/etc/sudoers.d

NOTE: visudo is merely opening the file /etc/sudoers in vi in a protected manner so that no 2 users can stomp on it at the same time.

Idea #3 - Group membership

Typically with user's being granted access to sudo they'll be added to a special UNIX group called admin. You can confirm if your account is a member of this group like so:

$ id -a | grep -o '[0-9]\+(admin)'
80(admin)

This group is special since it's typically what shows up in the system's /etc/sudoers file granting users admin access via sudo via this line:

$ cat /etc/sudoers
...
...
%admin      ALL = (ALL) ALL
...
...

Idea #4 - TextExpander

I was able to find threads where other people were reporting that TextExpander was auto capitalizing the first word in a sentence, which was causing the first letter of their password to be capitalized when they would type it. You can disable TextExpander to see if this is the cause of your password issues with sudo.

  • Password not working with Sudo
  • Admin Password Stopped Working In The Terminal
  • TextExpander - Help: Expanding Snippets

Idea #5 - Corrupt account or password

There's a slight chance that your user's password may have become corrupt in some capacity which is rendering sudo unable to use it. To help eliminate this as a possible cause you can try resetting/changing your user's password. To do so access the Systems Preferences and then open up Users & Groups and select your user and click the Change Password button.

Additionally you may find some success with attempting to follow some of the steps in this guide titled: How-To Fix Corrupt User Accounts in macOS if you suspect your macOS user account has become corrupt.

NOTE: I'd save this last suggestion as something to try once you've completely eliminated everything else.

Idea #6 - Create another account

To eliminate any issues with the system itself, you may want to try creating another macOS user account and grant this user sudo access as well. Once you've created this account, attempt to use sudo -l and verify that it functions. If it does not, then your issue is likely with the system either being mis-configured or perhaps some files/libraries that are critical to sudo have become corrupt.

Idea #7 - An issue with certain commands

I noticed someone asking you in comments about how you were able to run the command sudo -l and provide your password, but in subsequent commands such as sudo ls it failed.

This got me to thinking that perhaps your issue has something to do with an alias, shell function, or shell script that's getting picked up when you run sudo ls.

To investigate this idea a bit more you can do the following:

$ set -x; sudo -l; sudo ls; set +x 

This will enable more verbose output from commands. The set -x is what enables this. The set +x at the end will disable this.

In between we'll run the sudo -l and the sudo ls commands so we can see what commands are actually getting run when these execute.

For example:

$ set -x; sudo -l; sudo ls; set +x
+ sudo -l
...
...
+ sudo ls
...
...
+ set +x

Your output should look the same, if you see something other than sudo ls then you may have an alias or shell function that's inhibiting your ability to run sudo ls.