Finger of god: using sudo with TouchID on macOS

7 minute read

Diggin’ Dotfiles Collection

It’s time to dig the dotfiles, and to dig in to them! This post is a part of a blog post collection called Diggin’ Dotfiles where I unearth some gems from my personal dotfiles repo erikw/dotfiles.

The following articles are a part of this series:

Finger of god: using sudo with TouchID on macOS

Have you got a fancy MacBook with a TouchID button? Did you know that it’s super easy to configure sudo to use it for authentication?

All you have to do on a standard macOS system is to prepend a line to /etc/pam.d/sudo to enable the module. With your favorite editor, say neovim, edit the file like this $ sudo nvim /etc/pam.d/sudo:

# sudo: auth account password session
auth       sufficient  # Added: allow TouchID. Original file below:
auth       sufficient
auth       required
account    required
password   required
session    required

You’ll need to force save the file with :w! because there write permission on the file is not set.

To verify that it works, you might need to start by killing the active sudo session first if you’re configured it to have a timeout.

$ sudo -k
$ sudo ls /var/root  # This should now open a TouchID popup!

TouchID dialog from sudo


If you live inside Tmux you’ll soon or directly notice that it doesn’t work and you still have to manually type your passwords. The issue is that programs like tmux survive a GUI user session and is thus not connected in the same way as a GUI program. For background programs like this TouchID is not available or working properly.

There’s a solution though! To use the pam_reattach module. Start by installing it from Homebrew:

$ brew install pam-reattach

And now we need to prepend it to /etc/pam.d/sudo using the full path to it:

# sudo: auth account password session
auth       optional       /opt/homebrew/lib/pam/  # For tmux
auth       sufficient  # TouchID
auth       sufficient
auth       required
account    required
password   required
session    required

Now you can use TouchID with sudo from within tmux!

Automation of OS upgrade reset

After using this setup for a while you might experience that it stops working all of a sudden. That’s because on OS updates the /etc/pam.d/* files will be rested to factory default. While you could make the effort to edit the file after every OS update, we can script this to make it easier! The script that I’ve made will:

  • Update the sudo PAM file if it does not have the TouchID module installed.
  • Make a backup of the original file with a timestamp, in case something goes wrong.

Save this script as somewhere in your $PATH e.g. ~/bin:

#!/usr/bin/env bash
# Enable TouchID on macOS unless it already is. This script should be run after OS updates as the sudo file is reseted then.
# Dependencies: $(brew install pam-reattach) for tmux
# Inspired by:

# Run this script as sudo. Ref:
test "$EUID" -eq 0 || exec sudo bash "$0" "$@"

SCRIPT_NAME=$(basename "$0")

read -r -d '' ADDED_MODULES <<-EOF
	# Added by ${SCRIPT_NAME}:
	auth       optional       /opt/homebrew/lib/pam/
	auth       sufficient

	# Original:

timestamp=$(date "+%Y-%m-%d-%H%M%S")

if grep -q "" "$PAM_FILE"; then
	echo "TouchID for sudo is already enabled."
	cp $PAM_FILE ${PAM_FILE}.${timestamp}.bak
	echo -e "$ADDED_MODULES" | cat - $PAM_FILE > ${PAM_FILE}.new
	mv ${PAM_FILE}.new $PAM_FILE

	echo "TouchID for sudo is now enabled."

You can find the latest version of this script in my dotfiles.

Recovering from screw-ups

Be careful when editing /etc/pam.d/sudo because if you make a mistake this can lock you out of sudo. I accidentally did this myself while writing the post as a tried my own script from this computer which does not have TouchID like my work laptop does. What then, wait until the next OS upgrade to reset the file?

There’s a few ways to recovery luckily! You can reboot the computer with cmd+r and restore the file with the recovery terminal shell, or maybe more easy make a copy of the file to your user space and drag-n-drop it back to /etc/pam.d/ with Finder which will then ask you for your password to complete the operation. See more details in this supersuer answer.

Now, use your finger wisely!

Leave a comment

Your email address will not be published. Required fields are marked *