OpenSSH (ssh-keygen, ssh-add, ssh)SSH
错误信息
Load key "/path/to/key": incorrect passphrase supplied to decrypt private key
也可能输出为
- incorrect passphrase supplied to decrypt private key
- Bad passphrase, try again for /path/to/key:
交互式的 `ssh-add` 会用“Bad passphrase, try again for …:”再次询问;`ssh` 本身只是再次提示输入。
OpenSSH (ssh-keygen, ssh-add, ssh) — encrypted private key
含义
密钥文件解析正常,而且是加密的;用你提供的密码短语解密失败。OpenSSH 能把这种情况与文件损坏区分开,因为加密容器中带有校验值——所以这条消息确实意味着“密码短语错误”,对 OpenSSH 格式的密钥和加密的 PEM/PKCS#8 密钥都是如此。忘记的密码短语无法找回,只能更换密钥。
原因
这不是该密钥设置的密码短语
常见多个密钥各有不同的密码短语,或者密钥的密码短语已在另一台机器上用 `ssh-keygen -p` 修改过。
来自文件或变量的多余字符
偶见从文件或机密中读取的密码短语带有末尾换行符或引号,再通过 `-P` 或 `SSH_ASKPASS` 脚本传入。
键盘布局或大写锁定
偶见密码短语中如果含有在不同布局间位置会变的字符(y/z、特殊字符),在使用另一种布局的机器上就会失败。
修复方法
- 1.
以交互方式测试密码短语
如果 `ssh-keygen -y` 接受你手动输入的密码短语,问题就出在脚本传入密码短语的方式上。
bash ssh-keygen -y -f ~/.ssh/id_ed25519 > /dev/null # prompts; succeeds only with the right passphrase ssh-keygen -p -f ~/.ssh/id_ed25519 # change the passphrase (needs the old one) - 2.
用 SSH 代理代替传递密码短语
在 `ssh-agent`(或操作系统钥匙串)中解锁一次密钥,而不是把密码短语写进脚本或 CI。
bash # Unlock once per session instead of typing or storing the passphrase in scripts eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_ed25519 # macOS: keep it in the keychain ssh-add --apple-use-keychain ~/.ssh/id_ed25519