CentOS/Ubuntu实现批量免密

简介: CentOS/Ubuntu实现批量免密

备注

本文所有涉及安装都是用Ubuntu20.04演示,如果需要切换到Redhat/CentOS请自行将所有安装命令更换为yum/dnf install

ansible进行免密操作

安装ansible并初始化

安装

root@harbor:~#  apt update && apt -y install ansible
root@harbor:~# ansible --version
ansible 2.9.6
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.8.10 (default, Jun 22 2022, 20:18:18) [GCC 9.4.0]

修改配置文件

修改资产清单,默认资产清单在/etc/ansible/hosts

root@harbor:~# cat /etc/ansible/hosts
[k8s_node]  #主机组名
master1   #这里如果写主机名需要在/etc/hosts里面做解析
master2
master3
node1
node2
node3
[k8s_node:vars] #这里定义变量的时候需要和组名保持一致
ansible_ssh_user=root
ansible_ssh_pass="***" #你的密码,如果不统一的话可以单独在资产名后面定义

接下来检查一下

root@harbor:~# ansible k8s_node -m ping -o 
master1 | FAILED! => {"msg": "to use the 'ssh' connection type with passwords, you must install the sshpass program"}
master2 | FAILED! => {"msg": "to use the 'ssh' connection type with passwords, you must install the sshpass program"}
node1 | FAILED! => {"msg": "to use the 'ssh' connection type with passwords, you must install the sshpass program"}
master3 | FAILED! => {"msg": "to use the 'ssh' connection type with passwords, you must install the sshpass program"}
node2 | FAILED! => {"msg": "to use the 'ssh' connection type with passwords, you must install the sshpass program"}
node3 | FAILED! => {"msg": "to use the 'ssh' connection type with passwords, you must install the sshpass program"}

出现报错了,不过给了提示信息,我们安装上sshpass再试试

root@harbor:~# apt -y install sshpass
root@harbor:~# ansible k8s_node -m ping -o 
master1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": false,"ping": "pong"}
node1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": false,"ping": "pong"}
master2 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": false,"ping": "pong"}
node2 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": false,"ping": "pong"}
master3 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": false,"ping": "pong"}
node3 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": false,"ping": "pong"}

正常第一次使用模块可能会有如下提示

root@harbor:~# ansible lb -m ping -o
The authenticity of host 'lb.org ' can't be established.
ECDSA key fingerprint is SHA256:Vg7I4oCxTSrst9ztoQworwFW4/lj/gF+wMqnGKmw5bM.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

需要跳过的话可以编辑/etc/ansible/ansible.cfg

host_key_checking = False  #找到这行,去掉原本的注释

ansible批量免密资产

这里我是用ansible自己来免密别人

生成密钥

root@harbor:~# ssh-keygen -t rsa -b 2048 -P '' -q -f .ssh/id_rsa
root@harbor:~# ls .ssh/
id_rsa  id_rsa.pub

生产密钥之后只需要想办法将公钥送到被免密主机的.ssh/authorized_keys中

初次免密

正常如果没有被免密的主机其实很简单,只需要执行下面的playbook即可

---
- hosts: lb
  name: first Trust
  tasks:
  - name: mkdir .ssh
    file:
      path: /root/.ssh
      state: directory
  - name: copy key
    copy:
      src: /root/.ssh/id_rsa.pub
      dest: /root/.ssh/authorized_keys

执行一下

root@harbor:~# ansible-playbook first.yml 
PLAY [first Trust] ******************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************
ok: [lb.org]
TASK [mkdir .ssh] *******************************************************************************************************************************
changed: [lb.org]
TASK [copy key] *********************************************************************************************************************************
changed: [lb.org]
PLAY RECAP **************************************************************************************************************************************
lb.org                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
root@harbor:~# ssh lb.org
*** System restart required ***
Last login: Tue Nov  1 15:07:22 2022 from 10.10.21.174
root@lb:~# 

免密成功

第N次免密(N > 1)

这里指的是如果对方已经被别人做过免密的情况,那我们就不能直接用copy模块了,而是需要想办法将自己的公钥拼接到原来已有的authorized_keys中,剧本如下:

---
- hosts: k8s_node
  name: last Trust
  gather_facts: no
  tasks:
  - name: copy key
    copy:
      src: /root/.ssh/id_rsa.pub
      dest: /tmp/authorized_keys.tmp
  - name: trust
    shell: cat /tmp/authorized_keys.tmp >> /root/.ssh/authorized_keys
  - name: delete /tmp/authorized_keys.tmp
    file:
      path: /tmp/authorized_keys.tmp
      state: absent

执行并验证

root@harbor:~# ansible-playbook last.yml 
PLAY [last Trust] *******************************************************************************************************************************
TASK [copy key] *********************************************************************************************************************************
changed: [node1]
changed: [master3]
changed: [master1]
changed: [master2]
changed: [node2]
changed: [node3]
TASK [trust] ************************************************************************************************************************************
changed: [node2]
changed: [node1]
changed: [master1]
changed: [master2]
changed: [master3]
changed: [node3]
TASK [delete /tmp/authorized_keys.tmp] **********************************************************************************************************
changed: [node1]
changed: [node2]
changed: [master1]
changed: [master2]
changed: [master3]
changed: [node3]
PLAY RECAP **************************************************************************************************************************************
master1                    : ok=3    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
master2                    : ok=3    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
master3                    : ok=3    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node1                      : ok=3    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node2                      : ok=3    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node3                      : ok=3    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
root@harbor:~# ssh master1
*** System restart required ***
Last login: Tue Nov  1 15:21:31 2022 from 10.10.21.174

ansible批量协助其他机器进行免密

这里我的lb来免密其他的机器

---
- hosts: all
  name: last Trust
  tasks:
  - name: touch id_rsa
    shell: ssh-keygen -t rsa -b 2048 -P '' -q -f .ssh/id_rsa
    when: inventory_hostname in groups.lb
    #如果lb已经生成过密钥,记得注释上面的task
  - name: copy key
    fetch:
      src: /root/.ssh/id_rsa.pub
      dest: /tmp/
    when: inventory_hostname in groups.lb
  - name: copy key to  other hosts
    copy:
      src: /tmp/lb.org/root/.ssh/id_rsa.pub   #这个路径是/tmp拼接lb的公钥路径
      dest: /tmp/authorized_keys.tmp
    when: inventory_hostname in groups.k8s_node
  - name: trust
    shell: cat /tmp/authorized_keys.tmp >> /root/.ssh/authorized_keys
    when: inventory_hostname in groups.k8s_node
  - name: delete /tmp/authorized_keys.tmp
    file:
      path: /tmp/authorized_keys.tmp
      state: absent
    when: inventory_hostname in groups.k8s_node

ansible-playbook执行一下即可完成免密

shell脚本进行免密操作

利用expect和spawn进行免密

下面这个脚本可以直接对/etc/hosts中记录了的host进行免密

root@harbor:~# apt -y install expect
root@harbor:~# cat ssh-ssh.sh 
#!/bin/bash
ssh-keygen -t rsa -b 2048 -P "" -f /root/.ssh/id_rsa -q
for host in  `awk '{print $1}' /etc/hosts`;do
    expect -c "
    spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@${host}
        expect {
                *yes/no* {send -- "yes\r"; exp_continue}
                *assword* {send xxxx\r; exp_continue} #xxxx对应自己的密码
               }"
done
#xxxx对应自己的密码
root@harbor:~# sh ssh-ssh.sh

利用sshpass进行免密

root@harbor:~# apt -y install sshpass
root@harbor:~# cat ssh-ssh.sh 
#!/bin/bash
ssh-keygen -t rsa -b 2048 -P "" -f /root/.ssh/id_rsa -q
# export SSHPASS=xxxx #xxxx对应自己的密码,必须要提前声明,否则会报错
for host in  `awk '{print $1}' /etc/hosts`;do
   # sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $host
    sshpass -p 123456 ssh-copy-id -o StrictHostKeyChecking=no $host
done
root@harbor:~# sh ssh-ssh.sh


相关实践学习
深入解析Docker容器化技术
Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。Docker是世界领先的软件容器平台。开发人员利用Docker可以消除协作编码时“在我的机器上可正常工作”的问题。运维人员利用Docker可以在隔离容器中并行运行和管理应用,获得更好的计算密度。企业利用Docker可以构建敏捷的软件交付管道,以更快的速度、更高的安全性和可靠的信誉为Linux和Windows Server应用发布新功能。 在本套课程中,我们将全面的讲解Docker技术栈,从环境安装到容器、镜像操作以及生产环境如何部署开发的微服务应用。本课程由黑马程序员提供。     相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
目录
相关文章
|
7月前
|
Ubuntu 安全 Linux
CentOS与Ubuntu的深度对比与分析
Ubuntu更新频繁、文档丰富,适用于云服务与容器部署。 与CentOS的比较,Ubuntu基于Debian,而CentOS则源自RHEL。在软件包格式上,Ubuntu采用.deb和.snap,而CentOS则使用.rpm和flatpak。更新方面,Ubuntu使用apt,而CentOS则依赖yum。尽管CentOS以稳定性见长,不常对包进行更新,但这并不意味着Ubuntu在安全性上逊色。事实上,Ubuntu提供了更为丰富的文档和免费的技术支持。此外,Ubuntu的服务器版本在云服务和容器部署方面拥有更多的优势。
|
7月前
|
Ubuntu Linux 索引
Centos 7、Debian及Ubuntu系统中安装和验证tree命令的指南。
通过上述步骤,我们可以在CentOS 7、Debian和Ubuntu系统中安装并验证 `tree`命令。在命令行界面中执行安装命令,然后通过版本检查确认安装成功。这保证了在多个平台上 `tree`命令的一致性和可用性,使得用户无论在哪种Linux发行版上都能使用此工具浏览目录结构。
606 78
|
7月前
|
Ubuntu 安全 Unix
CentOS 与 Ubuntu 谁与争锋
不论你的选择如何,是 Ubuntu 还是 CentOS,两者都是非常优秀稳定的发行版。如果你想要一个发布周期更短的版本,那么就选 Ubuntu;如果你想要一个不经常变更包的版本,那么就选 CentOS。在下方留下的评论,说出你更钟爱哪一个吧!
|
7月前
|
Ubuntu 安全 小程序
服务器版本的CentOS和Ubuntu哪个更适合你?
但是以上的比较并不说明Ubuntu是不稳定的或者是不安全的,只是以上比较过程中,在稳定性方面Ubuntu稍微逊色了一点。由于Ubuntu在个人桌面电脑的使用率远远高于CentOS,用Ubuntu搭建服务器,如果遇到什么问题,寻找解决方案相对比较容易,这让Ubuntu在选择方面更优于CentOS。如果你是一个初学者,那么毫无疑问Ubuntu是更适合的选择。如果你正在经营自己的公司,在这两者之间,CentOS会更好一些。
|
7月前
|
Ubuntu 安全 Linux
centos和ubuntu有什么区别
总的来说,CentOS 更适合用于服务器和企业级应用,因为它稳定、可靠、安全,并且提供长期支持。而 Ubuntu 则更适合用于桌面应用程序和开发环境,因为它更加注重用户体验和新技术支持。
|
7月前
|
Ubuntu Linux 图形学
centos和ubuntu有什么区别
总之,CentOS和Ubuntu都是常见的Linux操作系统发行版,它们都是免费的、开源的操作系统。它们在更新周期、软件包管理器、默认桌面环境、用户接口和社区支持等方面存在一些不同。因此,选择哪种操作系统取决于用户的需求和偏好,以及特定的使用场景。 如果有任何疑问可以随时评论留言或私信我,欢迎关注我[点击关注],共同探讨。
|
7月前
|
Ubuntu 安全 Linux
CentOS和Ubuntu区别
好的选择。 Ubuntu有一个很大的社区平台,可以提供丰富的文档和经验。 Ubuntu服务器的图形界面适合大多数人的习惯。 因此,如果是初学者且没有特别的要求,就使用Ubuntu服务器吧。 CentOS适用于公司的生产环境,Centos的更新频率不高,仅发布稳定的版本。 网上项目教程大多基于Centos。 Ubuntu面向初学者,CentOS面向公司服务器。
|
7月前
|
Ubuntu 安全 数据挖掘
揭开Linux系统神秘面纱,选择Centos、Debian、Ubuntu?
CentOS、Debian 和 Ubuntu 三种 Linux 操作系统各具优势和适用场景。CentOS 更适合用于服务器应用,Debian 更适合稳定需求的系统环境,而 Ubuntu 更适合用于桌面操作系统和开发环境等。CentOS 和 Debian 相对保守,重视稳定性和安全性;Ubuntu 侧重更新和更好的可用性,重视用户体验。此外, Ubuntu 在市场上的占有率最高。因此,选择适合自己需求的操作系统非常重要,可以帮助用户提高效率和使用体验。
|
7月前
|
存储 Ubuntu 安全
Linux中Centos和Ubuntu的区别
CentOS主要面向服务器环境,而Ubuntu适用于服务器和桌面环境。   CentOS提供更精简的安装,而Ubuntu提供更广泛的开箱即用功能。   CentOS遵循RHEL的所有安全实践,而Ubuntu在安全方面采取更积极的方法。
|
7月前
|
Ubuntu Linux 云计算
CentOS与Ubuntu:Linux系统的双璧
选择Ubuntu还是CentOS,取决于用户的具体需求,如是否需要图形化界面、对稳定性的要求、软件包管理的偏好以及对商业支持的需求等。两者都是优秀的Linux发行版,只是在设计理念和目标用户群体上有所不同。#深度好文计划#