CentOS/Ubuntu实现批量免密

本文涉及的产品
传统型负载均衡 CLB,每月750个小时 15LCU
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
EMR Serverless StarRocks,5000CU*H 48000GB*H
简介: 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


相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
目录
相关文章
|
6月前
|
Ubuntu 网络安全 数据安全/隐私保护
百度搜索:蓝易云【Ubuntu实现免密登录设置教程】
完成以上步骤后,你将可以在客户端无需输入密码即可登录到服务器。请确保公钥已经正确复制到服务器,并妥善保管私钥,以确保安全性。
56 0
|
6月前
|
Ubuntu Linux
Centos 7、Debian、Ubuntu中tree指令的检查与下载
Centos 7、Debian、Ubuntu中tree指令的检查与下载
|
6月前
|
Kubernetes Ubuntu 安全
Linux|科普扫盲帖|配置网络软件源---阿里云镜像仓库服务使用(centos,Ubuntu)
Linux|科普扫盲帖|配置网络软件源---阿里云镜像仓库服务使用(centos,Ubuntu)
2949 0
|
6月前
|
存储 Ubuntu 应用服务中间件
【Nginx】centos和Ubuntu操作系统下载Nginx配置文件并启动Nginx服务详解
【Nginx】centos和Ubuntu操作系统下载Nginx配置文件并启动Nginx服务详解
95 1
|
1月前
|
Ubuntu 网络协议 Linux
liunx各大发行版(centos,rocky,ubuntu,国产麒麟kylinos)网卡配置和包管理方面的区别
liunx各大发行版(centos,rocky,ubuntu,国产麒麟kylinos)网卡配置和包管理方面的区别
112 0
|
3月前
|
Ubuntu 网络协议 Linux
liunx各大发行版(centos,rocky,ubuntu,国产麒麟kylinos)网卡配置和包管理方面的区别
本文对比了Linux主要发行版CentOS、Rocky Linux、Ubuntu及国产Kylin在网卡配置与包管理上的差异。
227 1
|
3月前
|
Web App开发 缓存 Ubuntu
Linux中yum、rpm、apt-get、wget的区别,yum、rpm、apt-get常用命令,CentOS、Ubuntu中安装wget
Linux中yum、rpm、apt-get、wget的区别,yum、rpm、apt-get常用命令,CentOS、Ubuntu中安装wget
201 11
|
3月前
|
Ubuntu Linux 测试技术
下载ISO镜像的方法 Debian、Red Hat 、CentOS、Ubuntu、Kali Linux🌐
Debian、Red Hat、CentOS、Ubuntu与Kali Linux均为知名Linux发行版。下载Debian须访问官网并按计算机架构选ISO文件。Red Hat下载通常需订阅账户,可从官网登录后获取。CentOS可从官网或镜像站点下载,注意CentOS 8已停更。Ubuntu下载简便,官网直接选取版本及架构即可。Kali Linux专为安全测试设计,官网提供直接下载ISO镜像服务。
733 0
|
3月前
|
Ubuntu Linux 数据安全/隐私保护
在Ubuntu 12.04和CentOS 6上如何添加和删除用户
在Ubuntu 12.04和CentOS 6上如何添加和删除用户
23 0
|
4月前
|
Ubuntu 安全 Linux
Ubuntu与CentOS的区别
Ubuntu与CentOS的区别
71 1