当前位置: 首页>>技术教程>>正文


无密码ssh无法正常工作

, , ,

问题描述

我试图将password-less ssh b /w A设置为B,将B设置为A。在两台计算机上使用ssh-keygen -trsa生成公钥和私钥。使用ssh-copy-id实用程序将public-keys从A复制到B以及将B复制到A

无密码ssh从AB,但notBA。我已经检查了〜/ssh /文件夹的权限,看起来很正常。

A's .ssh文件夹权限:

-rw-------  1 root root 13530 2011-07-26 23:00 known_hosts
-rw-------  1 root root   403 2011-07-27 00:35 id_rsa.pub
-rw-------  1 root root  1675 2011-07-27 00:35 id_rsa
-rw-------  1 root root   799 2011-07-27 00:37 authorized_keys
drwxrwx--- 70 root root  4096 2011-07-27 00:37 ..
drwx------  2 root root  4096 2011-07-27 00:38 .

B's .ssh文件夹权限:

-rw------- 1 root root  884 2011-07-07 13:15 known_hosts
-rw-r--r-- 1 root root  396 2011-07-27 00:15 id_rsa.pub
-rw------- 1 root root 1675 2011-07-27 00:15 id_rsa
-rw------- 1 root root 2545 2011-07-27 00:36 authorized_keys
drwxr-xr-x 8 root root 4096 2011-07-06 19:44 ..
drwx------ 2 root root 4096 2011-07-27 00:15 .

A是一个ubuntu 10.04(OpenSSH_5.3p1 Debian-3ubuntu4,OpenSSL 0.9.8k 2009年3月25日)B是一台debian机器(OpenSSH_5.1p1 Debian-5,OpenSSL 0.9.8g 2007年10月19日)

来自A

#ssh B

工作良好。

来自B

#ssh -vvv A 
...
...
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug2: key: /root/.ssh/identity ((nil))
debug2: key: /root/.ssh/id_rsa (0x7f1581f23a50)
debug2: key: /root/.ssh/id_dsa ((nil))
debug3: Wrote 64 bytes for a total of 1127
debug1: Authentications that can continue: publickey,password
debug3: start over, passed a different list publickey,password
debug3: preferred gssapi-keyex,gssapi-with-mic,gssapi,publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/identity
debug3: no such identity: /root/.ssh/identity
debug1: Offering public key: /root/.ssh/id_rsa
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug3: Wrote 368 bytes for a total of 1495
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /root/.ssh/id_dsa
debug3: no such identity: /root/.ssh/id_dsa
debug2: we did not send a packet, disable method
debug3: authmethod_lookup password
debug3: remaining preferred: ,password
debug3: authmethod_is_enabled password
debug1: Next authentication method: password
root@192.168.122.1's password: 

这实际上意味着它不使用文件/root/id_rsa进行身份验证。我也在两台机器上运行了ssh-add命令。

/etc/ssh/sshd_config文件的身份验证部分是

# Authentication:
LoginGraceTime 120
PermitRootLogin yes
StrictModes yes

RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile     %h/.ssh/authorized_keys

# Don't read the user's ~/.rhosts and ~/.shosts files

我的想法已经不多了。任何帮助,将不胜感激。

最佳解决方案

只需确保您已按照以下步骤操作:

在机器A上

打开终端并输入如下命令:

root@aneesh-pc:~# id

只是为了确保我们是根。

如果上面的命令输出如下所示我们是root,则使用su命令切换到root

uid=0(root) gid=0(root) groups=0(root)

1)创建密钥。

ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
49:7d:30:7d:67:db:58:51:42:75:78:9c:06:e1:0c:8d root@aneesh-pc
The key's randomart image is:
+--[ RSA 2048]----+
|          ooo+==B|
|         . E=.o+B|
|        . . .+.*o|
|       . . .  ...|
|        S        |
|                 |
|                 |
|                 |
|                 |
+-----------------+

我没有使用过任何密码。如果您需要,可以使用它。

2)将公钥复制到机器B的.ssh/authorized_keys文件中

root@aneesh-pc:~# ssh-copy-id -i /root/.ssh/id_rsa.pub root@mylap
root@mylap's password: 

现在尝试使用ssh 'root@mylap'登录计算机,并检入:

~/.ssh/authorized_keys

确保我们没有添加您不期望的额外密钥。

将mylap替换为您要登录的机器的主机名或IP(即机器B)

3)无密码登录B.

root@aneesh-pc:~# ssh root@mylap
Warning: Permanently added 'mylap,192.168.1.200' (RSA) to the list of known hosts.
Welcome to Ubuntu 11.04 (GNU/Linux 2.6.38-8-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

Last login: Wed Jul 27 15:23:58 2011 from streaming-desktop.local
aneesh@mylap:~$

在机器B上

4)创建登录到机器A的密钥

root@mylap:~# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
35:9f:e7:81:ed:02:f9:fd:ad:ef:08:c6:4e:19:76:b1 root@streaming-desktop
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|          o   .  |
|         . + + o |
|        S o * E  |
|           = O . |
|            O +  |
|           + o o.|
|            . o+=|
+-----------------+

5)将公钥复制到机器A的.ssh/authorized_keys文件中

root@mylap:~# ssh-copy-id -i /root/.ssh/id_rsa.pub root@aneesh-pc
Warning: Permanently added 'aneesh-pc,192.168.1.20' (RSA) to the list of known hosts.
root@aneesh-pc's password: 

现在尝试使用ssh 'root@aneesh-pc'登录计算机,并检入:

.ssh/authorized_keys

确保我们没有添加您不期望的额外密钥。

6)无密码登录A.

ssh root@aneesh-pc
Warning: Permanently added 'aneesh-pc,192.168.1.20' (RSA) to the list of known hosts.
Welcome to Ubuntu 11.04 (GNU/Linux 2.6.38-8-generic x86_64)

 * Documentation:  https://help.ubuntu.com/


Last login: Tue Jul 26 18:52:55 2011 from 192.168.1.116

如果您能够完成这些步骤,您就完成了。现在您有两台启用了ssh-key(public-key)登录的计算机。

次佳解决方案

setting up password-less ssh之后,我仍然被要求输入我的用户密码。查看远程机器上的/var/log/auth.log指出了问题:

sshd[4215]: Authentication refused: bad ownership or modes for directory /home/<user>

所以,确保正确:

chmod o-w ~/
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

虽然禁止其他用户在您的.ssh文件夹上进行书写,但对您的主文件夹具有相同的要求却比较棘手。

另外,请检查/etc/ssh/ssd_config以确保未禁用RSAAuthenticationPubkeyAuthentication选项。默认值为yes,这应该不是问题。

第三种解决方案

可能只是一个更高级别的权限问题。您需要从组和其他主目录和.ssh目录中删除写入权限。要修复这些权限,请运行chmod 755 ~ ~/.sshchmod go-w ~ ~/.ssh

如果您仍然遇到问题,请在日志中发出以下grep:

sudo egrep -i 'ssh.*LOCAL_USER_NAME' /var/log/secure

(用您的本地用户名替换LOCAL_USER_NAME ……)

假设sshd身份验证信息正在记录到安全日志中,应该默认情况下应该告诉您有关您的问题的更多信息。如果您看到如下错误:

DATE HOSTNAME sshd[1317]: Authentication refused: bad ownership or modes for directory /path/to/some/directory

这是上面描述的问题,您需要找到有问题的目录并从组和其他中删除写入权限。

至于您需要限制对主目录的写入权限的原因(即使权限已经限制在.ssh和后续目录中),它将允许其他用户重命名.ssh目录并创建一个新目录 – 尽管将无法使用(由于错误的权限)大多数用户的修复可能是更改权限而不是检查目录的内容…

TLDNR:允许对主目录的组和/或其他目录进行写访问将使ssh强制密码登录。

第四种方案

你在每台机器上使用root帐户吗?通常在Ubuntu上,您将使用用户帐户并根据需要为其授予sudo权限。

如果您使用非root用户sudo chown $USER -R ~/.ssh可能会解决您的问题

其他要检查的事项:

仔细检查B的id_rsa.pub是否在A的authorized_keys中。

检查A的/etc/ssh/sshd_config包含

PermitRootLogin yes
RSAAuthentication yes
PubkeyAuthentication yes

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/2169.html,未经允许,请勿转载。