
























跨平台使用 Yubikey OpenPGP 的步骤大同小异,之前已经写了:
本文以 Fedora Workstation 43 (GNOME 桌面环境) 为例,作为 Linux 桌面上使用步骤的补充说明,方便我在新系统上快速设置。
[可选] 若需查看 2FA 口令或简单管理 Yubikey,可通过 Flatpak 安装 Authenticator:
flatpak install flathub com.yubico.yubioath
安装 OpenPGP 智能卡相关的核心组件:
sudo dnf install gnupg \
pcsc-lite \
pcsc-lite-ccid \
gnupg2-scdaemon \
pcsc-tools
启动服务:
sudo systemctl enable --now pcscd
和往常一样,在新系统上导入你的 PGP 公钥并设置信任:
# 这是我的 PGP 公钥
curl -fsSL https://pgp.dejavu.moe/ | gpg --import -
# 导入通用配置
wget https://raw.githubusercontent.com/drduh/YubiKey-Guide/master/config/gpg.conf -O ~/.gnupg/gpg.conf
chmod 600 ~/.gnupg/gpg.conf
配置 Git 签名信息:
git config --global user.name <username>
git config --global user.email <email>
git config --global user.signingkey <keyid>
git config --global commit.gpgsign true
git config --global gpg.program /usr/bin/gpg
检查系统中可用的 pinentry(密码输入)程序:
ls -lh /usr/bin/ | grep pinentry
-rwxr-xr-x. 1 root root 2.4K Jul 28 2025 pinentry
-rwxr-xr-x. 1 root root 77K Jul 28 2025 pinentry-curses
-rwxr-xr-x. 1 root root 89K Jul 28 2025 pinentry-gnome3
为了在终端和图形化应用中都能获得最佳使用体验,建议通过脚本实现 pinentry 切换器:检测到 TTY 时调用 pinentry-curses,否则调用 pinentry-gnome3。
vim ~/.gnupg/pinentry-wrapper.sh
写入判断逻辑:
#!/bin/bash
if [ "$PINENTRY_USER_DATA" = "curses" ]; then
exec /usr/bin/pinentry-curses "$@"
else
exec /usr/bin/pinentry-gnome3 "$@"
fi
编辑 gpg-agent.conf 配置文件:
vim ~/.gnupg/gpg-agent.conf
填入以下配置(路径中用户名替换为实际名称):
#ttyname $GPG_TTY
enable-ssh-support
allow-loopback-pinentry
# 指定自定义包装脚本,必须使用绝对路径
pinentry-program /home/dejavu/.gnupg/pinentry-wrapper.sh
# 或者选择你期望的 pinentry 程序
#pinentry-program /usr/bin/pinentry-curses
#pinentry-program /usr/bin/pinentry
#pinentry-program /usr/bin/pinentry-qt
#pinentry-program /usr/bin/pinentry-gnome3
#pinentry-program /usr/bin/pinentry-tty
#pinentry-program /usr/bin/pinentry-x11
# 缓存有效期设置(单位:秒)
default-cache-ttl 3600
max-cache-ttl 7200
default-cache-ttl-ssh 3600
max-cache-ttl-ssh 7200
查看认证子密钥的 KeyGrip 并写入 ~/.gnupg/sshcontrol
# 查看 KeyGrip
gpg -K --with-keygrip
对于 ~/.gnupg/scdaemon.conf:尽管 Yubikey Guide
中建议开启,但在实测中这可能会导致缓存失效,导致每次签名/认证都需要输入 PIN。
若遇到此类问题,建议注释相关选项:
#disable-ccid
#pcsc-shared
将以下内容追加到 Shell 配置文件(.bashrc 或 .zshrc)中:
# SSH GPG Agent for Yubikey
export GPG_TTY=$(tty)
export SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket)
gpgconf --launch gpg-agent
export PINENTRY_USER_DATA="curses"
gpg-connect-agent updatestartuptty /bye > /dev/null
然后根据 Shell 的类型,执行:
source ~/.zshrc
# 或者
source ~/.bashrc
最后重启相关服务,使配置生效:
sudo systemctl stop pcscd pcscd.socket && sudo systemctl start pcscd.socket
gpgconf --kill all && gpgconf --launch gpg-agent
重新插入 Yubikey,验证智能卡是否被正常识别:
执行签名测试,检查 PIN 输入窗口:
echo "test" | gpg --clearsign
在终端里预期调用的是 pinentry-curses,就像这样:

最后,验证 SSH 公钥读取及认证功能:
至此,Linux 平台上的 Yubikey OpenPGP 环境配置完成。
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。