您的位置:首页 > 其它

免密码交互方式+ansible批量管理服务介绍

2018-01-14 18:09 525 查看

介绍了ssh服务

1) 远程连接加密传输数据协议,实现远程连接登录,默认端口22
2)ssh远程连接原理
依赖于锁头(公钥)和钥匙(私钥),实现远程加密连接
3)ssh基于秘钥远程登录原理
a 管理服务器创建秘钥対,将公钥传输发送给给管理端
b 管理端请求与被管理端建立连接
c 被管理向管理端发送公钥质询
d 管理端处理质询信息,实现管理与被管理端免密码交互
4)基于ssh协议相关命令
ssh scp sftp

netstat -lntup |egrep sshd 查看ssh端口

1.1 部署ssh+key (免密码交互方式) 架构换环境

确认一下部署架构环境
管理服务器:m01
被管理服务器: web01 nfs01 backup

架构部署(ssh+key)
第一个里程:在管理服务器上创建秘钥対
两种创建秘钥对方法:
a 利用交互方式创建秘钥对
[root@m01 ~]# ssh-keygen -t dsa
Generating public/private dsa key pair.                    --- 提示进行秘钥对创建
Enter file in which to save the key (/root/.ssh/id_dsa):   --- 提示私钥文件保存在什么位置,进行确认
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:                                    --- 以下内容表示秘钥指纹信息提示
0b:d2:c0:14:3c:9b:9d:de:1b:d8:3a:c6:92:f9:39:d5 root@m01
The key's randomart image is:
+--[ DSA 1024]----+
|   .o.           |
|   oo            |
|    o= .         |
|    ooo          |
|    ..o+S.       |
|     .o.=.E      |
|     + o.o       |
|    + *..        |
|     +oo         |
+-----------------+

b 利用免交互方式创建秘钥对
a 交互方式位置:需要确认私钥文件保存路径
-f filename  Specifies the filename of the key file.
-f "/root/.ssh/id_dsa"
b 交互方式位置:需要进行私钥文件加密确认
-N new_passphrase    Provides the new passphrase.
-P passphrase        Provides the (old) passphrase.
-N ""
ssh-keygen -t dsa -f "/root/.ssh/id_dsa" -N ""
ssh-keygen -t dsa -f "/root/.ssh/id_dsa" -N "" -q

第二个里程:在管理服务器上分发公钥给被管理端服务器
a 利用交互方式实现公钥分发
ssh-copy-id [-i [identity_file]] [user@]machine
ssh-copy-id -i /root/.ssh/id_dsa.pub  172.16.1.41

ssh-copy-id -i /root/.ssh/id_dsa.pub  172.16.1.31

ssh-copy-id -i /root/.ssh/id_dsa.pub  172.16.1.8

[root@m01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub  172.16.1.41

The authenticity of host '172.16.1.41

(172.16.1.41

)' can't be established.
RSA key fingerprint is 59:41:4e:36:ae:75:83:01:23:93:7b:c8:68:ff:37:9f.
Are you sure you want to continue connecting (yes/no)? yes --- 确认是否接受连接主机公钥信息
Warning: Permanently added '172.
.1.41

' (RSA) to the list of known hosts.
root@172.16.1.41's password: --- 首次连接需要基于口令连接
Now try logging into the machine, with "ssh '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 Dec  5 12:02:48 2017 from 10.0.0.253

[root@backup ~]# exit   退出当前客服端

[root@m01 ~]# ssh 172.16.1.41

uptime --- 可以不用登录主机,利用命令直接查看远程主机信息
09:52:05 up 9:02, 1 user, load average: 0.00, 0.00, 0.00

问题:如果客户端默认ssh端口发生变化,如何进行分发公钥
查看ssh-copy-id脚本文件信息
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/nu
ll 2>&1 || true)'" || exit 1

a 临时修改umask值信息为077
b 判断.ssh目录是否存在,如果没有不存在,创建.ssh目录
c 把管理端公钥文件中的内容复制到被管理端~/.ssh/authorized_keys文件中,设置权限为600
666-077=6 -1 -1 = 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/nu
ll 2>&1 || true)'" || exit 1

处理问题方法二:直接利用命令参数实现
ssh-copy-id -i /root/.ssh/id_dsa.pub  "172.16.1.8 -p52113"

问题:如果客户端默认ssh端口发生变化,如何进行分发公钥
查看ssh-copy-id脚本文件信息
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/nu
ll 2>&1 || true)'" || exit 1

a 临时修改umask值信息为077
b 判断.ssh目录是否存在,如果没有不存在,创建.ssh目录
c 把管理端公钥文件中的内容复制到被管理端~/.ssh/authorized_keys文件中,设置权限为600
666-077=6 -1 -1 = 600

处理问题方法一:直接修改脚本
ssh –p22 $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/nu
ll 2>&1 || true)'" || exit 1

处理问题方法二:直接利用命令参数实现
ssh-copy-id -i /root/.ssh/id_dsa.pub  "172.16.1.8 -p52113"
说明:正确理解是
-i                      为$1
/root/.ssh/id_dsa.pub   为$2
172.16.1.8

为$3
但是ssh-copy-id脚本文件中出现了两次shift参数,所以最终导致172.16.1.8

的$3变为了$1

理解shift脚本命令用法
[root@m01 scripts]# vim test_shift.sh

#!/bin/bash
until [ $# -eq 0 ]
do
echo $*
shift
done

[root@m01 scripts]# sh test_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

b 第一次远程连接需要基于口令认证
sshpass -p 123456 ssh-copy-id -i /root/.ssh/id_dsa.pub  "172.16.1.8 -p22 -o StrictHostKeyChecking=no"
Now try logging into the machine, with "ssh '172.16.1.8

-p52113 -o StrictHostKeyChecking=no'", and check in:

.ssh/authorized_keys

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

第三个里程碑:如何实现公钥批量分发,秘钥对自动生成
编写脚本实现公钥批量分发

[root@m01 scripts]# cat fenfa_check.sh
#!/bin/bash

var info
Password_info=123456
Server_Port=22
Cmd_info=$1

push public key to client server
for ip in 8 31 41
do
echo "================= host 172.16.1.$ip check_info ================="
ssh -p$Server_Port 172.16.1.$ip $Cmd_info
echo ""
done

[root@m01 scripts]# cat fenfa_keygen.sh
#!/bin/bash

var info
Password_info=123456
Server_Port=22

create key pair
rm /root/.ssh/id_dsa* -f
ssh-keygen -t dsa -f "/root/.ssh/id_dsa" -N "" -q

push public key to client server
for ip in 8 31 41
do
echo "================= host 172.16.1.$ip info ================="
sshpass -p $Password_info ssh-copy-id -i /root/.ssh/id_dsa.pub "172.16.1.$ip -p$Server_Port -o StrictHostKeyChecking=no"
echo "================= host info end ================="
echo ""
done

[root@m01 scripts]# cat test_shift.sh
#!/bin/bash

until [ $# -eq 0]
do
echo $*
shift
done
安装免密码sshpass

ansible批量管理服务介绍

软件由python语言开发
其功能实现基于SSH远程连接服务
可以实现批量系统配置、批量软件部署、批量文件拷贝、批量运行命令等功能

ansible软件参考资料

说明信息:
ansible软件相关参考链接信息
http://docs.ansible.com/ansible/intro_installation.html
http://www.ansible.com.cn/
http://docs.ansible.com/modules_by_category.html
http://www.ansible.cn/docs/

2.1 ansible软件特点

a 不需要单独安装客户端(no agents),基于系统自带的sshd服务,sshd就相当于ansible的客户端。
b 不需要服务端(no servers)
c 需要依靠大量的模块实现批量管理。
d 配置文件/etc/ansible/ansible.cfg,不用配置

2.2 安装部署ansible

管理端部署:
yum install -y ansible --- ansible软件也来自epel源

被管理端部署:
yum install libselinux-python -y --- 被管理端需要进行安装的软件(不安装看看会不会遇到问题)

2.3 配置ansible

vim /etc/ansible/hosts
[oldboy]
172.16.1.8
172.16.1.41
172.16.1.31
说明:才文件用来定义ansible可以管理的主机信息(IP地址或者域名)

变态需求:不想分发ssh-key公钥,又想利用ansible批量管理
[root@oldboy.com ~]# cat /etc/ansible/hosts
[test]
172.16.1.7 ansible_ssh_user=root ansible_ssh_pass=123456
172.16.1.31 ansible_ssh_user=root ansible_ssh_pass=123456
172.16.1.41 ansible_ssh_user=root ansible_ssh_pass=123456
说明:后面的用户和密码项是非必须的,在配置key认证的情况下,不使用密码也可以直接操作 。
未使用key的,也可以在ansible通过 -k参数在操作前询问手动输入密码。

2.4 利用ansible命令进行远程管理了

ansible命令语法
ansible oldboy -m command -a "hostname" --- 实现ansible第一次批量管理功能

ansible测试管理端与被管理端连通性命令
[root@m01 scripts]# ansible oldboy -m ping
172.16.1.31 | SUCCESS => {
"changed": false,
"failed": false,
"ping": "pong"
}
172.16.1.8 | SUCCESS => {
"changed": false,
"failed": false,
"ping": "pong"
}
172.16.1.41 | SUCCESS => {
"changed": false,
"failed": false,
"ping": "pong"
}
常见的报错
172.16.1.31 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).\r\n",
"unreachable": true
}
172.16.1.41 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).\r\n",
"unreachable": true
}
解决方式
sshpass -p 123456 ssh-copy-id -i /root/.ssh/id_dsa.pub "172.16.1.8 -p52113 -o StrictHostKeyChecking=no"
ansible oldboy -m command -a "hostname"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  免密码 交互方式