Can Touch ID on Mac authenticate sudo in Terminal?
-
2check this out: https://github.com/mattrajca/sudo-touchid – pathikrit Nov 14 '16 at 23:37
-
1@pathikrit - close but it looks like a PAM module would be a much better solution than a forked sudo. I haven't seen any that have been written yet; I may try my hand at it – Brad Dwyer Nov 16 '16 at 18:58
-
1Tmux users need to setup [pam_reattach](https://github.com/fabianishere/pam_reattach). Install e.g. via `brew install pam-reattach` and add the line `auth optional /opt/homebrew/lib/pam/pam_reattach.so` (or `/opt/homebrew/lib/...` for Intel Macs) before the line `auth sufficient pam_tid.so`. ([via](/a/444078)) – cachius Aug 12 '22 at 13:22
11 Answers
To allow TouchID on your Mac to authenticate you for sudo access instead of a password you need to do the following.
Open Terminal
Switch to the root user with
sudo su -Edit the
/etc/pam.d/sudofile with a command-line editor such asvimornanoThe contents of this file should look like one of the following examples:
-
# sudo: auth account password session auth required pam_opendirectory.so account required pam_permit.so password required pam_deny.so session required pam_permit.so -
# sudo: auth account password session auth sufficient pam_smartcard.so auth required pam_opendirectory.so account required pam_permit.so password required pam_deny.so session required pam_permit.so
-
You need to add an additional
authline to the top so it now looks like this:# sudo: auth account password session auth sufficient pam_tid.so auth sufficient pam_smartcard.so auth required pam_opendirectory.so account required pam_permit.so password required pam_deny.so session required pam_permit.soSave the file. (Note: this file is normally read-only so saving your changes may require you to force the save, e.g.
vimwill require you to usewq!when saving)Also note that
pam_smartcard.somay not be present on older MacOS versions.Exit from the root user or start a new terminal session.
Try to use
sudo, and you should be prompted to authenticate with TouchID as shown below.If you click 'Cancel,' you can just enter your password at the terminal prompt. If you click 'Use Password' you can enter your password in the dialog box.
If you SSH into your machine it will fall back to just use your password, since you can't send your TouchID fingerprints over SSH.
Note: See answer by user Pierz below if you're using iTerm, as there's a setting you need to explicitly change to enable this feature.
Note:
Recent MacOS updates may remove the entry. If TouchID stops working for sudo then check if the entry was removed and add it back in, following these instructions again.
- 115
- 7
- 15,678
- 8
- 43
- 65
-
7
-
6This should be the accepted answer - It's clear what happens, how it's done, and there is no need to install third party software. – Jeppe Mariager-Lam Jun 20 '18 at 11:19
-
Is there a way to replace all password prompts with TouchID? Whether it's in terminal, or in System Preferences, or in Keychain, or for Package installations? – Max Coplan Sep 20 '18 at 22:02
-
-
1There are a bunch of other files in /etc/pam.d. What would adding this line to the top of those do? For example, would adding it to /etc/pam.d/su work? What do the other files do? (does this also deserve its own question?) – Max Coplan Sep 20 '18 at 22:22
-
Tried doing for su, but I don't think it worked. I wonder why that is... – Max Coplan Sep 21 '18 at 17:18
-
If you ask a new question for this stuff you’re more likely to get answers – conorgriffin Sep 22 '18 at 11:16
-
2This worked only after I did https://apple.stackexchange.com/a/355880/158188 (but I did do this first) – aubreypwd Jun 18 '19 at 02:23
-
1@conorgriffin Hey dude, I've accidentally downvoted this, wanna make a small edit, so I can change my vote to a well deserved upvote? – ruohola Dec 06 '19 at 14:01
-
-
-
apple watch provides a similar prompt with TouchID.. it looks like it's the same mechanism. has anyone managed to make it work with the watch too? – Paschalis Feb 20 '20 at 22:24
-
17@Znarkus one potential security implication is that, at least in the US, you cannot be compelled to give up a password by a court (it's considered a violation of the 5th amendment), but your biometrics are not secret, so you can absolutely be forced by a court to biometric auth. i'm not sure that's a concern for most people, but it's a good piece of knowledge to have. – sneak Mar 21 '20 at 09:40
-
Is `pam_tid.so` more recently known as `pam_smartcard.so`? I used to have `auth sufficient pam_tid.so` in my `pam.d/sudo` file, but I now see `auth sufficient pam_smartcard.so` for some reason. Touch ID didn't seem to be working for sudo anymore, which is why I looked in the file. – P A N Jul 02 '20 at 07:10
-
1It seems that `pam_tid.so` was removed in a recent MacOS update. You can just add it back in along with the `pam_smartcard.so` entry, I'll update the answer – conorgriffin Jul 02 '20 at 17:43
-
6No need for all the steps of doing `sudo su -`, editing, force-saving with `wq!`, and exiting root. Just use `sudo -e /etc/pam.d/sudo` which opens the file in your default editor and seamlessly does all of the required priviledge escalation for you. – Caesar Feb 16 '21 at 21:03
-
Don't do `wq!`. Do `w!` first. Test, test, test, _then_ quit vim. If you make a typo you'll be locked out of your account. – Rol Feb 17 '21 at 19:16
-
Does this work anymore for you on the latest macOS Big Sur (11.2.3)? – Iulian Onofrei Mar 17 '21 at 09:02
-
Works for me on macOS Big Sur (11.2.3). Which step fails for you? @IulianOnofrei I have both lines in `/etc/pam.d/sudo`: `auth sufficient pam_tid.so` `auth sufficient pam_smartcard.so` – L.R. Mar 17 '21 at 12:24
-
That doesn't seem to "survive" an update of OSX, I had to apply these changes again after an update from 11.5.2 to 11.6. Any idea how to improve the solution to make it more robust? – Danny Lo Oct 16 '21 at 12:31
-
From what I understand about MacOS updates, this is because of how updates get applied. They effectively replace the existing OS image with the new one. So they overwrite your settings. Ways around it are probably to either automate the update of the file if the setting is not there or build some "check and update" logic into your `sudo` command. Pretty horrible in both cases, probably easier to just manually fix it once a year. Some ideas: https://news.ycombinator.com/item?id=26302139 – conorgriffin Oct 18 '21 at 14:38
-
after adding the line, i'm getting a window popup with my username prefilled, asking to enter the password. i'm on M1 pro with monterey, if that makes a difference? – ierdna Nov 22 '21 at 18:46
-
@ierdna it sounds like it's not working for you. Can you try this command to show the contents of your file and maybe ask a new question? `cat /etc/pam.d/sudo` – conorgriffin Nov 23 '21 at 22:19
-
is there something immutable that doesn't require changing fs? – Alexander Mills Nov 25 '21 at 18:29
-
awesome. Minor tip: since you were just sudo'ing to edit the file, you probably won't be prompted for password/touch afterwards to test! Just open a new terminal window tho. – orion elenzil Sep 07 '22 at 16:12
-
1I had to log out and log back in before it started working on macOS Ventura. Pretty scary, but it seems to work now! – Julia Nov 18 '22 at 12:42
If you're using iTerm2 (v3.2.8+) you may have seen Touch ID failing to work with sudo in the terminal despite having made the pam_tid.so modification as above, and it working in previous versions. This is down to an advanced feature that seems to be now enabled by default - this needs to be turned off here: iTerm2->Preferences > Advanced > (Goto the Session heading) > Allow sessions to survive logging out and back in.
Alternatively you can use this pam_reattach module to retain the session feature and TouchID sudo at the same time.
-
3But if you want to keep sessions restoring feature, you can try [this repo](https://github.com/fabianishere/pam_reattach) with a custom pam module. – Leo Jun 07 '19 at 23:35
-
I had to do this step, but worked right after I ensured `No` was set in the setting. You can also search for "touch" and the option will turn up. – aubreypwd Jun 18 '19 at 02:22
-
Just tested: When using `pam_reattach`, one needn't either change the setting or restart iTerm — everything works right away! (I haven't tested if sessions "actually survive" since I have no clue how, but I didn't need change the setting). – Blaisorblade Jul 31 '19 at 16:29
-
1The touch ID prompt hides the full-screened hotkey-window. Is there a workaround? – HappyFace Aug 12 '19 at 12:31
-
4PSA: __Do not__ typo anything in `/etc/pam.d/sudo`. If you do, you'll have to load up single-user mode to change it back. – Michael Aug 10 '20 at 16:44
-
I am currently using iTerm Build 3.4.2 and I don't have to do this in order to TouchID to work. I just added the pam_tid.so as mentioned in the accepted answer – Melvin Sy Dec 03 '20 at 10:36
-
@Michael Alternatively you can enable root via `dsenableroot` which first prompts for your password and then for a new-to-set root password. If you don't have enabled the root user before, that is. ([via](https://superuser.com/a/1233928)) `dsenableroot -d` to deactivate again. ([via](/q/437850)) – cachius Aug 12 '22 at 13:09
-
@Michael Actually, it is not necessary. The only thing that is needed is to temporarily enable root user on macos and then use 'su' to login in shell as root, and then temporarily `chmod +w /etc/pam.d/sudo`. Now you can fix any typos and then revert permissions back with `chmod -w /etc/pam.d/sudo`... And then disable root user in macos. – Drew Aug 21 '22 at 01:06
TouchID does support elevating privileges, but as of now, it only seems to be supported in Apple's own apps. My guess is that 3rd party apps will have to be updated to support it, unfortunately. I still end up typing in my password a lot.
See @conorgriffin's answer for instructions to enable TouchID for sudo.
- 5,442
- 7
- 32
- 59
- 1,139
- 9
- 20
I have created a simple script that enables sudo to use the TouchID PAM module exactly as conorgriffin explains. It does it in a single script that you can copy-paste to a terminal in it's entirety or use the "curl pipe bash" shortcut:
curl -sL https://gist.githubusercontent.com/RichardBronosky/31660eb4b0f0ba5e673b9bc3c9148a70/raw/touchid_sudo.sh | bash
#!/usr/bin/env bash
# curl -sL https://gist.githubusercontent.com/RichardBronosky/31660eb4b0f0ba5e673b9bc3c9148a70/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)
file_dir="$(dirname "$file")"
file_name="$(basename "$file")"
mapfile -t backup_list < <( ls -A "$file_dir"/{,.}"$file_name"* 2>/dev/null )
backup_count="${#backup_list[@]}"
backup_ext="$backup_count"
backup="$file_dir/.$file_name.$backup_ext"
cp "$file" "$backup" 2>/dev/null || touch "$file" "$backup"
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 < $backup
EOF
}
touchid_sudo
This script demonstrates a few cool patterns that I love to teach people who are new to bash or DevOps.
- Create a backup file that is numbered rather than simply
.bakon the end. (It looks gnarly, but that pattern works with whatever is in$fileand is reusable. - To make it safe to do
curl ... | bash, wrap everything in a function and call it on the last line. That way if the download is interrupted, nothing is (partially) done. - Put a call to
sudo bash -euin your script so that you don't have tell the user to do it. (-euare short for errexit and nounset and you should be using them!) - Single quoting bash heredoc
'EOF'to prevent premature shell expansion. - Making inline
awkmore readable.
- 641
- 8
- 11
-
4
-
If I may, I would suggest two changes: 1) use `#!/usr/bin/env bash` to support Apple moving the bash executable, and 2) add `2 > /dev/null` to the last `ls` in the backup file name counter; otherwise, ls will fail on the first script invocation (because no backup file exists yet). Otherwise, these are great suggestions. Thank you! – Moritz Friedrich Jun 24 '22 at 07:00
-
1Thank you @MoritzFriedrich! You are correct on all points and I've made changes. ☮❤ – Bruno Bronosky Jun 24 '22 at 19:33
-
The script above erroered for me: `bash: line 6: 0: ambiguous redirect` – Jan Katins Feb 20 '23 at 12:44
-
The script assumes a "modern" version of Bash, @JanKatins. You'll need to install a recent version of `bash` from Homebrew or elsewhere. MacOS comes with Bash 3.2 (the last version under GPL 2) for licensing reasons. Bash 3.2 fails on line 17 for 2 reasons (which is line 6 of the heredoc): 1. For some reason, the `<(` generates 2 file descriptors, which is why it's saying `ambiguous redirect`. This could be fixed by using a single set of double-quotes: `"$file_dir/{,.}$file_name*"`. 2. The `mapfile` builtin wasn't added until Bash 4.0. – Craig Buchek Feb 23 '23 at 00:07
-
@BrunoBronosky This script doesn't work with the builtin MacOS Bash 3.2. I think it could be made to work by removing the `mapfile` line and setting `backup_count` to `"$(( $(ls -A "$file_dir/"{,.}"$file_name"* 2>/dev/null | wc -l) ))""`. – Craig Buchek Feb 23 '23 at 00:18
-
1
You can use fingerprint for getting sudo access in the terminal or iTerm, just add auth sufficient pam_tid.so to the first line to your /etc/pam.d/sudo file.
- 211
- 2
- 2
-
6It might be interesting for other to know that the current iTerm version v3.2.8 doesn't allow this when another default option is set. You have to go into Preferences -> Advanced and deactivate `Allow sessions to survive logging out and back in`: https://gitlab.com/gnachman/iterm2/issues/7608#note_153123852 – kossmoboleat Mar 28 '19 at 08:12
To wrap up Andy and Glenjamin's solution into one play:
---
- hosts: localhost
tasks:
- name: install pam_reattach pam module
homebrew:
name: pam-reattach
state: present
register: reattach_result
- name: detect touch id support
shell: pgrep ControlStrip
ignore_errors: true
register: touch_id_result
- name: enable touch id for sudo commands
lineinfile:
path: /etc/pam.d/sudo
line: 'auth sufficient pam_tid.so'
insertbefore: '^auth sufficient pam_smartcard.so$'
become: yes
when: touch_id_result.rc == 0 and touch_id_result.stdout != ''
- name: enable persistent touch id for tmux and iterm
lineinfile:
path: /etc/pam.d/sudo
line: 'auth optional pam_reattach.so'
insertbefore: '^auth sufficient pam_tid.so$'
become: yes
when: reattach_result == 0
This can be run with just ansible-playbook sudo-touchid.yml, where sudo-touchid.yml is what I named this play.
The first step installs Fabian's pam_reattach, which allows sudo to work in iTerm, tmux, etc.
The second checks to see if this Mac has a touchbar; otherwise we're going to bail out.
Third, we're seeing if we already added pam_tid.so to /etc/pam.d/sudo, and only if there is a touchbar.
Finally, we also add pam_reattach.so as an optional auth method. According to the author, he might have have some unknown bug, and if we used required it could result in a lockout.
- 103
- 3
- 61
- 1
- 3
-
3This answer may benefit from an explanation on how to actually use it and why the different parts are necessary. – nohillside May 28 '20 at 14:59
-
Turns out `pgrep ControlStrip` succeeds also on my M1 Max MBP 2021, even if it has no touchbar any more. – Blaisorblade Dec 24 '21 at 20:20
-
really? I have a M1 Max MBP 2021 on macOS 12.6.2 (Monterey, ARM) and there is no ControlStrip process. – huyz Jan 16 '23 at 14:52
I created the following ansible tasks to enable touch id for sudo commands if your computer supports it:
- name: detect touch id support
shell: pgrep ControlStrip
ignore_errors: true
register: touch_id_result
- name: enable touch id for sudo commands
lineinfile:
path: /etc/pam.d/sudo
line: 'auth sufficient pam_tid.so'
insertbefore: '^auth sufficient pam_smartcard.so$'
become: yes
when: touch_id_result.rc == 0 and touch_id_result.stdout != ''
- 131
- 5
My 1 string answer:
sudo su root -c 'chmod +w /etc/pam.d/sudo && echo "auth sufficient pam_tid.so\n$(cat /etc/pam.d/sudo)" > /etc/pam.d/sudo && chmod -w /etc/pam.d/sudo'
What does a command do?
- First, I indicate that I want to do everything from root (using sudo, the trick will not work (more precisely, it will, but more difficult to implement)
- Then I give write permissions to /etc/pam.d/sudo
- Then I add the line
auth sufficient pam_tid.soto the beginning of the file. See that post - Then I take away the granted write permissions /etc/pam.d/sudo , taking into account Matin ZD remarks
P.S. While experimenting broke sudo and su in the terminal, it is repaired like this https://support.apple.com/en-us/HT204012 enable root
su - root
vim /etc/pam.d/sudo
Revert the file to its original state
- 666
- 1
- 5
- 19
- 21
- 3
-
The PS just saved my life! But I needed the English link not the Russian one (https://support.apple.com/en-us/HT204012), so I sent an edit. – Blaisorblade Dec 24 '21 at 19:13
This one-liner will insert the auth line described here as the second line of the sudo config file (so that it appears first after the comment line).
sudo sed -i -- '2s/^/auth sufficient pam_tid.so\n/' /etc/pam.d/sudo
- 367
- 3
- 17
Here's a Gist to make it as simple as possible.
https://gist.github.com/fraune/0831edc01fa89f46ce43b8bbc3761ac7
- 13
- 3
-
Please summarise your Gist into this answer. Link-only answers are not preferred here, as if the link disappears, your answer is worthless. – Andy Griffiths Feb 18 '23 at 00:45
One thing to mention is that you should first change the file permission then try to make changes:
sudo su -
chmod u+w /etc/pam.d/sudo
Adding this line at first line:
# sudo: auth account password session
auth sufficient pam_tid.so # << Add this here ;)
auth sufficient pam_smartcard.so
auth required pam_opendirectory.so
account required pam_permit.so
password required pam_deny.so
session required pam_permit.so
Then revert it back file permissions when you are done with:
chmod u-w /etc/pam.d/sudo
- 117
- 2
-
1I've been doing and recommending this change for years and never heard of anyone needing to do this. If this is sometimes necessary, I think explaining when would be useful, otherwise this appears to be completely extraneous. – grg Jan 16 '21 at 22:43
-
I had a focus on permissions, not for that line ;) @grg Also If you want to change the file or write to it by default the file permission is 444 which is not writable by anyone. – Matin Zadeh Dolatabad Jan 17 '21 at 23:20

