-
SSH 远程管理服务概念介绍
SSH 安全的远程连接 数据是加密的 22 SSH服务默认可以用root用户远程连接
telnet 不安全的远程连接 数据信息是明文的 23 telnet服务默认是不可以让root用户远程连接 -
SSH工作连接工作原理(数据加密)
私钥: 钥匙
公钥: 锁头
第一个步骤: 客户端 执行远程连接命令
第二个步骤: 客户端 服务端 建立三次握手过程
第三个步骤: 服务端 让客户端进行确认是否接收服务端公钥信息
第四个步骤: 客户端 进行公钥确认,接收到公钥信息
第五个步骤: 服务端 让客户端确认登录用户密码信息
第六个步骤: 客户端 进行密码信息确认
第七个步骤: 客户端 服务端 远程连接建立成功私钥和公钥作用:
- 利用私钥和公钥对数据信息进行加密处理
- 利用公钥和私钥进行用户身份认证
3.SSH 远程连接的方式
a 基于秘钥口令的方式远程连接 连接比较麻烦 连接不太安全
b 基于秘钥的方式进行远程连接 连接方便 连接比较安全
基于秘钥方式连接过程(原理)
1. 客户端(管理端) 执行命令创建秘钥对
2. 客户端(管理端) 建立远程连接(口令),发送公钥信息
3. 客户端(管理端) 再次建立远程连接
4. 服务端(被管理端) 发送公钥质询信息(你要是能打开我的锁头吗)
5. 客户端(管理端) 处理公钥质询信息(钥匙将锁头打开),将质询结果返回给服务端
6. 服务端(被管理端) 接收到质询结果,建立好远程连接
4.SSH实现基于秘钥连接点的部署步骤
准备工作: 准备一台管理服务器
01 第一个步骤:管理端创建秘钥对信息
[root@m01 ~]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Created directory ‘/root/.ssh’.
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. ssh-keygen -t dsa
02 第二个步骤:管理端需要将公钥进行分发
[root@m01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@172.16.1.41
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/root/.ssh/id_dsa.pub”
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed – if you are prompted now it is to install the new keys
root@172.16.1.41’s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@172.16.1.41'"
and check to make sure that only the key(s) you wanted were added.
03 第三个步骤:进行远程测试连接
[root@m01 ~]# ssh 172.16.1.41
Last login: Wed Apr 14 22:09:31 2021 from 172.16.1.61
[root@backup ~]#
补充: 如何批量管理多台主机,如何实现无交互进行远程连接分发公钥
01 第一个步骤: 安装免交互方式分发公钥命令 sshpass
yum install sshpass
02 编写分发秘钥脚本
#!/bin/bash
for ip in {7,8,9,31,41,71}
do
echo "=======================host 172.16.1.$ip fenfa_pub start==========================="
sshpass -f /etc/ssh.password ssh-copy-id -i /root/.ssh/id_dsa.pub root@172.16.1.$ip "-o StrictHostKeyChecking=no" &>/dev/null
echo -e "host 172.16.1.$ip fenfa success."
echo "========================host 172.16.1.$ip fenfa_pub end============================="
echo ""
done
03 编写远程主机用户密码文件 (密码一致)
echo "123456" > /etc/ssh.password
chmod 600 /etc/ssh.password
04 编写测试脚本
#!/bin/bash
for ip in {7,8,9,31,41,71}
do
echo "=======================host 172.16.1.$ip check_pub start==========================="
ssh 172.16.1.$ip hostname
echo "========================host 172.16.1.$ip fenfa_pub end============================="
echo ""
done
05 执行脚本
sh 脚本文件
5 SSH服务配置文件主要参数 /etc/ssh/sshd_config
/etc/ssh/sshd_config
Port 22 — 修改服务端口信息
ListenAddress 0.0.0.0 — 监听地址 指定一块网卡能够接受远程访问请求 *****
PS: 指定监听地址只能是本地网卡上有的地址
PermitEmptyPasswords no — 是否允许远程用户使用空密码登录,默认不允许
PermitRootLogin yes — 是否禁止root用户远程连接主机 建议改为no
GSSAPIAuthentication no — 是否开启GSSAPI认证功能 不用的时候关闭
UseDNS no — 是否开启反向DNS解析功能
6 ansible批量管理服务介绍
ansible批量管理服务意义
01 提高工作效率
02 提高工作的准确度
03 减少维护成本
04 减少重复性工作
ansible批量管理服务功能
01 可以实现批量系统操作配置
02 可以实现批量软件服务部署
03 可以实现批量文件数据分发
04 可以实现批量系统信息收集
7 ansible批量管理服务部署
管理服务器
01 第一个步骤 安装部署软件(需要配置好epel的yum源)
yum install -y ansible
02 第二个步骤: 需要编写主机清单文件
vim /etc/ansible/hosts
[rsync]
172.16.1.
[nfs]
172.16.1.31
[web]
172.16.1.7
172.16.1.8
172.16.1.9
[rsync:children]
rsync_server
rsync_client
[rsync_server]
172.16.1.41
[rsync_client]
172.16.1.8
[nfs]
172.16.1.31
[nfs:children]
nfs_server
nfs_client
[nfs_server]
172.16.1.31
[nfs_client]
172.16.1.8
172.16.1.41
03 第三个步骤测试是否可以管理多个主机
[root@m01 scripts]# ansible all -a "hostname"
172.16.1.31 | CHANGED | rc=0 >>
nfs01
172.16.1.41 | CHANGED | rc=0 >>
backup
172.16.1.8 | CHANGED | rc=0 >>
web02
172.16.1.9 | CHANGED | rc=0 >>
web03
172.16.1.7 | CHANGED | rc=0 >>
web01
8 ansible模块应用
ansible官方网站: https://docs.ansible.com/
模块的应用语法格式:
ansible 主机名称/主机组名称/主机地址信息/all -m(指定应用的模块信息) 模块名称 -a(指定动作信息) “执行什么动作”
命令类型模块:
掌握第一个模块: command (默认模块)
command – Executes a command on a remote node
在一个远程主机上执行一个命令
简单用法:
[root@m01 scripts]# ansible 172.16.1.31 -m command -a "hostname"
172.16.1.31 | CHANGED | rc=0 >>
nfs01
扩展应用:
1) chdir Change into this directory before running the command.
在执行命令之前对目录进行切换
ansible 172.16.1.31 -m command -a "chdir=/tmp touch oldboy.txt"
2) creates If it already exists, this step won't be run.
如果文件存在了,不执行命令操作
ansible 172.16.1.31 -m command -a "creates=/tmp/hosts touch oldboy.txt"
3) removes If it already exists, this step will be run.
如果文件存在了, 这个步骤将执行
ansible 172.16.1.31 -m command -a "removes=/tmp/hosts chdir=/tmp touch oldboy.txt"
4) free_form(required)
The command module takes a free form command to run.
There is no parameter actually named 'free form'. See the examples!
使用command模块的时候,-a参数后面必须写上一个合法linux命令信息
注意事项:
有些符号信息无法识别: <", ">", "|", ";" and "&"
掌握第二个模块: shell (万能模块)
shell – Execute commands in nodes
在节点上执行操作
简单用法:
[root@m01 scripts]# ansible 172.16.1.31 -m shell-a "hostname"
172.16.1.31 | CHANGED | rc=0 >>
nfs01
扩展应用:
1) chdir Change into this directory before running the command.
在执行命令之前对目录进行切换
ansible 172.16.1.31 -m shell -a "chdir=/tmp touch oldboy.txt"
2) creates If it already exists, this step won't be run.
如果文件存在了,不执行命令操作
ansible 172.16.1.31 -m shell -a "creates=/tmp/hosts touch oldboy.txt"
3) removes If it already exists, this step will be run.
如果文件存在了, 这个步骤将执行
ansible 172.16.1.31 -m shell -a "removes=/tmp/hosts chdir=/tmp touch oldboy.txt"
4) free_form(required)
The command module takes a free form command to run.
There is no parameter actually named 'free form'. See the examples!
使用command模块的时候,-a参数后面必须写上一个合法linux命令信息
实践应用: 利用shell执行脚本
第一个步骤: 编写一个脚本
#!/bin/bash
yum install -y htop
第二个步骤: 将脚本发送到远程主机
scp -rp 172.16.1.31
第三个步骤: 将脚本权限进行修改(添加执行权限)
chmod +x /server/scripts/yum.sh
第四个步骤: 运行ansible命令执行脚本
掌握第三个模块: script (万能模块)
第一个步骤: 编写一个脚本
第二个步骤: 运行ansible命令执行脚本
PS: scripts模块参数功能和command模块类似
文件类型模块:
copy – Copies files to remote locations
将数据信息进行批量分发
基本用法:
ansible 172.16.1.31 -m copy -a "src=/etc/hosts dest=/etc/"
172.16.1.31 | CHANGED => { --- 对哪台主机进行操作
"changed": true, --- 是否对主机信息进行改变
"checksum": "6ed7f68a1d6b4b36c1418338b2001e421eeba270", --- 生成一个文件校验码==MD5数值
"dest": "/etc/hosts", --- 显示目标路径信息
"gid": 0, --- 显示复制后文件gid信息
"group": "root", --- 显示复制后文件属组信息
"md5sum": "7afd7b74854f0aaab646b3e932f427c0", --- 生成一个文件校验码==MD5数值
"mode": "0644", --- 显示复制后文件权限信息
"owner": "root", --- 显示复制后文件属主信息
"size": 401, --- 显示文件的大小信息
"src": "/root/.ansible/tmp/ansible-tmp-1557804498.23-26487341925325/source",
"state": "file", --- 显示文件的类型信息
"uid": 0 --- 显示复制后文件uid信息
}
补充说明: ansible软件输出颜色说明:
01. 绿色信息: 查看主机信息/对主机未做改动
02. 黄色信息: 对主机数据信息做了修改
03. 红色信息: 命令执行出错了
04. 粉色信息: 忠告信息
05. 蓝色信息: 显示ansible命令执行的过程???
扩展用法:
01. 在传输文件时修改文件的属主和属组信息
ansible 172.16.1.31 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ owner=oldboy group=oldboy"
02. 在传输文件时修改文件的权限信息
ansible 172.16.1.31 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ mode=1777"
03. 在传输数据文件信息时对远程主机源文件进行备份
ansible 172.16.1.31 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ backup=yes"
04. 创建一个文件并直接编辑文件的信息
ansible 172.16.1.31 -m copy -a "content='oldboy123' dest=/etc/rsync.password"
自行研究: remote_src directory_mode local_follow
If no, it will search for src at originating/master machine.
src参数指定文件信息,会在本地管理端服务进行查找
If yes it will go to the remote/target machine for the src. Default is no.
src参数指定文件信息,会从远程主机上进行查找
PS: ansible软件copy模块复制目录信息
ansible 172.16.1.31 -m copy -a " =/oldboy dest=/oldboy"
src后面目录没有/: 将目录本身以及目录下面的内容都进行远程传输复制
ansible 172.16.1.31 -m copy -a "src=/oldboy/ dest=/oldboy"
src后面目录有/: 只将目录下面的内容都进行远程传输复制
file – Sets attributes of files
设置文件属性信息
基本用法:
ansible 172.16.1.31 -m file -a "dest=/etc/hosts owner=oldboy group=oldboy mode=666"
扩展用法:
1. 可以利用模块创建数据信息 (文件 目录 链接文件)
state 参数
=absent --- 缺席/删除数据信息
=directory --- 创建一个目录信息
=file --- 检查创建的数据信息是否存在 绿色存在 红色不存在
=hard --- 创建一个硬链接文件
=link --- 创建一个软链接文件
=touch --- 创建一个文件信息
创建目录信息:
ansible 172.16.1.31 -m file -a "dest=/oldboy/ state=directory"
ansible 172.16.1.31 -m file -a "dest=/oldboy/oldboy01/oldboy02/ state=directory"
创建文件信息:
ansible 172.16.1.31 -m file -a "dest=/oldboy/oldboy.txt state=touch"
创建链接文件信息:
ansible 172.16.1.31 -m file -a "src=/oldboy/oldboy.txt dest=/oldboy/oldboy_hard.txt state=hard"
ansible 172.16.1.31 -m file -a "src=/oldboy/oldboy.txt dest=/oldboy/oldboy_link.txt state=link"
2. 可以利用模块删除数据信息
ansible 172.16.1.31 -m file -a "dest=/oldboy/oldboy.txt state=absent"
ansible 172.16.1.31 -m file -a "dest=/oldboy/ state=absent"
yum模块
name --- 指定安装软件名称
state --- 指定是否安装软件
installed --- 安装软件
present
latest
absent --- 卸载软件
removed
ansible 172.16.1.31 -m yum -a "name=iotop state=installed"
service模块: 管理服务器的运行状态 停止 开启 重启
name: --- 指定管理的服务名称
state: --- 指定服务状态
started 启动
restarted 重启
stopped 停止
enabled --- 指定服务是否开机自启动
ansible 172.16.1.31 -m service -a "name=nfs state=started enabled=yes"
cron模块: 批量设置多个主机的定时任务信息
crontab -e
* * * * * 定时任务动作
分 时 日 月 周
minute: # Minute when the job should run ( 0-59, *, */2, etc )
设置分钟信息
hour: # Hour when the job should run ( 0-23, *, */2, etc )
设置小时信息
day: # Day of the month the job should run ( 1-31, *, */2, etc )
设置日期信息
month: # Month of the year the job should run ( 1-12, *, */2, etc )
设置月份信息
weekday: # Day of the week that the job should run ( 0-6 for Sunday-Saturday, *, etc )
设置周信息
job 用于定义定时任务需要干的事情
基本用法:
ansible 172.16.1.31 -m cron -a "minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'"
扩展用法:
01. 给定时任务设置注释信息
ansible 172.16.1.31 -m cron -a "name='time sync' minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'"
02. 如何删除指定定时任务
ansible 172.16.1.31 -m cron -a "name='time sync01' state=absent"
PS: ansible可以删除的定时任务,只能是ansible设置好的定时任务
03. 如何批量注释定时任务
ansible 172.16.1.31 -m cron -a "name='time sync' job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1' disabled=yes"
mount: 批量进行挂载操作
src: 需要挂载的存储设备或文件信息
path: 指定目标挂载点目录
fstype: 指定挂载时的文件系统类型
state
present/mounted --- 进行挂载
present: 不会实现立即挂载,修改fstab文件,实现开机自动挂载
mounted: 会实现立即挂载, 并且会修改fstab文件,实现开机自动挂载 *****
absent/unmounted --- 进行卸载
absent: 会实现立即卸载, 并且会删除fstab文件信息,禁止开机自动挂载
unmounted: 会实现立即卸载, 但是不会会删除fstab文件信息 *****
user模块: 实现批量创建用户
基本用法:
ansible 172.16.1.31 -m user -a "name=oldboy01"
扩展用法:
1) 指定用户uid信息
ansible 172.16.1.31 -m user -a "name=oldboy02 uid=6666"
2) 指定用户组信息
ansible 172.16.1.31 -m user -a "name=oldboy03 group=oldboy02"
ansible 172.16.1.31 -m user -a "name=oldboy04 groups=oldboy02"
3) 批量创建虚拟用户
ansible 172.16.1.31 -m user -a "name=rsync create_home=no shell=/sbin/nologin"
4) 给指定用户创建密码
PS: 利用ansible程序user模块设置用户密码信息,需要将密码明文信息转换为密文信息进行设置
生成密文密码信息方法:
方法一:
ansible all -i localhost, -m debug -a "msg={{ '密码信息123456' | password_hash('sha512', 'oldboy') }}"
[root@m01 tmp]# ansible all -i localhost, -m debug -a "msg={{ '123456' | password_hash('sha512', 'oldboy') }}"
localhost | SUCCESS => {
"msg": "$6$oldboy$MVd3DevkLcimrBLdMICrBY8HF82Wtau5cI8D2w4Zs6P1cCfMTcnnyAmmJc7mQaE9zuHxk8JFTRgYMGv9uKW7j1"
}
方法二:(忽略)
mkpasswd --method=sha-512
方法三:
yum install -y python-pip
pip install passlib
python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"
Password:
$6$rJJeiIerQ8p2eR82$uE2701X7vY44voF4j4tIQuUawmTNHEZhs26nKOL0z39LWyvIvZrHPM52Ivu9FgExlTFgz1VTOCSG7KhxJ9Tqk.
ansible 172.16.1.31 -m user -a 'name=oldboy08 password=$6$oldboy$MVd3DevkLcimrBLdMICrBY8HF82Wtau5cI8D2w4Zs6P1cCfMTcnnyAmmJc7mQaE9zuHxk8JFTRgYMGv9uKW7j1'
9 ansible剧本自动化部署rsync,nfs服务
01 剧本目录结构
/etc/ansible/roles/
├── nfs-client
│ ├── files
│ ├── headlers
│ ├── tasks
│ │ ├── check-mount-info.yml
│ │ ├── client-boot-nfs.yml
│ │ ├── client-yum-nfs.yml
│ │ ├── main.yml
│ │ └── mount-nfs.yml
│ ├── templates
│ └── vars
│ └── main.yml
├── nfs-server
│ ├── files
│ │ └── exports
│ ├── handlers
│ │ └── main.yml
│ ├── tasks
│ │ ├── boot-server.yml
│ │ ├── create-data-dir.yml
│ │ ├── main.yml
│ │ ├── push-conf-info.yml
│ │ └── yum-rpc-nfs.yml
│ ├── templates
│ │ └── exports
│ └── vars
│ └── main.yml
├── rsync-client
│ ├── files
│ ├── headlers
│ ├── tasks
│ │ ├── create-rsync-passwrod.yml
│ │ ├── main.yml
│ │ └── rsync-test.yml
│ ├── templates
│ └── vars
├── rsync-server
│ ├── files
│ ├── handlers
│ │ └── main.yml
│ ├── tasks
│ │ ├── boot-rsync-server.yml
│ │ ├── create-backup-dir.yml
│ │ ├── create-rsync-passwrd.yml
│ │ ├── create-user.yml
│ │ ├── main.yml
│ │ ├── push-conf.yml
│ │ └── yum-rsync.yml
│ ├── templates
│ │ └── rsyncd.conf
│ └── vars
│ └── main.yml
└── site.yml
02 编写剧本文件
/etc/ansible/roles/nfs-server/tasks
/etc/ansible/roles/nfs-server/vars
/etc/ansible/roles/nfs-server/templates
/etc/ansible/roles/nfs-server/handlers
/etc/ansible/roles/nfs-client/tasks
/etc/ansible/roles/nfs-client/vars
/etc/ansible/roles/rsync-server/tasks
/etc/ansible/roles/rsync-server/vars
/etc/ansible/roles/rsync-server/templates
[root@m01 templates]# cat rsyncd.conf
# rsyncd.conf start
# 指定管理备份目录的用户
uid = rsync
# 指定管理备份目录的用户组
gid = rsync
# 定义rsync备份服务的网络端口号
port= {{ PORT_INFO }}
# 伪装称为超级用户 root
fake super = yes
user chroot = no
#定义最大连接数 同时只能有200个客户端连接到备份服务器
max connections = 200
#设置 超时时间(单位秒)
timeout = 300
# 记录进程号码信息 1.让程序快速停止进程 2. 判断一个服务是否正在运行
pid file = /var/run/rsyncd.pid
# 锁文件 控制最大连接数
lock file = /var/run/rsync.lock
# rsync服务的日志文件
log file = /var/log/rsync.log
# 忽略传输中的简单错误
ignore errors
# 指定备份目录是可读可写
read only = false
# 使客户端可以查看服务端的模块信息
list = false
# 允许传输备份数据的主机(白名单)
hosts allow = 172.16.1.0/24
# 禁止传输备份数据的主机(黑名单)
hosts deny = 0.0.0.0/32
# 指定认证用户
auth users = rsync_backup
# 指定认证用户密码文件 用户名称:密码信息
secrets file = /etc/rsync.password
# 模块信息
[backup]
comment = "backup dir by oldboy"
# 模块中配置参数 指定备份目录
path = /backup
[dev]
comment = "devdata dir by oldboy"
# 模块中配置参数 指定备份目录
path = /devdata
[dba]
comment = "dba dir by oldboy"
# 模块中配置参数 指定备份目录
path = /dba
/etc/ansible/roles/rsync-server/handlers
/etc/ansible/roles/rsync-client/tasks
/etc/ansible/roles
检查剧本语法
ansible-playbook --syntax-check site.yml
执行剧本
ansible-playbook site.yml