I deleted all my SSH keys
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
I deleted all my SSH keys
I deleted all my SSH keys 2026-11-04 11:00 security I’ve been using ed25519-sk keys with YubiKeys since practically the moment they were available. But because of some specific use-cases (and because, frankly, YubiKeys are expensive) I’ve had a few traditional on-disk keys hanging around for a while just waiting for me to finally decide to get rid of them. This post documents how I’m using various secure enclaves (YubiKey, TPM, Apple Secure Enclave) to hold/encrypt SSH key material and achieve the goal of not having any unprotected key material living on-disk.
我删除了所有的 SSH 密钥 2026-11-04 11:00 安全。自 ed25519-sk 密钥与 YubiKey 几乎一经推出,我就一直在使用它们。但由于一些特定的用例(坦率地说,YubiKey 很贵),我保留了一些传统的磁盘密钥,一直等着最终决定将它们彻底清除。本文记录了我如何利用各种安全隔离区(YubiKey、TPM、Apple Secure Enclave)来存储/加密 SSH 密钥材料,并实现磁盘上不留任何未受保护密钥材料的目标。
First – Isn’t using a passphrase enough? Probably. I don’t intend to imply that this post is the only way that anyone should be doing SSH keys. Using a passphrase will encrypt the key with bcrypt & AES which keeps it protected even if the on-disk keyfile leaks somehow. And with a functioning ssh-agent, the passphrase should only need to be entered once per session, with the decrypted key material being held in RAM after that point. So yes, the ’traditional’ method is probably fine for most threat models. But I’m still keen to move to hardware-backed keys anyway, mostly for the satisfaction.
首先,使用密码短语(passphrase)难道不够吗?可能够了。我并不是想暗示这篇文章是每个人使用 SSH 密钥的唯一方式。使用密码短语会通过 bcrypt 和 AES 加密密钥,即使磁盘上的密钥文件以某种方式泄露,它也能保持受保护状态。而且在 ssh-agent 正常工作的情况下,每个会话只需输入一次密码短语,之后解密后的密钥材料会保存在内存中。所以是的,对于大多数威胁模型来说,“传统”方法可能已经足够好了。但我仍然热衷于转向硬件支持的密钥,主要是为了获得心理上的满足感。
About hardware-resident keys When I say ‘hardware-resident’ keys, I mean keys that use a secure enclave or purpose-built hardware like YubiKeys to store or protect the key material. The theoretical benefit here is that the hardware used to generate the SSH key is needed in order to use the key. There’s no mechanism that allows leaking or exfiltrating keys that generated using this method. Any material that does exist on-disk is useless without the hardware.
关于硬件驻留密钥:当我提到“硬件驻留”密钥时,我指的是使用安全隔离区或像 YubiKey 这样专用硬件来存储或保护密钥材料的密钥。其理论上的优势在于,使用该密钥时必须具备生成该密钥的硬件。没有任何机制允许泄露或窃取通过此方法生成的密钥。任何存在于磁盘上的材料,如果没有硬件配合都是毫无用处的。
Goals So I want to: Switch to using hardware-resident keys entirely Do so as natively as possible, avoiding additional software where I can Test these in various real-world scenarios, such as running within an LXC or noninteractively mounting an SSHFS server
目标:我想要:完全切换到使用硬件驻留密钥;尽可能以原生方式实现,在可能的情况下避免使用额外的软件;在各种实际场景中测试这些密钥,例如在 LXC 中运行或非交互式挂载 SSHFS 服务器。
Hardware I’m going to try three different hardware methods for achieving this, which I think will cover all of my needs: Method 1: YubiKey Method 2: TPM Method 3: Apple Secure Enclave
硬件:我将尝试三种不同的硬件方法来实现这一目标,我认为这些方法可以满足我的所有需求:方法 1:YubiKey;方法 2:TPM;方法 3:Apple Secure Enclave。
Method 1: YubiKey With a YubiKey (5 and newer), SSH keys can be directly enrolled into the device using ed25519-sk (where the -sk stands for “Security Key”). This doesn’t require any additional software beyond OpenSSH itself. To store a new SSH key on a YubiKey, insert it and run: ssh-keygen -t ed25519-sk -O resident -O application=“ssh:yournamehere” -f ~/.ssh/id_ed25519-sk
方法 1:YubiKey。使用 YubiKey(5 及更新版本),SSH 密钥可以直接通过 ed25519-sk(其中 -sk 代表“安全密钥”)注册到设备中。除了 OpenSSH 本身,这不需要任何额外的软件。要在 YubiKey 上存储新的 SSH 密钥,请插入设备并运行:ssh-keygen -t ed25519-sk -O resident -O application=“ssh:yournamehere” -f ~/.ssh/id_ed25519-sk
The application tag set in the command is arbitrary and can be set to whatever memorable string you prefer to differentiate between multiple SSH keys if you want to create multiple keys on the same YubiKey. If you have ykman installed, you can now inspect the list of credentials enrolled in the device: $ ykman fido credentials list Enter your PIN: Credential ID RP ID Username Display name 0f41fb61… ssh:yournamehere openssh openssh
命令中设置的应用程序标签是任意的,如果您想在同一个 YubiKey 上创建多个密钥,可以将其设置为您喜欢的任何易记字符串,以区分不同的 SSH 密钥。如果您安装了 ykman,现在可以检查设备中注册的凭据列表:$ ykman fido credentials list 输入您的 PIN:Credential ID RP ID Username Display name 0f41fb61… ssh:yournamehere openssh openssh
If ykman complains that you haven’t set a FIDO PIN on the YubiKey yet, you can do this with ykman fido access change-pin. Note that you don’t actually need ykman to enrol or use SSH keys with a YubiKey but it does make managing the device more convenient. Once the key has been created, you can retrieve the public key from disk: $ cat ~/.ssh/id_ed25519-sk.pub sk-ssh-ed25519@openssh.com AAAAGnNrLXNzaC1lZDI1NTE5QG9wZW5zc2guY29tAAAAIA9B+2GGCkQvBzpa4WobQhfC9YU5Su3pKXBmVLSXEqfRAAAAEHNzaDp5b3VybmFtZWhlcmU= alice@hostname
如果 ykman 提示您尚未在 YubiKey 上设置 FIDO PIN,您可以使用 ykman fido access change-pin 来完成。请注意,您实际上并不需要 ykman 来注册或使用 YubiKey 的 SSH 密钥,但它确实使设备管理更加方便。密钥创建完成后,您可以从磁盘检索公钥:$ cat ~/.ssh/id_ed25519-sk.pub sk-ssh-ed25519@openssh.com AAAAGnNrLXNzaC1lZDI1NTE5QG9wZW5zc2guY29tAAAAIA9B+2GGCkQvBzpa4WobQhfC9YU5Su3pKXBmVLSXEqfRAAAAEHNzaDp5b3VybmFtZWhlcmU= alice@hostname
This key can be used like any other SSH public key – added to authorized_keys on a host, copied with ssh-copy-id, or pasted into services that depend on SSH like Git forges. You will also find a ~/.ssh/id_ed25519-sk file on disk (without the .pub extension). Note that although this looks like a private key, it doesn’t actually contain the key material. This is just a handle to find the relevant key on the hardware device.
此密钥可以像任何其他 SSH 公钥一样使用——添加到主机的 authorized_keys 中,使用 ssh-copy-id 复制,或粘贴到依赖 SSH 的服务(如 Git 托管平台)中。您还会在磁盘上找到一个 ~/.ssh/id_ed25519-sk 文件(没有 .pub 扩展名)。请注意,虽然它看起来像私钥,但它实际上并不包含密钥材料。这只是一个用于在硬件设备上查找相关密钥的句柄。
To configure the key for authentication, it can be enrolled with ssh-add like a regular key, or specified directly in ~/.ssh/config: Host blog.infected.systems IdentityFile ~/.ssh/id_ed25519-sk From here, you can now SSH into the destination host as with a regular key. SSH will ask you to tap the YubiKey to confirm presence when the SSH command runs.
要配置用于身份验证的密钥,可以像普通密钥一样使用 ssh-add 注册,或者直接在 ~/.ssh/config 中指定:Host blog.infected.systems IdentityFile ~/.ssh/id_ed25519-sk。至此,您现在可以像使用普通密钥一样通过 SSH 登录目标主机。当运行 SSH 命令时,SSH 会要求您轻触 YubiKey 以确认您的存在。
Unattended YubiKey usage When using an ed25519-sk key created with the default settings as above, you will be asked to confirm your presence whenever the key is used by physically touching the YubiKey. It’s probably a good idea to keep this default, but if you have a specific use-case that requires unattended key usage, you can generate a no-touch-required key as follows: ssh-keygen -t ed25519-sk -O resident -O no-touch-required -O application=“ssh:nameyourkey” -f ~/.ssh/id_ed25519-sk
无人值守的 YubiKey 使用:当使用上述默认设置创建的 ed25519-sk 密钥时,每次使用密钥时,系统都会要求您通过物理触摸 YubiKey 来确认您的存在。保留此默认设置可能是一个好主意,但如果您有需要无人值守使用密钥的特定用例,可以按如下方式生成无需触摸的密钥:ssh-keygen -t ed25519-sk -O resident -O no-touch-required -O application=“ssh:nameyourkey” -f ~/.ssh/id_ed25519-sk
When using a no-touch-required key, although it isn’t possible for a malicious actor to exfiltrate the key even with root access to the host the YubiKey is plugged into, a malicious actor with access could use the key without requiring any presence detection or human interaction. So do make sure this is actually suitable for your risk appetite before doing it. To use a no-touch-required key for unattended authentication to remote servers, you need to prepend the entry in the authorized_keys file on the destination host with no-touch-required, otherwise OpenSSH’s default security settings will reject the key. For example: $ cat ~/.ssh/authorized_keys no-touch-required sk-ssh-ed25519@openssh.com AAAAGnNrLXNzaC1lZDI1NTE5QG9wZW5zc2guY29tAAAAIA9B+2GGCkQvBzpa4WobQhfC9YU5Su3pKXBmVLSXEqfRAAAAEHNzaDp5b3VybmFtZWhlcmU= alice@hostname
使用“无需触摸”密钥时,尽管恶意行为者即使拥有 YubiKey 所插入主机的 root 权限也无法窃取密钥,但拥有访问权限的恶意行为者可以在无需任何存在检测或人工交互的情况下使用该密钥。因此,在执行此操作之前,请务必确保这确实符合您的风险承受能力。要将“无需触摸”密钥用于远程服务器的无人值守身份验证,您需要在目标主机的 authorized_keys 文件中的条目前加上 no-touch-required,否则 OpenSSH 的默认安全设置将拒绝该密钥。例如:$ cat ~/.ssh/authorized_keys no-touch-required sk-ssh-ed25519@openssh.com AAAAGnNrLXNzaC1lZDI1NTE5QG9wZW5zc2guY29tAAAAIA9B+2GGCkQvBzpa4WobQhfC9YU5Su3pKXBmVLSXEqfRAAAAEHNzaDp5b3VybmFtZWhlcmU= alice@hostname
Method 2: TPM YubiKeys are nice, though fairly expensive. It would be useful if we could use the TPM we already have on our host to protect key material instead. Well, it turns out we can! Unfortunately this does require additional software, but it’s a fairly minimal Golang package – ssh-tpm-agent. It can be installed with Go directly from GitHub, but is natively packaged by Ubuntu and I’m working on packaging it for Fedora. To create a TPM-backed SSH key with a PIN, run: ssh-tpm-keygen
方法 2:TPM。YubiKey 很好,但相当昂贵。如果我们能利用主机上现有的 TPM 来保护密钥材料,那将非常有用。事实证明我们可以!遗憾的是,这确实需要额外的软件,但它是一个相当精简的 Golang 包——ssh-tpm-agent。它可以直接通过 Go 从 GitHub 安装,Ubuntu 已经原生打包了它,我正在努力为 Fedora 进行打包。要创建带有 PIN 的 TPM 支持的 SSH 密钥,请运行:ssh-tpm-keygen