目录

Linux人脸认证配置

首先需要确认电脑是否有红外摄像头, 输入 v4l2-ctl --list-devices 命令观察输出:

Integrated Camera: Integrated C (usb-0000:03:00.3-3):
        /dev/video0
        /dev/video1
        /dev/video2
        /dev/video3
        /dev/media0
        /dev/media1

可以看到有很多个 video 挂载, 依次用命令 ffplay /dev/video0 检查, 从 0 开始递增, 直到看到有黑白图像, 证明这就是机器的红外摄像头.

之后安装 linux-enable-ir-emitter , Arch 下可以用命令 yay -S aur/linux-enable-ir-emitte 安装, 首先在当前窗口临时开启 root 用户的 GUI 权限,运行命令:

xhost +local:root

然后运行 sudo linux-enable-ir-emitter configure , wayland 下可能报错, 可以尝试用 sudo QT_QPA_PLATFORM=wayland linux-enable-ir-emitter configure 或者 sudo QT_QPA_PLATFORM=xcb linux-enable-ir-emitter configure , 直到有弹出画面, 工具会让你确认它激活的是否是红外摄像头的补光灯, 如果激活正确, 可以看到红外摄像头的部分有红光闪烁, 并且弹出的画面也会有相对清晰的红外图像, 确认后输入 yes 即可, 具体输出如下:

 ❯ sudo QT_QPA_PLATFORM=xcb linux-enable-ir-emitter configure
[INFO] Stand in front of and close to the camera and make sure the room is well lit.
[INFO] Ensure to not use the camera during the execution.
[ WARN:[email protected]] global cap_v4l.cpp:914 open VIDEOIO(V4L2:/dev/video3): can't open camera by index
[ WARN:[email protected]] global cap.cpp:478 open VIDEOIO(V4L2): backend is generally available but can't be used to capture by index
[INFO] Configuring the camera /dev/video2.
Is the ir emitter flashing (not just turn on)? Yes/No? yes
[ERROR] The emitter is already working, skipping the configuration.

现在就激活了红外摄像头的补光灯. 之后需要开启服务, 以便在系统启动或休眠唤醒时, 重新向摄像头硬件发送那段特定的激活指令, 运行如下命令:

sudo systemctl enable --now linux-enable-ir-emitter.service

开始安装 Howdy 来实现认证逻辑, Arch Linux 下可以使用命令 yay -S aur/howdy-git 注意 aur/howdy 版本的 howdy 不会自动编译 pam_howdy.so 文件, 可能要手动触发编译, 具体可以看相关包的介绍页面.

接下来配置 howdy, 运行 sudo howdy config 进入配置文件, 找到 device_path 行, 将内容修改为确定的红外摄像头路径:

device_path = /dev/video2

运行 sudo howdy add 之后输入此次识别人脸的标签名称, 开始认证, 一般可以看到红外摄像头补光灯有红光闪烁, 之后可以不同角度, 戴/不戴眼镜都录入一次, 提高识别成功率.

sudo howdy list 可以查看目前录入的人脸, sudo howdy remove <ID> 可以删除对应 ID 的人脸信息.

人脸认证可以集成到 sudo / 登录界面 / 启动界面 等多个地方.

修改 /etc/pam.d/sudo 文件, 如下:

#%PAM-1.0

# 人脸认证逻辑
auth sufficient /lib/security/pam_howdy.so

# 原生认证逻辑
auth            include         system-auth
account         include         system-auth
session         include         system-auth

这样就会在密码输入之前验证人脸信息, 通过则不需要再输入密码, 缺点是容易直接执行相比输密码少了一道确认, 可以修改为以下内容:

#%PAM-1.0

# 人脸认证逻辑
# 询问密码 -> 无密码键入则扫描人脸
auth sufficient pam_unix.so try_first_pass likeauth nullok
auth sufficient /lib/security/pam_howdy.so

auth            include         system-auth
account         include         system-auth
session         include         system-auth

这样的逻辑就是 sudo 之后, 优先弹出密码输入选项, 可以输入密码, 也可以不输入密码直接空密码回车进入人脸认证逻辑.

/etc/pam.d/sddm 顶部加入人脸配置如下:

#%PAM-1.0

auth sufficient /lib/security/pam_howdy.so

auth        include     system-login
-auth       optional    pam_gnome_keyring.so
-auth       optional    pam_kwallet5.so

account     include     system-login

password    include     system-login
-password   optional    pam_gnome_keyring.so    use_authtok

session     optional    pam_keyinit.so          force revoke
session     include     system-login
-session    optional    pam_gnome_keyring.so    auto_start
-session    optional    pam_kwallet5.so         auto_start

/etc/pam.d/gdm-password 顶部添加内容:

#%PAM-1.0

auth sufficient /lib/security/pam_howdy.so

auth       include                     system-local-login
auth       optional                    pam_gnome_keyring.so

account    include                     system-local-login

password   include                     system-local-login
password   optional                    pam_gnome_keyring.so use_authtok

session    include                     system-local-login
session    optional                    pam_gnome_keyring.so auto_start

DMS 默认读取 /etc/pam.d/login 的认证逻辑, 不推荐直接修改, 创建文件 sudo cp /etc/pam.d/login /etc/pam.d/dankshell , 之后在顶部加入配置如下:

#%PAM-1.0

auth sufficient /lib/security/pam_howdy.so

auth       requisite    pam_nologin.so
auth       include      system-local-login
account    include      system-local-login
session    include      system-local-login
password   include      system-local-login

Polkit 默认认证文件在 /usr/lib/pam.d/polkit-1 , 更新后会重置, /etc/pam.d/ 下建立同名文件可以覆盖, 但经过测试像上面一样复制源过来直接加上配置会自动开始认证, 但是调用摄像头失败, 认证失败, 然后再次调用摄像头认证, 一直循环, 导致密码验证被锁定, 即时输入正确密码也无法使用 sudo 命令, 需要等 10 分钟左右, 使用命令 sudo faillock --user username --reset 来解除认证记录, --user 后面换成需要的用户名, 使用 faillock 来查看锁定记录.

根据 journalctl -f 推测是因为 polkit 进程没有访问红外摄像头权限导致鉴权失败, 可以尝试使用 udev 等方式, 使其获得权限, 考虑到 polkit 使用频率较低, 暂时不配置了, 有需要可以自行研究.