无密码ssh无法正常工作
我试图设置一个无密码的ssh b / w A
到B
和B
到A
在两台计算机上使用ssh-keygen -trsa
生成公钥和私钥。 使用ssh-copy-id
实用程序将公钥从A
复制到B
以及将B
复制到A
无密码ssh从A
到B
工作,但从B
到A
。 我已经检查了〜/ 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
身份validation。 我也在两台机器上运行了ssh-add
命令。
/etc/ssh/sshd_config
文件的身份validation部分是
# 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(公钥)登录的计算机。
设置无密码ssh后 ,我仍然被要求输入我的用户密码。 查看远程计算机上的/var/log/auth.log
指出了问题:
sshd[4215]: Authentication refused: bad ownership or modes for directory /home/
所以,确保正确:
chmod ow ~/ chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys
虽然禁止其他用户写你的.ssh
文件夹是显而易见的,对你的主文件夹有相同的要求是比较棘手的。
另外,请检查/etc/ssh/ssd_config
以确保未禁用RSAAuthentication
和PubkeyAuthentication
选项。 默认为yes
因此不应该是一个问题。
可能只是一个更高级别的权限问题。 您需要从组和其他目录中删除写入权限到您的主目录和.ssh目录。 要修复这些权限,请运行chmod 755 ~ ~/.ssh
或chmod go-w ~ ~/.ssh
。
如果您仍然遇到问题,请在日志中发出以下grep:
sudo egrep -i 'ssh.*LOCAL_USER_NAME' /var/log/secure
(将LOCAL_USER_NAME
替换为您的本地用户名…)
假设sshd身份validation信息正在记录到安全日志中,应该默认情况下,这应该可以告诉您有关您的问题的更多信息。 如果您看到如下错误:
DATE HOSTNAME sshd [1317]:身份validation被拒绝:目录/路径/到/某些/目录的所有权或模式不正确
这是上面描述的问题,您需要找到有问题的目录并从组和其他中删除写入权限。
至于您需要限制对主目录的写入权限的原因(即使权限已经限制在.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
在/ etc / ssh / sshd_config中对目标进行更改
PermitRootLogin没有
至
PermitRootLogin是的
然后杀死-HUP你的sshd PID:
root @dzone2#ps -ef | grep ssh root 28075 27576 0 11月17日? 6:11 / usr / lib / ssh / sshd
root 17708 20618 0 10:09:30 pts/37 0:00 grep ssh root@dzone2 # kill -HUP 28075 root@dzone2 # ps -ef|grep ssh root 17861 20618 0 10:09:44 pts/37 0:00 grep ssh root 17852 27576 0 10:09:42 ? 0:00 /usr/lib/ssh/sshd