为什么我的公钥不允许我在没有密码的情况下登录?

Hec*_*out 5 ssh cygwin keys

在我的家庭网络上,我有一台运行 Ubuntu 的服务器和另一台运行 Windows 7 的桌面。我在 Windows 机器上使用 cygwin ssh 进入服务器。我试图在这两台机器之间设置密钥。在 Windows 机器上,我使用ssh-keygen -t rsa. 然后我将生成的 id_rsa.pub 'scp' 到服务器并将它放在~/.ssh/authorized_keys文件中。据我所知,这应该是我完成这项工作所需的全部内容。但是,正如您可能已经猜到的那样,事实并非如此。

我正在尝试使用 ssh username@192.168.1.101。我的猜测(我无法获得很好的信息)是我可能需要设置一些不同的东西才能使用 cygwin 或者它可能与使用 IP 地址而不是主机名有关。 . 任何方向或想法?

vmf*_*rms 9

这可能是您的 ~/.ssh 目录或您的 ~/.ssh/authorized_keys 文件的权限问题。

您的 ~/.ssh 目录应该被修改为 700,并且您的 authorized_keys 文件至少应该被修改为 644。如果你检查你的 /var/log/secure 日志文件,它应该会给你一些关于它失败原因的提示。


Dav*_*ley 8

如果您遇到 ssh 身份验证问题,首先要调查客户端通过一些调试所说的内容:

% ssh -v host
...
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering public key: /home/david/.ssh/id_dsa
debug1: Server accepts key: pkalg ssh-dss blen 433
debug1: Enabling compression at level 6.
debug1: Authentication succeeded (publickey).
...
Run Code Online (Sandbox Code Playgroud)

是成功的公钥连接。

% ssh -v host
...
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering public key: /home/david/.ssh/id_dsa
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /home/david/.ssh/identity
debug1: Trying private key: /home/david/.ssh/id_rsa
debug1: Next authentication method: password
david@ace's password: 
...
Run Code Online (Sandbox Code Playgroud)

这并没有真正提供太多关于为什么公钥身份验证被拒绝的信息。最好的信息可以在服务器上获得。sshd 将记录到 AUTH syslog 工具,因此您可以在记录到的任何位置找到信息(对于 Debian 而言/var/log/auth/)。

Aug 19 08:18:36 ace sshd[10100]: Authentication refused: bad ownership or modes 
       for directory /home/david/.ssh
Run Code Online (Sandbox Code Playgroud)

这告诉我们 .ssh 的权限是错误的,我们可以轻松修复它。

Aug 19 08:26:41 ace sshd[12156]: error: key_read: uudecode
       ZAAAAB3NzaC1kc3MAAACBAIUuAmpj9FuE71EfqJDVAfI+pUZ++xSWbUvEh7U36WW/...
Run Code Online (Sandbox Code Playgroud)

这告诉我们它无法读取该特定密钥,因此我们知道要修复该问题。

如果您没有从日志中获得任何有用的信息,您可以打开日志记录。编辑/etc/ssh/sshd_config该行并将其更改LogLevel为:

 LogLevel DEBUG
Run Code Online (Sandbox Code Playgroud)

然后运行

 /etc/init.d/ssh reload
Run Code Online (Sandbox Code Playgroud)

现在,当您尝试连接时,您应该会看到一些日志,例如:

Aug 19 08:32:12 ace sshd[13537]: debug1: Checking blacklist file 
      /usr/share/ssh/blacklist.DSA-1024
Aug 19 08:32:12 ace sshd[13537]: debug1: Checking blacklist file 
      /etc/ssh/blacklist.DSA-1024
Aug 19 08:32:12 ace sshd[13537]: debug1: temporarily_use_uid: 1002/513 (e=0/0)
Aug 19 08:32:12 ace sshd[13537]: debug1: trying public key file 
      /home/david/.ssh/authorized_keys
Aug 19 08:32:12 ace sshd[13537]: debug1: fd 4 clearing O_NONBLOCK
Aug 19 08:32:12 ace sshd[13537]: debug1: matching key found: file 
      /home/david/.ssh/authorized_keys, line 1
Aug 19 08:32:12 ace sshd[13537]: Found matching DSA key: 
      1c:46:89:52:c1:79:c8:8f:43:3c:4e:77:ad:a1:5d:1b
Run Code Online (Sandbox Code Playgroud)

这是登录成功。

如果需要更多信息,可以使用DEBUG2和DEBUG3来获取更多信息。解决问题后,不要忘记再次更改日志级别(可能更改为 INFO)。