最近在搭建Hadoop环境须要设置无密码登录,所谓无密码登录实际上是指经过证书认证的方式登录,使用一种被称为"公私钥"认证的方式来进行ssh登陆。 linux
在linux系统中,ssh是远程登陆的默认工具,由于该工具的协议使用了RSA/DSA的加密算法.该工具作linux系统的远程管理是很是安全的。telnet,由于其不安全性,在linux系统中被搁置使用了。
算法
" 公私钥"认证方式简单的解释:首先在客户端上建立一对公私钥 (公钥文件:~/.ssh/id_rsa.pub; 私钥文件:~/.ssh/id_rsa)。而后把公钥放到服务器上(~/.ssh/authorized_keys), 本身保留好私钥.在使用ssh登陆时,ssh程序会发送私钥去和服务器上的公钥作匹配.若是匹配成功就能够登陆了。
ubuntu
在Ubuntu和Cygwin 配置都很顺利,而在Centos系统中配置时遇到了不少问题。故此文以Centos(Centos5 ) 为例详细讲解如何配置证书验证登录,具体操做步骤以下: api
1. 确认系统已经安装好OpenSSH的server 和client 安全
安装步骤这里再也不讲述,不是本文的重点。 服务器
2. 确认本机sshd的配置文件(须要root权限) session
$ vi /etc/ssh/sshd_config ssh
找到如下内容,并去掉注释符”#“ ide
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/
总计 16K
-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.10
Welcome 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
记得关闭selinux
查看SELinux状态:
一、/usr/sbin/sestatus -v ##若是SELinux status参数为enabled即为开启状态
SELinux status: enabled
二、getenforce ##也能够用这个命令检查
关闭SELinux:
一、临时关闭(不用重启机器):
setenforce 0 ##设置SELinux 成为permissive模式
##setenforce 1 设置SELinux 成为enforcing模式
二、修改配置文件须要重启机器:
修改/etc/selinux/config 文件
将SELINUX=enforcing改成SELINUX=disabled
重启机器便可