Newshot

你可以用 Touch ID 执行 sudo

Translation(model translation, not the source's own text)

你可以用 Touch ID 来执行 sudo

每次我的 agent 运行 sudo 都要敲密码,我实在受够了,于是换成了 Touch ID。现在会弹出一个小提示。大约一分钟就能设置好。

sudo 的 Touch ID 提示,显示是谁发起的请求以及具体命令
sudo 的 Touch ID 提示,显示是谁发起的请求以及具体命令

开启方法

sudo 会向 PAM(macOS 从 Unix 继承的可插拔认证模块)询问你是不是你声称的那个人。Apple 提供了一个名为 pam_tid.so 的 PAM 模块,改为向指纹传感器询问。从 macOS Sonoma 起,本地修改可以写在一个能挺过系统更新的配置文件里,Apple 甚至提供了模板,其中正确的行已被注释掉:


        sudo cp /etc/pam.d/sudo_local.template /etc/pam.d/sudo_local
sudo nano /etc/pam.d/sudo_local
      

删掉这一行前面的 # 并保存:


        auth       sufficient     pam_tid.so
      

sufficient 表示只要指纹匹配就足够了。如果你取消提示,sudo 会像以前一样回退到询问密码,所以不会把自己锁在外面。打开一个新终端运行 sudo -v 试试。

我的版本

自带的模块有一个问题。只要有东西在捕获屏幕,macOS 就不再在这些提示中提供 Touch ID,而我的 DisplayLink 扩展坞一直在捕获屏幕。屏幕共享和屏幕录制也会触发。

所以我让我的 agent 写了一个小型 PAM 模块,直接通过 Apple 的 LocalAuthentication API 请求指纹,这样在屏幕被捕获时依然可用。由于它自己绘制提示,还能显示是谁发起的请求,并展示完整命令。

源代码、安装步骤,以及我如何排查屏幕捕获问题的记录,都在我 skills 仓库的 macos-touchid-sudo skill 里。

这个模块是我大约两个月前写的,从那以后每天都在用,没有任何问题。我把它放在 skills 仓库里,是为了能在我另外两台 MacBook 上安装,在那两台机器上也同样好用。

我的桌面,三台 MacBook,全都在用 Touch ID 执行 sudo
我的桌面,三台 MacBook,全都在用 Touch ID 执行 sudo

订阅以获取新文章。

The translation is machine-generated and individual terms may be off. Nothing is rewritten or cut; the original is authoritative.

Published by Lobsters
This site only summarises and aggregates; copyright stays with the original author. Headlines and summaries follow the source — where they differ, the original wins.
Read the original ↗