In most cases, the sudo
command displays the error unable to initialize PAM: No such file or directory
mostly appears when the pluggable authentication module file /etc/pam.d/sudo
has been edited with a typo or an incorrect PAM module.
If you have an older Mac, you can boot into single-user mode
, which allows you to access the command line mode of macOS directly, bypassing the UI.
-
Press and hold down the
COMMAND + S
keys until you see commands being executed in the Terminal. -
Open the
sudo
file withvi /Volumes/Macintosh\ - Data/etc/pam.d/
, fix what's wrong, save and close the file by switching fromINSERT
to theCOMMAND
mode with theESC
key, type:wq!
, and hitenter
. -
Close the Terminal via the menu from the top of the screen
-
Restart your machine
-
Back into the UI, open your Terminal, and verify the file content fix with
cat /etc/pam.d/sudo
. If everything looks good, give it a try by running:sudo whoami
.
On modern M1 Silicon machines, there is no single-user mode available, which is why the following steps differ here.
-
Boot into recovery mode by pressing and holding the power button on your Mac until you see Loading startup options.
-
Click on Options, and then click on Continue.
-
If requested, enter the password for an administrator account. This will take you to the macOS Utilities screen.
-
Go to "Disk Utility" and check if "Macintosh - Data" is mounted; if not, mount it by clicking the "Mount" button in the top right corner.
-
Close "Disk Utility" and return to "macOS Utilities".
-
Access the Terminal by selecting it from the menu at the top of the screen and launch the Terminal.
-
Open the
sudo
file withvi /Volumes/Macintosh\ - Data/etc/pam.d/
, correct any errors, save and close the file by switching fromINSERT
to theCOMMAND
mode using theESC
key, typing:wq!
, and pressingenter
. -
Close the Terminal using the menu at the top of the screen
-
Restart your machine
-
Go back to the UI, open your Terminal, and verify the file content fix with
cat /etc/pam.d/sudo
. If everything looks good, give it a try by running:sudo whoami
.
Cheers \w
This is unecessarily longer and more complicated than it needs to be to fix the issue of not being able to use Sudo. Here's how to fix it:
sudo
in terminalnano ~/sudo
and then paste this into the file:Press control + O to save and control + X to quit
/etc/pam.d
Now move the sudo file you saved in your home folder and move it to that folder. You will be asked for your password or Touch ID. Once the sudo file is successfully replaced, it should be good to go. You can type
sudo whoami
in terminal to verify it is working now.If you want to add TouchID as the method of using sudo, here is a bash script to simplify it and you can run this after every macOS update:
In terminal type this:
nano ~/enable_touch_id.sh
and then paste this in the file:Press control + O to save and control + X to quit
Next, make the script and executable:
chmod +x ~/enable_touch_id.sh
Now you can run this shell script everytime you update so you dont have to do it manually.
Complements to nicholasmangold for the shell script.