Ansible服务部署与使用

第1章 SSH+Key实现基于密钥连接(Ansible使用前提)

说明:

  Ansible其功能实现基于SSH远程连接服务

  使用Ansible需要首先实现SSH密钥连接

1.1 部署SSH Key

1.1.1 第一个里程碑: 创建密钥对

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  #创建出来的公钥

1.1.2 第二个里程碑: 分发公钥文件

[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 '[email protected]'", and check in

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting. 

1.1.3 第三个里程碑: 基于密钥登陆测试

[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

不用的登陆到远程主机直接执行命令,返回输出结果↑

说明:

管理主机一旦创建好秘钥对文件,给多个主机分发公钥时,公钥文件相同

扫描二维码关注公众号,回复: 2007266 查看本文章

1.1.4 ssh服务分发公钥实质执行过程

①. 管理服务器创建私钥和公钥(密钥对

②. 将公钥文件远程传送复制到被管理服务器相应用户~/.ssh/id_dsa.pub下,并修改.ssh目录权限为700

③. 修改公钥文件文件名称为authorized_keys,授权权限为600

④. 利用ssh服务配置文件的配置参数,进行识别公钥文件authorized_keys

⑤. 进而实现基于密钥远程登录服务器(免密码登录/非交互方式登录)

1.2 默认端口号不是22,如何分发公钥

1.2.1 查询ssh-copy-id命令可以得知这是个脚本文件

[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

1.2.2 实现非22端口的分发

方法一: 修改脚本内容

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 [email protected]"
Now try logging into the machine, with "ssh '-p 52113 [email protected]'", and check in:
 
  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

1.2.3 关于 /usr/bin/ssh-copy-id 脚本中 $1的说明

1.2.3.1  编写脚本shift

[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 [email protected]"

由于/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

1.3 实现自动分发公钥,远程管理多台主机

1.3.1 【预备知识】shell中三种循环

#for 循环

for n in (1..100)
do
      xxx
done

#while循环:循环条件为真时,一直循环;为假时,停止循环

while [ture]
do
      xxx
done

#until 循环: 循环条件为假时,一直循环;为真时,停止循环

until [ture]
do
   xxx
done
 

1.3.2 实现自动分发公钥,远程管理多台主机的阻碍因素?

01.创建秘钥对需要进行交互

  a.需要确认秘钥保存路径

  b.需要确认密码信息

02.分发公钥时需要进行交互

  a.需要进行确认yes|no

  b.第一次分发公钥需要进行密码认证

1.3.3 解决阻碍因素

1.自动保存路径,并且不密码

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 安静的    不输出信息,减少信息输出

2.解决分发公钥时需要进行的交互

sshpass -p123456 ssh-copy-id -i ~/.ssh/id_rsa.pub " [email protected].$ip  -o StrictHostKeyChecking=no "

参数说明:

-o  option  选择 ( man 手册中可以查到有很多选项)
StrictHostKeyChecking=no 对询问的回应(不进行对密钥检查)

要实现免密码,需要一款软件 sshpass  该软件就是为ssh提供密码使用的

[root@m01 ~]# yum install  sshpass  -y

注意:密码与 -p之间不能有空格

1.3.4 最终批量分发脚本内容

[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 " [email protected].$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

1.3.5 实现基于密钥的批量管理脚本

[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=============

基于密钥登陆方式,分发的公钥文件会识别用户信息,所以能够实现免密码批量管理。

猜你喜欢

转载自www.linuxidc.com/Linux/2017-10/148047.htm