设为首页 收藏本站
查看: 936|回复: 0

[经验分享] 免密码交互方式+ansible批量管理服务介绍

[复制链接]

尚未签到

发表于 2018-7-29 07:57:24 | 显示全部楼层 |阅读模式
介绍了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>  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"

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-542776-1-1.html 上篇帖子: Ansible详解(三) 下篇帖子: ansible任务的异步执行
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表