82
  • Does Touch ID for the MacBook Pro Touch Bar support elevating admin privileges in macOS?

  • Somewhat differently, can the Touch ID give sudo access in Terminal?

I wonder this because I'm considering getting a YubiKey which can do string input to password fields, but Touch ID for Macs may render it unnecessary.

  • 2
    check this out: github.com/mattrajca/sudo-touchid – pathikrit Nov 14 '16 at 23:37
  • @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
16
  1. 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.

  2. See @conorgriffin's answer for instructions to enable TouchID for sudo.

| improve this answer | |
  • 1
    Has anyone actually done this? Looks pretty scary :) – Nic Cottrell Jan 27 '17 at 12:52
  • @NicCottrell yes, working fine on my end – swrobel Jan 27 '17 at 15:58
147

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/sudo file with a command-line editor such as vim or nano

  • The 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 auth line 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.so
    
  • Save the file. (Note: this file is normally read-only so saving your changes may require you to force the save, e.g. vim will require you to use wq! when saving)

  • Also note that pam_smartcard.so may 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.

    TouchID prompt

  • 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.

| improve this answer | |
  • 5
    Seems to work! Are there any security implications with this? – Znarkus Dec 11 '17 at 10:07
  • 2
    This 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
  • 2
    This worked only after I did apple.stackexchange.com/a/355880/158188 (but I did do this first) – aubreypwd Jun 18 '19 at 2: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 6 '19 at 14:01
  • 3
    @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 at 9:40
58

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.

iTerm preferences

| improve this answer | |
  • 3
    But if you want to keep sessions restoring feature, you can try this repo with a custom pam module. – Leo Jun 7 '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 2: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
  • 1
    The touch ID prompt hides the full-screened hotkey-window. Is there a workaround? – HappyFace Aug 12 '19 at 12:31
  • You saved my life... – Andrew Ravus Aug 21 '19 at 9:32
7

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.

| improve this answer | |
  • 6
    It 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: gitlab.com/gnachman/iterm2/issues/7608#note_153123852 – kossmoboleat Mar 28 '19 at 8:12
3

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

The complete script:

#!/bin/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)
  bak=$(dirname $file)/.$(basename $file).$(echo $(ls $(dirname $file)/{,.}$(basename $file)* | 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
}

touchid_sudo

This script demonstrates a few cool patterns that I love to teach people who are new to bash or DevOps.

  1. Create a backup file that is numbered rather than simply .bak on the end. (It looks gnarly, but that pattern works with whatever is in $file and is reusable.
  2. 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.
  3. Put a call to sudo bash -eu in your script so that you don't have tell the user to do it. (-eu are short for errexit and nounset and you should be using them!)
  4. Single quoting bash heredoc 'EOF' to prevent premature shell expansion.
  5. Making inline awk more readable.
| improve this answer | |
  • 1
    Appreciate the 'cool patterns' you shared. – displayName Sep 29 at 13:57
1

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 != ''
| improve this answer | |
1

To wrap up Andy and Glenjamin's solution into one play:

---
- hosts: localhost
  tasks:
  - name: install pam_reattach pam module
    homebrew:
      name: fabianishere/personal/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 ran with just ansible-playbook sudo-touchid.yml, where sudo-touchid.yml is what I named this play.

The first step installs Fabian's pam_reatttach, that allows sudo to work in iTerm, tmux, etc. It isn't in the default brew, so we're fetching it from his own branch.

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.

| improve this answer | |
  • 1
    This answer may benefit from an explanation on how to actually use it and why the different parts are necessary. – nohillside May 28 at 14:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .