SSH公钥认证不起作用

我在Ubuntu Server 12.04(A)上为SSH服务器设置公钥认证时遇到问题,无法从Ubuntu Server 13.04(B)进行认证。

我现在在做什么(我试图按照这里的说明):

  • 在B:使用ssh-keygen -C ""创建一个新密钥,不使用密码,写入/.ssh/id_rsa – 我没有收到任何错误
  • 在B:运行ssh-copy-id -i /.ssh/id_rsa user@host-a – 也是成功消息
  • 在B: ssh -i /.ssh/id_rsa user@host-a – 我仍然需要为user@host-a输入我的密码

在A上,我检查了运行ssh-copy-id后是否修改了/.ssh/authorized_keys ,情况就是如此。 此外,在两台设备上我都将其添加到/etc/ssh/sshd_config

 RSAAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile /.ssh/authorized_keys 

有谁知道这可能是什么问题?


这是机器A上我的/var/log/auth.log的尾部:

 Jun 13 22:17:56 laptop-camil sshd[12344]: Server listening on 0.0.0.0 port 22. Jun 13 22:17:56 laptop-camil sshd[12344]: Server listening on :: port 22. Jun 13 22:18:27 laptop-camil sshd[12345]: Authentication refused: bad ownership or modes for directory /.ssh Jun 13 22:18:30 laptop-camil sshd[12345]: Accepted password for camilstaps from 164.138.27.37 port 48407 ssh2 Jun 13 22:18:30 laptop-camil sshd[12345]: pam_unix(sshd:session): session opened for user camilstaps by (uid=0) Jun 13 22:18:35 laptop-camil sshd[12464]: Received disconnect from 164.138.27.37: 11: disconnected by user Jun 13 22:18:35 laptop-camil sshd[12345]: pam_unix(sshd:session): session closed for user camilstaps Jun 13 22:18:42 laptop-camil sshd[12516]: Authentication refused: bad ownership or modes for directory /.ssh Jun 13 22:18:44 laptop-camil sshd[12516]: Connection closed by  [preauth] 

日志文件中的任何内容,尤其是/var/log/auth.log ? 您还可以仔细检查.ssh目录和文件的权限。

我自己没有必要为这种访问修改sshd_config。 我想知道你的修改是否破坏了东西,尤其是AuthorizedKeysFile系列。 通常,您希望将authorized_keys置于$USER/.ssh

以下是我的一台服务器上用户的权限:

 :~/.ssh$ ls -ld . drwx------ 2 rrd rrd 4096 May 28 17:57 . :~/.ssh$ ll total 280 -rw-r----- 1 rrd rrd 4351 May 22 16:20 authorized_keys -rw------- 1 rrd rrd 1679 Apr 27 2012 id_rsa -rw-r--r-- 1 rrd rrd 399 Apr 27 2012 id_rsa.pub -rw-r--r-- 1 rrd rrd 266138 Jun 13 00:18 known_hosts 

确保单个文件至少受此限制。

正如guntbert指出的那样,还要检查目录和文件是否归您所有。 否则权限将没有意义(或工作)。

谁拥有B上的authorized_keys中的密钥? (在密钥后面写着user @ host的位。)它是root @ A吗?

也就是说,在查看~/.ssh/authorized_keys ,你的设置相当于bert@etherbert

 ssh-rsa AAAA...ffsII8dSaDF33 bert@etherbet 

我只是手动编辑远程.ssh /授权密钥进行测试,输入你正在开始连接的用户的id_rsa.pub内容。

确保您来自具有远程authorized_keys文件中的密钥的用户。

目录~/.ssh必须由用户拥有,而不是root。 所以改变它,它会工作。

为避免每次使用ssh-agent时都必须为私钥键入密码。 ssh-add .ssh/id_rsa会将密钥添加到代理,从那时起代理将提供密钥到ssh。

除了所有其他人提供了解决方案,我的另外建议是你应该首先检查日志文件: /var/log/secure ,这是sshd放入登录的地方。如果出现问题,检查sshd在/var/log/secure抱怨了什么/var/log/secure将迅速缩小可能的问题范围。

这是一个老问题,已经回答了,但如果用户加密了主目录(使用ecryptfs或其他类似的东西),ssh守护程序将无法看到〜/ .ssh / authorized_keys文件。 如果是这种情况,请遵循此处列出的解决方案。

此解决方案建议更改sshd配置(/ etc / ssh / sshd_config)并将AuthorizedKeysFile更改为/etc/ssh/%u/authorized_keys并将authorized_keys文件复制到/ etc / ssh / username / authorized_keys文件(以及/ etc的正确所有权) / ssh / username和sshd所需的适当权限)。

没有什么对我有用。 我不知道为什么? 我试过每个解决方案。

第一

ssh-copy-id:没有复制id_rsa和id_rsa.pub

第二

ssh-agent $ SHELL

ssh-add -L

SSH-ADD

ssh-copy-id -i remote-host

两者都没用。 我想我不走运。 有人说要从root更改.ssh文件夹的权限。 我认为这不是一个更好的选择。 当我的上述案例失败时我正在做什么。 我在服务器上创建了一个新密钥,并将此密钥保存在github / gitlab上。 这也不是一个很酷的方式。 在这里,我尝试了一个备用,我希望它可以帮助某人。

首先,我在远程服务器上创建一个文件夹,该文件夹具有可以写入的用户权限。 然后我在我的本地机器上按照以下步骤操作

cd~ / .ssh

scp -r * remote_user@192.168.100。**:path_to_writable_folder_on_remote_server

然后我登录远程服务器然后

cd path_to_that_folder_where_I_copied_keys

& 然后

mv *〜/ .ssh

(哇)最后,它奏效了。