Skip to content

Instantly share code, notes, and snippets.

@fragolinux
Forked from RichardBronosky/touchid_sudo.sh
Last active July 18, 2022 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fragolinux/e5637fb30d82d6bc0b3b28309be2c1b8 to your computer and use it in GitHub Desktop.
Save fragolinux/e5637fb30d82d6bc0b3b28309be2c1b8 to your computer and use it in GitHub Desktop.
Use TouchID for sudo on modern MacBook Pro machines
#!/bin/bash
# curl -sL https://gist.githubusercontent.com/fragolinux/e5637fb30d82d6bc0b3b28309be2c1b8/raw/touchid_sudo.sh | bash
# This script is ready to copy-paste in whole, or just the line above (without the leading #)
# Use TouchID for sudo on modern MacBook Pro machines
# This script adds a single line to the top of the PAM configuration for sudo
# See: https://apple.stackexchange.com/q/259093/41827 for more info.
touchid_sudo(){
sudo bash -eu <<'EOF'
file=/etc/pam.d/sudo
# A backup file will be created with the pattern /etc/pam.d/.sudo.1
# (where 1 is the number of backups, so that rerunning this doesn't make you lose your original)
bak=$(dirname $file)/.$(basename $file).$(echo $(ls $(dirname $file)/{,.}$(basename $file)* 2> /dev/null | wc -l))
cp $file $bak
awk -v is_done='pam_tid' -v rule='auth sufficient pam_tid.so' '
{
# $1 is the first field
# !~ means "does not match pattern"
if($1 !~ /^#.*/){
line_number_not_counting_comments++
}
# $0 is the whole line
if(line_number_not_counting_comments==1 && $0 !~ is_done){
print rule
}
print
}' > $file < $bak
EOF
}
grep -q 'auth sufficient pam_tid.so' /etc/pam.d/sudo || touchid_sudo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment