单独 h+ agen lena entity 内容 设置 ati ssh-key
介绍了 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: --- 以下内容表示秘钥指纹信息提示
b 利用免交互方式创建秘钥对
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 |
+-----------------+'
a 交互方式位置:需要确认私钥文件保存路径
b 交互方式位置:需要进行私钥文件加密确认
-f filename Specifies the filename of the key file.
-f "/root/.ssh/id_dsa"
第二个里程:在管理服务器上分发公钥给被管理端服务器
-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 利用交互方式实现公钥分发
Are you sure you want to continue connecting (yes/no)? yes --- 确认是否接受连接主机公钥信息
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.
root@172.16.1.41's password: --- 首次连接需要基于口令连接
Warning: Permanently added '172.
.1.41
' (RSA) to the list of known hosts.
--- 进行连接测试,已经可以免密码登录远程主机
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
uptime --- 可以不用登录主机,利用命令直接查看远程主机信息
Last login: Tue Dec 5 12:02:48 2017 from 10.0.0.253
[root@backup ~]# exit 退出当前客服端
[root@m01 ~]# ssh 172.16.1.41
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/
a 临时修改umask值信息为077
authorized_keys && (test -x /sbin/restorecon && /sbin/restorecon .ssh .ssh/authorized_keys >/dev/nu
ll 2>&1 || true)'" || exit 1
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/
a 临时修改umask值信息为077
authorized_keys && (test -x /sbin/restorecon && /sbin/restorecon .ssh .ssh/authorized_keys >/dev/nu
ll 2>&1 || true)'" || exit 1
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脚本命令用法
b第一次远程连接需要基于口令认证sshpass - p 123456 ssh - copy - id - i / root / .ssh / id_dsa.pub "172.16.1.8 -p22 -o StrictHostKeyChecking=no"Now
[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
第三个里程碑:如何实现公钥批量分发,秘钥对自动生成
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.
编写脚本实现公钥批量分发
安装免密码 sshpass
[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
ansible 批量管理服务介绍
软件由 python 语言开发
其功能实现基于 SSH 远程连接服务
可以实现批量系统配置,批量软件部署,批量文件拷贝,批量运行命令等功能
ansible 软件参考资料
说明信息:
ansible 软件相关参考链接信息
2.1 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/
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
说明:才文件用来定义 ansible 可以管理的主机信息(IP 地址或者域名)
vim /etc/ansible/hosts
[oldboy]
172.16.1.8
172.16.1.41
172.16.1.31
变态需求:不想分发 ssh-key 公钥,又想利用 ansible 批量管理
说明:后面的用户和密码项是非必须的,在配置 key 认证的情况下,不使用密码也可以直接操作 .
[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 的,也可以在 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
}
免密码交互方式 + ansible 批量管理服务介绍
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"
来源: http://www.bubuko.com/infodetail-2461718.html