如何验证/检查/测试/验证我的 SSH 密码?

发布于 2024-10-07 08:51:25 字数 47 浏览 5 评论 0原文

我想我忘记了 SSH 密钥的密码,但我有预感它可能是什么。我如何检查我是否正确?

I think I forgot the passphrase for my SSH key, but I have a hunch what it might be. How do I check if I'm right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

何处潇湘 2024-10-14 08:51:25

ssh-keygen -y

ssh-keygen -y 将提示您输入密码(如果有)。

   如果您输入正确密码,它将显示关联的公钥。
   如果您输入错误密码,将会显示加载失败
   如果密钥没有密码,它将不会提示您输入密码,并会立即向您显示关联的公钥。

请注意,参数的顺序很重要。 -y 必须出现在-f input_keyfile之前,否则您将收到错误Too mucharguments.


例如,

创建一个新的公钥/私钥对,带或不带密码:

$ ssh-keygen -f /tmp/my_key
...

现在看看您是否可以访问该密钥对:

$ ssh-keygen -y -f /tmp/my_key

Following are extended examples, with example output.


使用密码的行为:

首先,创建一个新的公钥/私钥对,带有密码:

$ ssh-keygen -f /tmp/my_key_with_passphrase
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /tmp/my_key_with_passphrase.
Your public key has been saved in /tmp/my_key_with_passphrase.pub.
The key fingerprint is:
de:24:1b:64:06:43:ca:76:ba:81:e5:f2:59:3b:81:fe [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|     .+          |
|   . . o         |
|    = . +        |
|   = + +         |
|  o = o S .      |
|   + = + *       |
|    = o o .      |
|     . .         |
|      E          |
+-----------------+

现在尝试通过输入正确的密码来访问密钥对。
请注意,将显示公钥,退出状态 ($?) 将为 0 表示成功:

$ ssh-keygen -y -f /tmp/my_key_with_passphrase
Enter passphrase:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBJhVYDYxXOvcQw0iJTPY64anbwSyzI58hht6xCGJ2gzGUJDIsr1NDQsclka6s0J9TNhUEBBzKvh9nTAYibXwwhIqBwJ6UwWIfA3HY13WS161CUpuKv2A/PrfK0wLFBDBlwP6WjwJNfi4NwxA21GUS/Vcm/SuMwaFid9bM2Ap4wZIahx2fxyJhmHugGUFF9qYI4yRJchaVj7TxEmquCXgVf4RVWnOSs9/MTH8YvH+wHP4WmUzsDI+uaF1SpCyQ1DpazzPWAQPgZv9R8ihOrItLXC1W6TPJkt1CLr/YFpz6vapdola8cRw6g/jTYms00Yxf2hn0/o8ORpQ9qBpcAjJN
$ echo $?
0

现在尝试通过输入 来访问密钥对密码错误
请注意,将显示 load failed 错误消息(消息可能因操作系统而异),并且退出状态 ($?) 将为 1指示错误:

$ ssh-keygen -y -f /tmp/my_key_with_passphrase
Enter passphrase:
load failed
$ echo $?
1

没有密码的行为:
首先,创建一个新的公钥/私钥对,没有密码:

$ ssh-keygen -f /tmp/my_key_with_no_passphrase
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /tmp/my_key_with_no_passphrase
Your public key has been saved in /tmp/my_key_with_no_passphrase.pub
The key fingerprint is:
SHA256:wx2N90FB68dUM9DnFXkUm/2RoiWV0hjF3N29D5TBeDo [email protected]
The key's randomart image is:
+---[RSA 3072]----+
|           .BBXXX|
|           =o*==/|
|          o.+*o*B|
|       . . oEoo=+|
|        S .. .o.=|
|         .     ..|
|                 |
|                 |
|                 |
+----[SHA256]-----+

现在尝试访问该密钥对。请注意,没有提示输入密码,将显示公钥,退出状态 ($?) 将为 0 表示成功:

$ ssh-keygen -y -f /tmp/my_key_with_no_passphrase
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDHr9DfECzng5+cSq2ZnuGMHU2c5j106bJlPt4a4SFWagQJJD9FyeC53sfzprTyuUnM7DYZ8y/QZ6xqI6aWqD7FGyrsvUktPoFShz7CyXZM85SriLJeV2ORRlUeszjK0hDk942nnKcu7SDugaVCQHmuPk9HrE/z4ikaLcD3N/MUNAwZGtyGI5yHQN9o3H064c6gESu/WyjaF2WNWcD8OoqiLXMj2gAWSNVCu43nFsdaW2UmVxiW8oI8ZoPKd5gndyPFOSQk4atYxyO4fhDoMqlwf4dPYNR3Gi6Bfc/8sHPRUiJGSSPSUOSlP/GPjFMY/J8lRex7745eGn3Tm98gtyeto1wTgom5O0H46LxjA3IEeeD4Nn4sQw/EF3lVr1RR7N8uTK4lrHUPKOFa26BpbIoNi6Omhmjgj5GoerMUIB+7aG7mUsQgxy6cTAUefIxVF6RG9GquS/NozC7KSJKfgKq73BROSJIf/9XMQhyK7LFnFFwzHOy6IxXPIFsPef1k69U= [email protected]
$ echo $?
0

ssh-keygen -y

ssh-keygen -y will prompt you for the passphrase (if there is one).

    If you input the correct passphrase, it will show you the associated public key.
    If you input the wrong passphrase, it will display load failed.
    If the key has no passphrase, it will not prompt you for a passphrase and will immediately show you the associated public key.

Note that the order of arguments is important. -y must come before -f input_keyfile, else you will get the error Too many arguments.


e.g.,

Create a new public/private key pair, with or without a passphrase:

$ ssh-keygen -f /tmp/my_key
...

Now see if you can access the key pair:

$ ssh-keygen -y -f /tmp/my_key

Following are extended examples, with example output.


Behavior with a passphrase:

First, create a new public/private key pair, with a passphrase:

$ ssh-keygen -f /tmp/my_key_with_passphrase
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /tmp/my_key_with_passphrase.
Your public key has been saved in /tmp/my_key_with_passphrase.pub.
The key fingerprint is:
de:24:1b:64:06:43:ca:76:ba:81:e5:f2:59:3b:81:fe [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|     .+          |
|   . . o         |
|    = . +        |
|   = + +         |
|  o = o S .      |
|   + = + *       |
|    = o o .      |
|     . .         |
|      E          |
+-----------------+

Now attempt to access the key pair by inputting the correct passphrase.
Note that the public key will be shown and the exit status ($?) will be 0 to indicate success:

$ ssh-keygen -y -f /tmp/my_key_with_passphrase
Enter passphrase:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBJhVYDYxXOvcQw0iJTPY64anbwSyzI58hht6xCGJ2gzGUJDIsr1NDQsclka6s0J9TNhUEBBzKvh9nTAYibXwwhIqBwJ6UwWIfA3HY13WS161CUpuKv2A/PrfK0wLFBDBlwP6WjwJNfi4NwxA21GUS/Vcm/SuMwaFid9bM2Ap4wZIahx2fxyJhmHugGUFF9qYI4yRJchaVj7TxEmquCXgVf4RVWnOSs9/MTH8YvH+wHP4WmUzsDI+uaF1SpCyQ1DpazzPWAQPgZv9R8ihOrItLXC1W6TPJkt1CLr/YFpz6vapdola8cRw6g/jTYms00Yxf2hn0/o8ORpQ9qBpcAjJN
$ echo $?
0

Now attempt to access the key pair by inputting an incorrect passphrase.
Note that the load failed error message will be displayed (message may differ depending on OS) and the exit status ($?) will be 1 to indicate an error:

$ ssh-keygen -y -f /tmp/my_key_with_passphrase
Enter passphrase:
load failed
$ echo $?
1

Behavior with no passphrase:
First, create a new public/private key pair, without a passphrase:

$ ssh-keygen -f /tmp/my_key_with_no_passphrase
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /tmp/my_key_with_no_passphrase
Your public key has been saved in /tmp/my_key_with_no_passphrase.pub
The key fingerprint is:
SHA256:wx2N90FB68dUM9DnFXkUm/2RoiWV0hjF3N29D5TBeDo [email protected]
The key's randomart image is:
+---[RSA 3072]----+
|           .BBXXX|
|           =o*==/|
|          o.+*o*B|
|       . . oEoo=+|
|        S .. .o.=|
|         .     ..|
|                 |
|                 |
|                 |
+----[SHA256]-----+

Now attempt to access that key pair. Note that there is no prompt for the passphrase, the public key will be displayed, and the exit status ($?) will be 0 to indicate success:

$ ssh-keygen -y -f /tmp/my_key_with_no_passphrase
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDHr9DfECzng5+cSq2ZnuGMHU2c5j106bJlPt4a4SFWagQJJD9FyeC53sfzprTyuUnM7DYZ8y/QZ6xqI6aWqD7FGyrsvUktPoFShz7CyXZM85SriLJeV2ORRlUeszjK0hDk942nnKcu7SDugaVCQHmuPk9HrE/z4ikaLcD3N/MUNAwZGtyGI5yHQN9o3H064c6gESu/WyjaF2WNWcD8OoqiLXMj2gAWSNVCu43nFsdaW2UmVxiW8oI8ZoPKd5gndyPFOSQk4atYxyO4fhDoMqlwf4dPYNR3Gi6Bfc/8sHPRUiJGSSPSUOSlP/GPjFMY/J8lRex7745eGn3Tm98gtyeto1wTgom5O0H46LxjA3IEeeD4Nn4sQw/EF3lVr1RR7N8uTK4lrHUPKOFa26BpbIoNi6Omhmjgj5GoerMUIB+7aG7mUsQgxy6cTAUefIxVF6RG9GquS/NozC7KSJKfgKq73BROSJIf/9XMQhyK7LFnFFwzHOy6IxXPIFsPef1k69U= [email protected]
$ echo $?
0
沧桑㈠ 2024-10-14 08:51:25

您可以通过尝试将 SSH 密钥密码加载到 SSH 代理来验证它。对于 OpenSSH,这是通过 ssh-add 完成的。

完成后,请记住通过运行 ssh-add -d 从终端卸载 SSH 密码。

You can verify your SSH key passphrase by attempting to load it into your SSH agent. With OpenSSH this is done via ssh-add.

Once you're done, remember to unload your SSH passphrase from the terminal by running ssh-add -d.

指尖上得阳光 2024-10-14 08:51:25

到目前为止,我在网上找到的最佳答案是:

ssh-keygen -y -P "" -f ~/.ssh/id_rsa

这将尝试使用密码作为空字符串读取私钥。如果您的密钥没有密码短语,那么这将完美地显示公钥。

否则应用程序将退出并显示错误代码和消息:

Load key "~/.ssh/id_rsa": incorrect passphrase supplied to decrypt private key

The best answer so far, that I found on the web is:

ssh-keygen -y -P "" -f ~/.ssh/id_rsa

That will attempt to read the private key with the passphrase as empty string. That will show a public key perfectly fine if you have a key with no passphrase.

Otherwise the app will exit with an error code and the message:

Load key "~/.ssh/id_rsa": incorrect passphrase supplied to decrypt private key
行雁书 2024-10-14 08:51:25

将 @RobBednark 的解决方案扩展到特定的 Windows + PuTTY 场景,您可以这样做:

  1. 使用 PuTTYgen 生成 SSH 密钥对(遵循在 Windows 中手动生成 SSH 密钥< /em>),将其保存到 PPK 文件;

  2. 通过 Windows 资源管理器中的上下文菜单,选择“使用 PuTTYgen 进行编辑”。它将提示输入密码。

如果您输入错误的密码,它只会再次提示。

请注意,如果您想键入,请在包含 PPK 文件的文件夹上使用以下命令:puttygen private-key.ppk -y

Extending @RobBednark's solution to a specific Windows + PuTTY scenario, you can do so:

  1. Generate SSH key pair with PuTTYgen (following Manually generating your SSH key in Windows), saving it to a PPK file;

  2. With the context menu in Windows Explorer, choose Edit with PuTTYgen. It will prompt for a password.

If you type the wrong password, it will just prompt again.

Note, if you like to type, use the following command on a folder that contains the PPK file: puttygen private-key.ppk -y.

情栀口红 2024-10-14 08:51:25

使用“ssh-keygen -p”。您可以添加“-f”,

它会提示您输入旧密码。如果密码正确,会提示输入新密码。如果旧密码不正确,您将收到“无法加载密钥<...>”。

Use "ssh-keygen -p". You can add "-f "

It will prompt you for the old password. If the password is correct, it will prompt to enter a new password. If the old password is incorrect, you will get "Failed to load key <...>".

绅士风度i 2024-10-14 08:51:25

如果您的密码是解锁 SSH 密钥,并且您没有 ssh-agent,但计算机上安装了 sshd(SSH 守护进程),请执行以下操作:

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys;
ssh localhost -i ~/.ssh/id_rsa

其中 ~/.ssh /id_rsa.pub 是公钥,~/.ssh/id_rsa 是私钥。

If your passphrase is to unlock your SSH key and you don't have ssh-agent, but do have sshd (the SSH daemon) installed on your machine, do:

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys;
ssh localhost -i ~/.ssh/id_rsa

Where ~/.ssh/id_rsa.pub is the public key, and ~/.ssh/id_rsa is the private key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文