第1章 SSH+Key实现基于密钥连接(Ansible使用前提)
说明:
Ansible其功能实现基于SSH远程连接服务
使用Ansible需要首先实现SSH密钥连接
ssh-keygen
-t 指定密钥类型 rsa1 dsa(常用) ecdsa
语法:
SYNOPSIS
ssh-keygen [-q] [-b bits] -t type [-N new_passphrase] [-C comment]
[-f output_keyfile]
ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile]
ssh-keygen -i [-f input_keyfile]
ssh-keygen -e [-f input_keyfile]
ssh-keygen -y [-f input_keyfile]
ssh-keygen -c [-P passphrase] [-C comment] [-f keyfile]
ssh-keygen -l [-f input_keyfile]
ssh-keygen -B [-f input_keyfile]
ssh-keygen -D pkcs11
ssh-keygen -F hostname [-f known_hosts_file] [-l]
ssh-keygen -H [-f known_hosts_file]
ssh-keygen -R hostname [-f known_hosts_file]
ssh-keygen -r hostname [-f input_keyfile] [-g]
ssh-keygen -G output_file [-v] [-b bits] [-M memory] [-S start_point]
ssh-keygen -T output_file -f input_file [-v] [-a num_trials]
[-W generator]
ssh-keygen [-n] [-D smartcard]
ssh-keygen -s ca_key -I certificate_identity [-h] [-Z principals]
[-O option] [-V validity_interval] [-z serial_number] file ...
ssh-keygen -L [-f input_keyfile]
创建密钥的过程
[root@m01 ~]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa): #私钥创建后保存的路径
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): #私钥需不需进行加密,设置密码
Enter same passphrase again: #私钥需不需进行加密,再次输入密码确认
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
31:4a:4f:9f:97:b0:b6:ca:4c:53:78:70:89:83:5f:16 root@m01
The key's randomart image is:
+--[ DSA 1024]----+
| E |
| . . o |
| o B * |
| . = @ + . |
| . S B o |
| + o |
| o . |
| + o |
| + |
+-----------------+
创建出来的文件:
[root@m01 ~]# ll /root/.ssh/
total 8
-rw------- 1 root root 668 Oct 17 18:55 id_dsa #创建出来的私钥
-rw-r--r-- 1 root root 598 Oct 17 18:55 id_dsa.pub #创建出来的公钥
[root@m01 ~]# man ssh-copy-id
ssh-copy-id - install your public key in a remote machine’s autho-rized_keys
注意:密钥分发命令属于openssh-clients软件包
[root@nfs01 ~]# rpm -qf `which ssh-copy-id`
openssh-clients-5.3p1-122.el6.x86_64
语法格式
ssh-copy-id [-i [identity_file]] [user@]machine
-i 指定要分发的公钥文件以及路径信息
[user@] 以什么用户身份进行分发
machine 将公钥分发到哪台主机上,远程主机IP地址
[root@m01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@172.16.1.41
The authenticity of host '172.16.1.41 (172.16.1.41)' can't be established.
RSA key fingerprint is d3:41:bb:0d:43:88:da:a3:2c:e8:36:91:11:c9:e4:9c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.1.41' (RSA) to the list of known hosts.
root@172.16.1.41's password:
Now try logging into the machine, with "ssh 'root@172.16.1.41'", and check in
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[root@m01 ~]# ssh 172.16.1.41
Last login: Tue Oct 17 18:38:47 2017 from 10.0.0.1
[root@backup ~]#
基于密钥登陆方式成功↑
[root@m01 ~]# ssh root@172.16.1.41 "hostname -i"
172.16.1.41
不用的登陆到远程主机直接执行命令,返回输出结果↑
说明:
管理主机一旦创建好秘钥对文件,给多个主机分发公钥时,公钥文件相同
①. 管理服务器创建私钥和公钥(密钥对)
②. 将公钥文件远程传送复制到被管理服务器相应用户~/.ssh/id_dsa.pub下,并修改.ssh目录权限为700
③. 修改公钥文件文件名称为authorized_keys,授权权限为600
④. 利用ssh服务配置文件的配置参数,进行识别公钥文件authorized_keys
⑤. 进而实现基于密钥远程登录服务器(免密码登录/非交互方式登录)
[root@m01 ~]# file `which ssh-copy-id `
/usr/bin/ssh-copy-id: POSIX shell script text executable
看看脚本内容发现传输方式
[root@m01 ~]# cat `which ssh-copy-id`|grep ssh
ssh $1 "exec sh -c 'cd; 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
说明:
1、切换用户到家目录下,临时设置umask值
2、判断客户端相应用户中有没有.ssh目录,如果没有.ssh 目录就进行创建
3、将管理端公钥文件内容添加到客户端~./ssh/authorized_keys, 默认authorized_keys文件不存在,需要创建,文件权限600
方法一: 修改脚本内容
ssh -p52113 $1 "exec sh -c 'cd; 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
说明:根据命令脚本,修改$1传参信息,从而实现根据ssh不同端口传送公钥文件
方法二:将传入的参数上添加上端口信息(推荐)
[root@m01 scripts]# ssh-copy-id -i /root/.ssh/id_dsa.pub "-p 52113 znix@172.16.1.250"
Now try logging into the machine, with "ssh '-p 52113 znix@172.16.1.250'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[root@m01 scripts]# cat shift.sh
#!/bin/bash
until [ $# -eq 0 ]
do
echo $*
shift
done
测试
[root@m01 scripts]# sh shift.sh 1 2 3 4 5 6
1 2 3 4 5 6
2 3 4 5 6
3 4 5 6
4 5 6
5 6
6
说明:
shift命令用于对参数的移动(左移),通常用于在不知道传入参数个数的情况下依次遍历每个参数然后进行相应处理(常见于Linux中各种程序的启动脚本)。
ssh-copy-id -i /root/.ssh/id_dsa.pub "-p 52113 znix@172.16.1.250"
由于/usr/bin/ssh-copy-id 脚本中前面使用了两个shift 所有原本该为的参数变为了 3的参数变为了 1.
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
fi
else
#for 循环
for n in (1..100)
do
xxx
done
#while循环:循环条件为真时,一直循环;为假时,停止循环
while [ture]
do
xxx
done
#until 循环: 循环条件为假时,一直循环;为真时,停止循环
until [ture]
do
xxx
done
01.创建秘钥对需要进行交互
a.需要确认秘钥保存路径
b.需要确认密码信息
02.分发公钥时需要进行交互
a.需要进行确认yes|no
b.第一次分发公钥需要进行密码认证
ssh-keygen -t rsa -f ~/.ssh/id_rsa -N "" -q
参数说明:
-f filename Specifies the filename of the key file.
指定密钥文件保存的路径信息(免交互)
-P passphrase Provides the (old) passphrase.
提供一个密码信息
-N new_passphrase Provides the new passphrase.
-P -N 都是免交互方式指定密码信息
-q 安静的 不输出信息,减少信息输出
sshpass -p123456 ssh-copy-id -i ~/.ssh/id_rsa.pub " root@172.16.1.$ip -o StrictHostKeyChecking=no "
参数说明:
-o option 选择 (man 手册中可以查到有很多选项)
StrictHostKeyChecking=no 对询问的回应(不进行对密钥检查)
要实现免密码,需要一款软件 sshpass 该软件就是为ssh提供密码使用的
[root@m01 ~]# yum install sshpass -y
注意:密码与 -p之间不能有空格
[root@m01 scripts]# vim ssh-key.sh
#!/bin/bash
. /etc/rc.d/init.d/functions
# 创建密钥
\rm ~/.ssh/id_rsa* -f
ssh-keygen -t rsa -f ~/.ssh/id_rsa -N "" -q
# 分发公钥
for ip in 31 41 8
do
sshpass -p123456 ssh-copy-id -i ~/.ssh/id_rsa.pub " root@172.16.1.$ip -o StrictHostKeyChecking=no " &>/dev/null
if [ $? -eq 0 ];then
action "fenfa 172.16.1.$ip" /bin/true
else
action "fenfa 172.16.1.$ip" /bin/false
fi
echo ""
done
脚本执行效果:
[root@m01 scripts]# sh ssh-key.sh
fenfa 172.16.1.31 [ OK ]
fenfa 172.16.1.41 [ OK ]
fenfa 172.16.1.8 [ OK ]
说明:
脚本中引用 . /etc/rc.d/init.d/functions 函数,可以显示执行结果的判断。
使用if语句进行判断,action 执行相应的动作。true/false
[root@m01 scripts]# vim piliang_guanli.sh
#!/bin/bash
CMD=$1
for ip in 8 31 41
do
echo ========host 172.16.1.$ip=======
ssh root@172.16.1.$ip "$CMD"
echo ============END===============
echo ""
done
脚本执行效果:
[root@m01 scripts]# sh piliang_guanli.sh date
======172.16.1.8======
Thu Oct 19 16:25:08 CST 2017
=========END=============
======172.16.1.31======
Thu Oct 19 16:25:08 CST 2017
=========END=============
======172.16.1.41======
Thu Oct 19 16:25:08 CST 2017
=========END=============
基于密钥登陆方式,分发的公钥文件会识别用户信息,所以能够实现免密码批量管理。