T--Linux SSH免密码登录

来源:互联网 发布:三种网络分层结构 编辑:程序博客网 时间:2024/06/03 18:27

Linux SSH免密码登录

转自:http://m.blog.csdn.net/article/details?id=44803835 (侵删)

Hadoop中有些管理脚本需要用到SSH连接,这些脚本是自动运行的,我们显然不想每次运行这些脚本都输入密码。但是反过来,我们也不希望没有授权的用户可以不用密码登录SSH。

为此,我们需要设置一种安全又便捷的免密码登录——在登录了一台机器之后可以不要密码登录别的机器(举例来说,在master上的grid用户只要在master上验证就允许它访问slave1、slave2了)。


原理

为了理解SSH免密码登录的原理,我们先来看看密码登录时的原理:

1.客户端发送SSH连接请求。

2.服务端响应请求,将公钥发送给客户端。

3.客户端用公钥加密自己的用户名发送给服务端。

4.服务端用私钥解密用户名,比对用户名列表看是否允许登录。

5.若允许登录则接受请求,要求输入密码。

6.客户端用公钥加密密码,并发送给服务端。

7.服务端用私钥解密密码,验证用户。

8.验证成功后提示用户。

9.客户端用公钥加密一个通信密码给服务端,此后双方用此密码加密通信内容。


通信的关键是一对公钥-私钥系统,公钥加密的数据必须用私钥解开,同样地,私钥加密的数据必须由公钥解开。那么,如果服务端有客户端的公钥,它用此公钥加密的数据就只有客户端自己能用自己的私钥解开(假设客户端保密工作做的好,只有自己知道自己的私钥)。所以,如果客户端能解开服务端加密的数据,那么服务端就能够信任此客户端为可信的。所以,只要在服务端拷贝客户端的公钥,就可以完成安全快捷的免密码SSH登录。原理如下:

1.客户端发送SSH连接请求。

2.服务端发现在authorized_keys文件中有客户端的主机名。

3.服务端用authorized_keys里客户端的公钥加密一个challenge。

4.客户端解密challenge,将内容用服务端的公钥加密传给服务端。

5.服务端收到challenge,发现与原始内容一样,完成客户端认证。

6.客户端用服务端公钥加密通信密码,双方用通信密码通信。


步骤

为了将公钥拷贝到服务端,首先要在本地创建公钥和私钥文件,这可以通过命令ssh-keygen -t rsa来完成。然后要将客户端的公钥拷贝到服务端,对应用户的~/.ssh/authorized_keys文件中。为此我们可以有多种方法,例如可以将文件拷贝,然后到对应目录去vim黏贴。可以用scp传送到目标目录,然后在用vim添加。

还有一种方法,也是最常用的,就是使用ssh-copy-id脚本,用这种方法,只要运行脚本:

ssh-copy-id -i id_rsa.pub user@machine

就可以完成拷贝公钥到目标节点的全部工作,省去我们不少的力气。ssh-copy-id脚本的位置:/usr/bin/ssh-copy-id,脚本内容如下:

#!/bin/sh                                                                                                                                                              # Shell script to install your public key on a remote machine# Takes the remote machine name as an argument.# Obviously, the remote machine must accept password authentication,# or one of the other keys in your ssh-agent, for this to work. ID_FILE="${HOME}/.ssh/id_rsa.pub"                                                                                                              if [ "-i" = "$1" ]; then                                                                                                                                      shift                                                                                                                                                              # check if we have 2 parameters left, if so the first is the new ID file  if [ -n "$2" ]; then                                                                                                                                          if expr "$1" : ".*\.pub" > /dev/null ; then                                                                                                      ID_FILE="$1"                                                                                                                                             else      ID_FILE="$1.pub"                                                                                                                                  fi    shift         # and this should leave $1 as the target name                                                                             fielse  if [ x$SSH_AUTH_SOCK != x ] ; then                                                                                                              GET_ID="$GET_ID ssh-add -L"  fifi if [ -z "`eval $GET_ID`" ] && [ -r "${ID_FILE}" ] ; then  GET_ID="cat ${ID_FILE}"fi if [ -z "`eval $GET_ID`" ]; then  echo "$0: ERROR: No identities found" >&2  exit 1fi if [ "$#" -lt 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then  echo "Usage: $0 [-i [identity_file]] [user@]machine" >&2  exit 1fi { eval "$GET_ID" ; } | ssh $1 "umask 077; test -d ~/.ssh || mkdir ~/.ssh ; cat >> ~/.ssh/authorized_keys && (test -x /sbin/restorecon && /sbin/restorecon ~/.ssh~/.ssh/authorized_keys >/dev/null 2>&1 || true)" || exit 1 cat <<EOFNow try logging into the machine, with "ssh '$1'", and check in:   .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting. EOF


0 0
原创粉丝点击