换机器最烦的不是装系统,是 ~/.ssh 要再拷一遍。私钥散落在 U 盘、网盘、旧电脑里,权限一不对 ssh 直接不理你。
后来把钥匙放进 KeePass 库,主机信息也写在同一份 kdbx 里。库跟着网盘走,Windows、Debian、macOS 各自用客户端把钥匙交给 ssh,不必再复制私钥文件。
生成钥匙本身还是老办法,见 SSH Key 生成、配置与连接。这篇只讲:库怎么记,三个系统怎么接到 ssh config。
客户端约定:
| 系统 | 客户端 | 钥匙怎么交给 ssh |
|---|---|---|
| Debian / Ubuntu | KeePassXC | 注入系统 ssh-agent |
| Windows | KeePassXC | 注入 Windows OpenSSH Agent(或 Pageant) |
| macOS | Strongbox(Pro) | Strongbox 自己当 agent |
同一份 kdbx。KeePassXC 和 Strongbox 都认 KeeAgent 那套约定。
库里怎么记
两台机经常共用一把钥匙。所以不要「一台主机一条,私钥复制两份」。
| |
钥匙条目 /SSH/Keys/prod-ed25519:
- Username / Password / URL 都空(钥匙没有 passphrase 就空着;有的话 Password 填 passphrase)
- 附件:
id_ed25519(私钥,OpenSSH 格式)、id_ed25519.pub(公钥) - 自定义字段
KeyType=ed25519 - KeePassXC 里打开条目 → SSH Agent:用附件
id_ed25519,勾选解锁加入 agent、锁定撤掉
私钥必须是:
| |
不是老的 BEGIN RSA PRIVATE KEY。要转的话:
| |
这会就地改文件。
主机条目 /SSH/Hosts/vps-a:
| 字段 | 示例 |
|---|---|
| Title | vps-a |
| Username | root |
| Password | 空(走 key) |
| URL | ssh://root@203.0.113.10:22 |
HostName | 203.0.113.10 |
Port | 22 |
IdentityRef | SSH/Keys/prod-ed25519 |
vps-b 同理,换 IP。两条 host 没有私钥。只导出主机条目连不上。
下面例子里的 IP、别名都是占位,换成你自己的。
Debian / Ubuntu:KeePassXC
| |
CLI 也在这个包里:keepassxc-cli。
- 打开库。
- Tools → Settings → SSH Agent,打开集成。
- 当前 shell 要有
SSH_AUTH_SOCK。GNOME 桌面一般自带;WSL、纯 ssh 登录经常没有:
| |
- 打开
/SSH/Keys/prod-ed25519,SSH Agent 选项勾上。解锁后:
| |
应能看到这把 ed25519。然后:
| |
锁库,钥匙从 agent 里撤掉。
加不进去时,看 SSH_AUTH_SOCK 是不是指向真正的 OpenSSH agent。GNOME Keyring 那套有时不认 OpenSSH 私钥,自己起 ssh-agent 更干净。
无桌面、只想落盘:
| |
权限不对,Linux 上 ssh 会直接忽略钥匙。目录 700,私钥 600,config 也建议 600。
Windows:KeePassXC + OpenSSH
Win10 / Win11 自带 OpenSSH 客户端。KeePassXC 把钥匙交给 OpenSSH Authentication Agent,PowerShell、Windows Terminal 里的 ssh 就能用。
服务默认是停的,先打开(管理员 PowerShell):
| |
KeePassXC:
- Tools → Settings → SSH Agent,启用,选 OpenSSH(用 PuTTY 再选 Pageant)。
- 钥匙条目同样勾选解锁加入 agent。
ssh-add -l能列出钥匙。
config 在 %USERPROFILE%\.ssh\config,也就是 C:\Users\<你>\.ssh\config。写法跟 Linux 一样,路径用正斜杠也行:
| |
走 agent 时可以不写 IdentityFile。钥匙多、服务器只让试几把时,再指定公钥(见 macOS 那节)。
WSL 是另一套环境。 Windows 上的 KeePassXC 不会自动把钥匙注入 Debian 子系统的 agent。两边选一条:
- 终端直接用 Windows 的
ssh,跟上面这节 - 进 WSL 之后按 Debian 那节:子系统里装 KeePassXC /
keepassxc-cli,或把附件导出到 WSL 的~/.ssh
不要指望 Windows 解锁库,WSL 里的 ssh 就会自动有钥匙。
macOS:Strongbox
官方:Use Strongbox As Your SSH Agent。
限制:Pro、仅 macOS、只要 kdbx。iPhone 能看、能导出,不能当 agent。
Strongbox 不把钥匙交给系统 ssh-agent,自己监听一个 socket,ssh / git 去问它。每次用钥匙会弹确认。私钥不出 Strongbox。
- 打开同一份库。侧栏 SSH Keys 里应有
prod-ed25519。没有就打开条目,打开 Enabled for SSH Agent。 - Strongbox → Settings → SSH Agent,启用。
- socket 默认很长,做一次短路径:
| |
~/.ssh/config顶部指定 agent。走 Strongbox 时,IdentityFile指向 公钥(告诉 ssh 用哪把),不是私钥:
| |
公钥从钥匙条目详情页复制出来即可,可以落盘。私钥不要再导出到 ~/.ssh。
库保持解锁,ssh vps-a。弹出批准就点允许。锁库 = 停用。
终端里 Git 提交签名不认 IdentityAgent,只认环境变量。需要的话写进 ~/.zshrc:
| |
GUI 的 Git 客户端更麻烦,官方文档有 LaunchAgent 写法,按需再做。
ssh config 怎么对齐库
库是源,config 是各机本地的索引。Host 别名跟条目 Title 一致最好记。
| |
- KeePassXC(Win / Linux):钥匙在 agent 里,通常不用
IdentityFile;要指定就指向 私钥 路径,或只依赖 agent。 - Strongbox:
IdentityAgent指向 Strongbox socket,IdentityFile指向 公钥。
IdentitiesOnly yes 建议都加上。ssh 默认把 agent 里的钥匙挨个试,服务器大约试 6 把就踢人。
然后:
| |
不要搞混
- 主机条目是备忘,签名用的是钥匙条目。
- 公钥可以到处放;私钥只放 kdbx 附件,或某台机
chmod 600的导出文件。 - 测试库不要用弱主密码装着真实 SSH 私钥去同步。
- Windows 的 agent 和 WSL 的 agent 不是同一个。
- Strongbox 的 SSH Agent 是付费功能,而且只在 Mac 上当 agent。
参考: