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
Steps I followed after I realized I had a type in /etc/pam.d/sudo file.
From finder menu go to /System/Library/CoreServices/Applications
Choose “Directory Utility” from the window that opens
click on the local to Unlock it (Enter password when asked).
Go to Edit in the menu bar
Select “Enable root user”
Logout from current user
login as a root user (username: root; password: your root password)
Once logged in you can open terminal and correct your typo.
Login back to your account.. (Now it must be working fine.)
Don’t forget to “disable root user”