如何更改我的私钥密码?

kch*_*kch 364 unix ssh passphrase ssh-keygen

我有一个现有的公钥/私钥对。私钥受密码保护,加密可以是 RSA 或 DSA。这些密钥是您生成ssh-keygen并通常存储在~/.ssh.

我想更改私钥的密码。在标准的 Unix shell 上,我该怎么做?

另外,我如何简单地删除密码?直接改成空?

Mik*_*zur 478

要更改默认密钥的密码:

$ ssh-keygen -p
Run Code Online (Sandbox Code Playgroud)

如果需要指定键,请传递-f选项:

$ ssh-keygen -p -f ~/.ssh/id_dsa
Run Code Online (Sandbox Code Playgroud)

然后根据提示提供您的旧密码和新密码(两次)。(~/.ssh/id_rsa如果您有 RSA 密钥,请使用。)

更多详情来自man ssh-keygen

[...]
SYNOPSIS
    ssh-keygen [-q] [-b bits] -t type [-N new_passphrase] [-C comment]
               [-f output_keyfile]
    ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile]
[...]
     -f filename
             Specifies the filename of the key file.
[...]
     -N new_passphrase
             Provides the new passphrase.

     -P passphrase
             Provides the (old) passphrase.

     -p      Requests changing the passphrase of a private key file instead of
             creating a new private key.  The program will prompt for the file
             containing the private key, for the old passphrase, and twice for
             the new passphrase.
[...]
Run Code Online (Sandbox Code Playgroud)

  • 另外,为了以后懒惰的人,我会颠倒顺序:先快速回答,后手册页。 (17认同)
  • 如果您的机器使用 OpenSSH >= 6.5,[您应该使用 `-o` 选项](http://www.tedunangst.com/flak/post/new-openssh-key-format-and-bcrypt-pbkdf)启用新的私钥格式(默认情况下 bcrypt 为 KDF)。对于较旧的 OpenSSH 版本,[使用 PKCS#8 获得更安全的私钥文件](http://blog.patshead.com/2013/09/generate-new-more-secure-ssh-keys.html)。 (6认同)
  • 我认为答案很好,因为它同时显示了您可以在哪里更改密码以及在哪里寻找答案。我帮助过很多人设置了 ssh 密钥,对他们来说,要真正记住他们使用过的工具并不总是那么容易。此外,在'网上搜索答案是许多人的第一选择...... (2认同)
  • @FranciscoLuz 我的答案中的命令特定于 DSA 密钥。如果您有 RSA 密钥,那么您的命令是正确的。我在答案中添加了一个简介来解决这个问题。 (2认同)