分享

Linux(Centos)配置OpenSSH无密码登陆配置的两种方法

nettman 2013-12-2 13:28:05 发表于 实践型 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 0 10145
本帖最后由 nettman 于 2013-12-2 14:46 编辑

最近在搭建Hadoop环境需要设置无密码登陆,所谓无密码登陆其实是指通过证书认证的方式登陆,使用一种被称为"公私钥"认证的方式来进行ssh登录。
在linux系统中,ssh是远程登录的默认工具,因为该工具的协议使用了RSA/DSA的加密算法.该工具做linux系统的远程管理是非常安全的。telnet,因为其不安全性,在linux系统中被搁置使用了。

       " 公私钥"认证方式简单的解释:首先在客户端上创建一对公私钥 (公钥文件:~/.ssh/id_rsa.pub; 私钥文件:~/.ssh/id_rsa)。然后把公钥放到服务器上(~/.ssh/authorized_keys), 自己保留好私钥.在使用ssh登录时,ssh程序会发送私钥去和服务器上的公钥做匹配.如果匹配成功就可以登录了。

ssh配置概述:  
主机A:10.0.5.199
主机B:10.0.5.198
需要配置主机A无密码登录主机A,主机B
先确保所有主机的防火墙处于关闭状态。
在主机A上执行如下:
 1. $cd ~/.ssh
 2. $ssh-keygen -t rsa  --------------------然后一直按回车键,就会按照默认的选项将生成的密钥保存在.ssh/id_rsa文件中。
 3. $cp id_rsa.pub authorized_keys
         这步完成后,正常情况下就可以无密码登录本机了,即ssh localhost,无需输入密码。
 4. $scp authorized_keys summer@10.0.5.198:/home/summer/.ssh   ------把刚刚产生的authorized_keys文件拷一份到主机B上.  
 5. $chmod 600 authorized_keys      
     进入主机B的.ssh目录,改变authorized_keys文件的许可权限。
   (4和5可以合成一步,执行:  $ssh-copy-id -i summer@10.0.5.198 )

正常情况下上面几步执行完成后,从主机A所在机器向主机A、主机B所在机器发起ssh连接,只有在第一次登录时需要输入密码,以后则不需要。

可能遇到的问题:
1.进行ssh登录时,出现:”Agent admitted failure to sign using the key“ .
   执行: $ssh-add
   强行将私钥 加进来。
2.如果无任何错误提示,可以输密码登录,但就是不能无密码登录,在被连接的主机上(如A向B发起ssh连接,则在B上)执行以下几步:
  $chmod o-w ~/
   $chmod 700 ~/.ssh
   $chmod 600 ~/.ssh/authorized_keys

3.如果执行了第2步,还是不能无密码登录,再试试下面几个
  $ps -Af | grep agent
        检查ssh代理是否开启,如果有开启的话,kill掉该代理,然后执行下面,重新打开一个ssh代理,如果没有开启,直接执行下面:
       $ssh-agent
  还是不行的话,执行下面,重启一下ssh服务
       $sudo service sshd restart
4. 执行ssh-add时提示“Could not open a connection to your authenticationh agent”而失败
执行: ssh-agent bash
下面为大家详细介绍具体的步骤:
以Centos(Centos5 ) 为例详细讲解如何配置证书验证登陆,具体操作步骤如下:
1. 确认系统已经安装好OpenSSH的server 和client
   
2. 确认本机sshd的配置文件(需要root权限)
    $ vi /etc/ssh/sshd_config
    找到以下内容,并去掉注释符”#“
    RSAAuthentication yes
    PubkeyAuthentication yes
    AuthorizedKeysFile      .ssh/authorized_keys

3.  如果修改了配置文件需要重启sshd服务 (需要root权限)
   $ vi /sbin/service sshd restart


4. ssh登陆系统 后执行测试命令:
   $ ssh localhost
   回车会提示你输入密码,因为此时我们还没有生成证书

5.生成证书公私钥的步骤:
   $ ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa
   $ cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys

6.测试登陆 ssh localhost:
   $ ssh localhost
正常情况下会登陆成功,显示一些成功登陆信息,如果失败请看下面的 一般调试步骤

7.一般调试步骤
   本人在配置时就失败了,按照以上步骤依旧提示要输入密码。于是用ssh -v 显示详细的登陆信息查找原因:
   $ ssh -v localhost
   回车显示了详细的登陆信息如下:

。。。。。。
debug1: Authentications that can continue: publickey,gssapi-with-mic,password
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure. Minor code may provide more information
Unknown code krb5 195 debug1: Unspecified GSS failure. Minor code may provide more information
Unknown code krb5 195
debug1: Unspecified GSS failure. Minor code may provide more information
Unknown code krb5 195
debug1: Next authentication method: publickey
debug1: Trying private key: /home/huaxia/.ssh/identity
debug1: Trying private key: /home/huaxia/.ssh/id_rsa
debug1: Offering public key: /home/huaxia/.ssh/id_dsa
debug1: Authentications that can continue: publickey,gssapi-with-mic,password
debug1: Next authentication method: password
huaxia@localhost's password:



同时用root用户登陆查看系统的日志文件:

   $tail /var/log/secure -n 20

。。。。。。
Jul 13 11:21:05 shnap sshd[3955]: Accepted password for huaxia from 192.168.8.253 port 51837 ssh2
Jul 13 11:21:05 shnap sshd[3955]: pam_unix(sshd:session): session opened for user huaxia by (uid=0)
Jul 13 11:21:47 shnap sshd[4024]: Connection closed by 127.0.0.1
Jul 13 11:25:28 shnap sshd[4150]: Authentication refused: bad ownership or modes for file /home/huaxia/.ssh/authorized_keys
Jul 13 11:25:28 shnap sshd[4150]: Authentication refused: bad ownership or modes for file /home/huaxia/.ssh/authorized_keys
Jul 13 11:26:30 shnap sshd[4151]: Connection closed by 127.0.0.1
。。。。。。


   从上面的日志信息中可知文件/home/huaxia/.ssh/authorized_keys 的权限有问题。
   查看/home/huaxia/.ssh/ 下文件的详细信息如下:

$ ls -lh ~/.ssh/
-rw-rw-r-- 1 huaxia huaxia 602 07-13 11:22 authorized_keys
-rw------- 1 huaxia huaxia 672 07-13 11:22 id_dsa
-rw-r--r-- 1 huaxia huaxia 602 07-13 11:22 id_dsa.pub
-rw-r--r-- 1 huaxia huaxia 391 07-13 11:21 known_hosts
   修改文件authorized_keys的权限(权限的设置非常重要,因为不安全的设置安全设置,会让你不能使用RSA功能 ):

$ chmod 600 ~/.ssh/authorized_keys

   再次测试登陆如下:

$ ssh localhost
Last login: Wed Jul 13 14:04:06 2011 from 192.168.8.253

   看到这样的信息表示已经成功实现了本机的无密码登陆。

8.认证登陆远程服务器(远程服务器OpenSSH的服务当然要启动)

    拷贝本地生产的key到远程服务器端(两种方法)
    方法一:
$cat ~/.ssh/id_rsa.pub | ssh 远程用户名@远程服务器ip 'cat - >> ~/.ssh/authorized_keys'

    方法二:
    在本机上执行:
$ scp ~/.ssh/id_dsa.pub michael@192.168.8.148:/home/michael/
    登陆远程服务器michael@192.168.8.148 后执行:

$ cat id_dsa.pub >> ~/.ssh/authorized_keys

   本机远程登陆192.168.8.148的测试:

$ssh michael@192.168.8.148
Linux michael-VirtualBox 2.6.35-22-generic #33-Ubuntu SMP Sun Sep 19 20:34:50 UTC 2010 i686 GNU/Linux
Ubuntu 10.10Welcome to Ubuntu!
* Documentation: https://help.ubuntu.com/
216 packages can be updated.
71 updates are security updates.
New release 'natty' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Wed Jul 13 14:46:37 2011 from michael-virtualbox
michael@michael-VirtualBox:~$


    可见已经成功登陆。

     如果登陆测试不成功,需要修改远程服务器192.168.8.148上的文件authorized_keys的权限(权限的设置非常重要,因为不安全的设置安全设置,会让你不能使用RSA功能 )

chmod 600 ~/.ssh/authorized_keys



加微信w3aboutyun,可拉入技术爱好者群

没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条