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

在我的家庭networking上,我有一台运行Ubuntu的服务器和另一台运行Windows 7的桌面。我在Windows机器上使用cygwinssh放入服务器。 我试图在这两台机器之间设置密钥。 在Windows机器上,我使用ssh-keygen -t rsa创build密钥。 然后,我将所得到的id_rsa.pub scp到服务器,并将其放置在~/.ssh/authorized_keys文件中。 据我所知,应该是所有我需要做这个工作。 但是,正如你可能猜到的那样,事实并非如此。

我正在尝试使用[email protected] ssh。 我的猜测(我没能得到很好的信息)是我可能需要设置一些有点不同,使用cygwin或它可能有一些与使用IP地址,而不是主机名。任何方向或想法?

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

你的〜/ .ssh目录应该被修改为700,你的authorized_keys文件至less应该被修改为644。 如果你检查你的/ var / log / secure日志文件,它应该给你一些提示,说明它失败的原因。

如果你对ssh有authentication问题,首先要调查的是客户说的一些debugging:

 % 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). ... 

是一个成功的公钥连接。

 % 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: ... 

这并没有提供很多关于为什么公钥authentication被拒绝的信息。 最好的信息在服务器上可用。 Sshd将login到AUTH系统日志工具,所以你可以find任何logging到的信息(在Debian /var/log/auth/ )。

 Aug 19 08:18:36 ace sshd[10100]: Authentication refused: bad ownership or modes for directory /home/david/.ssh 

这告诉我们.ssh的权限是错误的,我们可以很容易地修复它。

 Aug 19 08:26:41 ace sshd[12156]: error: key_read: uudecode ZAAAAB3NzaC1kc3MAAACBAIUuAmpj9FuE71EfqJDVAfI+pUZ++xSWbUvEh7U36WW/... 

这告诉我们,它没有阅读特定的关键,所以我们知道要解决这个问题。

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

  LogLevel DEBUG 

然后运行

  /etc/init.d/ssh reload 

现在,当你尝试连接你应该看到一些日志,如:

 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 

这是一个成功的login。

如果您需要更多信息,可以使用DEBUG2和DEBUG3来获得更多信息。 不要忘了在修复问题时再次更改日志级别(可能是INFO)。

这个权限对于〜/ .ssh / authorized_keys文件是很重要的。 该文件本身不能有任何其他人写入的权限chmod go-rwx ~/.ssh/authorized_keys另外,.ssh目录不允许写入chmod go-rwx ~/.ssh你的主目录也应该不允许写入chmod go-w ~/

原因是,如果其他人能够写信给你的目录(通过组权限或其他许可),那么在你不知情的情况下,很容易有人在那里塞进授权密钥。 如果您的主目录允许写入,则可以重新创build.ssh目录及其内容。

一些发行版提供了一个名为ssh-copy-id的工具(这实际上是一个shell脚本)。 我发现使用这种方法可以解决许多问题,将用户密钥的.pub部分复制到远程帐户的authorized_keys文件中。

用法

 $ ssh-copy-id user@remotehost 

你甚至可以告诉它不同的键复制(它默认为id_rsa*键。

 $ ssh-copy-id -i ~/.ssh/somekey.pub user@remotehost 

该脚本还负责在远程主机上创build用户的$HOME/.ssh目录, 正确设置权限。

 $ ssh-copy-id --help Usage: /usr/bin/ssh-copy-id [-i [identity_file]] [user@]machine 

它有一个基本的手册页。 在我的基于Red Hat的发行版(Fedora,CentOS,RHEL)上,它是openssh-client包的一部分!