声明:本文仅作学习交流使用,引用需标明出处。
如有谬误,敬请指正
本次内容围绕Linux运维核心技能scp远程拷贝、源码编译安装、rsync同步、inotify+rsync实时同步 展开,实验基于两台互通虚拟机(client:192.168.8.100/24、server:192.168.8.101/24),所有操作均要求关闭firewalld服务、将SELinux设为disabled。
一、scp远程拷贝
1. 核心概述
scp是Linux下基于SSH协议的安全远程复制命令 (scp = ssh + cp),支持本地与远程主机间加密传输文件/目录;继承SSH特性,支持口令认证 (输入密码)和无密码认证 (SSH密钥);
核心选项:
-r:递归拷贝,拷贝目录时必须使用;-P(大写):指定远程服务器SSH端口号(默认22可省略)。
2. 实验环境准备(client+server主机均执行)
(1)client主机配置
bash
[root@localhost ~]# hostnamectl set-hostname client #配置主机名(退出窗口重新进入生效)
[root@client ~]# nmcli connection modify ens160 ipv4.method manual ipv4.addresses 192.168.8.100/24 connection.autoconnect yes
[root@client ~]# nmcli connection up ens160
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/6)
[root@client ~]# ifconfig ens160 | head -2
[root@client ~]# systemctl disable firewalld #关闭firewalld服务开机自启
[root@client ~]# systemctl stop firewalld #停止firewalld服务
[root@client ~]# vim /etc/selinux/config #修改SElinux配置文件
# 配置文件中修改:SELINUX=disabled
[root@client ~]# reboot #重启操作系统,使SELinux配置生效
[root@client ~]# getenforce #查看SELinux状态,确保为disabled状态
[root@client ~]# systemctl status firewalld #查看firewalld状态,确保为停止状态
(2)server主机配置
bash
[root@localhost ~]# hostnamectl set-hostname server #配置主机名(退出窗口重新进入生效)
[root@server ~]# nmcli connection modify ens160 ipv4.method manual ipv4.addresses 192.168.8.101/24 connection.autoconnect yes
[root@server ~]# nmcli connection up ens160
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/6)
[root@server ~]# ifconfig ens160 | head -2
[root@server ~]# systemctl disable firewalld #关闭firewalld服务开机自启
[root@server ~]# systemctl stop firewalld #停止firewalld服务
[root@server ~]# vim /etc/selinux/config #修改SElinux配置文件
# 配置文件中修改:SELINUX=disabled
[root@server ~]# reboot #重启操作系统,使SELinux配置生效
[root@server ~]# getenforce #查看SELinux状态,确保为disabled状态
[root@server ~]# systemctl status firewalld #查看firewalld状态,确保为停止状态
3. scp上传(本地→远程,client主机操作)
核心格式 :scp [选项] 本地文件或目录 用户@远程主机:/远程主机目标目录
bash
[root@client ~]# scp /etc/hostname root@192.168.8.101:/opt #远程拷贝单文件
root@192.168.8.101's password: 123456 #输入密码不显示,直接回车
[root@server ~]# ls /opt #查看server主机/opt下的数据
[root@client ~]# scp /etc/shells root@192.168.8.101:/opt #远程拷贝单文件
[root@client ~]# scp -r /boot root@192.168.8.101:/opt #远程拷贝目录(必加-r)
[root@client ~]# scp /etc/passwd /etc/group root@192.168.8.101:/opt #远程批量拷贝多文件
[root@server ~]# ls /opt #查看server主机/opt/里的内容
4. scp下载(远程→本地,client主机操作)
核心格式 :scp [选项] 用户@远程主机:/远程主机文件或目录 本地目标目录
bash
[root@client ~]# scp root@192.168.8.101:/etc/hostname /opt #远程拷贝单文件到本地
root@192.168.8.101's password: 123456 #输入密码不显示,直接回车
[root@client ~]# ls /opt #查看client主机/opt下的数据
[root@client ~]# scp root@192.168.8.101:/etc/shells /mnt #远程拷贝单文件到本地/mnt
[root@client ~]# scp -r root@192.168.8.101:/boot /mnt #远程拷贝目录到本地(必加-r)
[root@client ~]# scp root@192.168.8.101:/etc/passwd root@192.168.8.101:/etc/group /opt #远程批量拷贝多文件到本地
[root@client ~]# ls /mnt #查看client主机/mnt里的内容
5. 核心小结
- 掌握scp上传 (本地→远程 )和下载 (远程→本地 )两大核心场景,牢记目录拷贝必须加
-r; - 口令认证时密码输入无回显,直接输入后回车即可;
- scp基于SSH协议加密传输,保证数据传输安全性。
二、源码编译安装
1. 核心优势
- 可获取软件最新版本,及时修复官方BUG,源码包多为压缩包格式;
- 软件功能可按需选择/定制,可选功能比预编译包(如rpm)更丰富;
- 源码包跨平台性强,适用于各种Linux发行版;
- 所有rpm预编译包均由源码包编译而来,最新源码包可从软件官方网站下载。
2. 前置准备:配置本地YUM仓库(client主机)
为安装编译依赖工具,需配置本地光盘YUM仓库,实现光盘永久挂载,步骤如下:
bash
[root@client ~]# mkdir /mydvd #创建光盘挂载点
[root@client ~]# vim /etc/fstab #编写配置文件,实现光盘永久挂载
# 在配置文件最后一行下方追加:/dev/cdrom /mydvd iso9660 defaults 0 0
[root@client ~]# mount -a #刷新配置文件,使挂载生效
[root@client ~]# df -h #查看挂载信息,验证光盘挂载成功
[root@client ~]# mkdir /etc/yum.repos.d/bak #创建YUM配置文件备份目录
[root@client ~]# mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak #移动系统自带YUM客户端文件至备份目录
[root@client ~]# vim /etc/yum.repos.d/mydvd.repo #编写本地YUM仓库配置文件
# 配置文件完整内容:
[AppStream] #仓库名称AppStream
name=AppStream #描述信息
baseurl=file:///mydvd/AppStream #指定YUM仓库repodata位置,file://表示本地协议
enabled=1 #启用仓库
gpgcheck=0 #不做签名验证
[BaseOS] #仓库名称BaseOS
name=BaseOS #描述信息
baseurl=file:///mydvd/BaseOS #指定YUM仓库repodata位置,file://表示本地协议
enabled=1 #启用仓库
gpgcheck=0 #不做签名验证
[root@client ~]# dnf repolist -v #查看仓库软件包数量,验证YUM仓库配置生效
3. 准备编译环境(client主机)
开源软件多基于C/C++语言开发,需安装gcc、gcc-c++、make等核心编译工具:
bash
[root@client ~]# dnf -y install gcc gcc-c++ make #安装编译依赖工具,-y自动确认
[root@client ~]# rpm -q gcc gcc-c++ make #检查软件包是否安装成功
[root@client ~]# gcc --version #检查gcc版本
# 示例输出:gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-10)
4. 标准实现流程(以inotify-tools-3.13为例,client主机)
源码编译安装通用标准流程:获取源码包→解压→初始化→编译→安装→验证,以下为完整实操命令:
(1)获取并解压源码包
bash
# 通过WindTerm远程连接client,按alt w再按alt e打开文件管理器,将inotify-tools-3.13.tar.gz拖拽至/root
[root@client ~]# ls #查看源码包是否已上传至虚拟机/root
# 示例输出:公共 视频 文档 音乐 anaconda-ks.cfg inotify-tools-3.13.tar.gz
# 模板 图片 下载 桌面 initial-setup-ks.cfg
[root@client ~]# tar -xf inotify-tools-3.13.tar.gz -C /usr/src/ #释放源码包至/usr/src/
[root@client ~]# ls /usr/src/ #查看释放结果
# 示例输出:debug inotify-tools-3.13 kernels
(2)初始化(生成Makefile编译说明书)
bash
[root@client ~]# cd /usr/src/inotify-tools-3.13 #切换至源码包目录
[root@client inotify-tools-3.13]# ./configure #初始化,检查系统环境并生成Makefile
[root@client inotify-tools-3.13]# ls Makefile #查看Makefile是否生成,验证初始化成功
(3)编译及安装
bash
[root@client inotify-tools-3.13]# make #编译,读取Makefile调用gcc生成二进制程序
[root@client inotify-tools-3.13]# make install #安装,将编译好的文件复制到系统安装目录
# 注:两步可合并执行:make && make install (&&表示前方命令执行成功后再执行后方命令)
(4)验证安装结果
bash
[root@client inotify-tools-3.13]# ls /usr/local/bin/inotifywa* #查看安装的inotify相关程序
[root@client inotify-tools-3.13]# inotifywait --help #查看程序的帮助信息,验证程序可用
5. 核心小结
掌握源码编译安装6步标准流程,理解各步骤核心作用:
- 解压(释放源码)
- configure(检查环境+生成Makefile)
- make(编译源码)
- make install(安装程序)
- 编译前必须安装C/C++编译依赖,且需配置可用的YUM仓库
./configure --help可查看自定义配置参数,如--prefix=可指定软件安装路径
三、rsync同步
1. 核心概述
rsync(Remote Sync,远程同步)支持本地同步、远程同步 ,官方网址:http://rsync.samba.org/;
核心优势:增量拷贝 ,仅传输源目录与目标目录之间变化过的数据 ,区别于cp/scp的完全拷贝 (无论数据是否变化均全部传输),同步效率更高;
基于SSH协议实现远程同步,要求远程主机开启sshd服务,支持口令认证/无密码认证。
2. 核心语法与关键差异
基础命令格式 :rsync [选项] 源目录 目标目录
核心易错点 :源目录加/与不加/ 效果完全不同,是rsync使用的关键:
rsync [选项] /src /dst:将整个/src目录 同步至/dst,结果为/dst/src;rsync [选项] /src/ /dst:将**/src目录内的所有数据**同步至/dst,结果为/dst直接包含src的文件/子目录。
3. rsync常用核心选项
| 选项 | 核心作用 | 补充说明 |
|---|---|---|
-a |
归档模式 | 等价于rlptgoD,保留文件所有属性(递归、符号链接、权限、时间、属主/属组、设备文件等),所有同步场景必加 |
-n |
测试同步 | 仅模拟同步过程,不做实际修改,适合先验证再执行真实同步 |
--delete |
删除多余数据 | 删除目标目录中源目录没有的内容 ,实现源目录与目标目录完全一致 |
-v |
详细输出 | 显示同步的详细操作信息,便于排查同步问题 |
-z |
压缩传输 | 传输过程中对数据进行压缩/解压,减少带宽占用,适合大文件/远程同步 |
-r |
递归同步 | 递归处理目录,包含子目录及所有文件(已包含在-a中,无需单独添加) |
4. rsync本地同步(client主机)
bash
[root@client ~]# mkdir /src /dst #创建测试目录
[root@client ~]# touch /src/a.txt #创建测试文件
[root@client ~]# rsync -a /src /dst #将/src整个目录同步至/dst
[root@client ~]# ls /dst/ #查看同步结果,输出:src
[root@client ~]# rsync -a /src/ /dst #将/src目录里边的数据同步至/dst
[root@client ~]# ls /dst/ #查看同步结果,输出:a.txt src
# 验证-av选项的增量同步作用
[root@client ~]# touch /src/b.txt #源目录/src中新增数据b.txt
[root@client ~]# touch /src/c.txt #源目录/src中新增数据c.txt
[root@client ~]# rsync -av /src/ /dst #将/src/里新增的数据同步至/dst目录(-v显示传输过程)
# 示例输出:
sending incremental file list
./
b.txt
c.txt
sent 189 bytes received 57 bytes 492.00 bytes/sec
total size is 0 speedup is 0.00
# 验证--delete选项的完全同步作用
[root@client ~]# touch /dst/ddd.txt #目标目录/dst中新增数据ddd.txt
[root@client ~]# touch /src/sss.txt #源目录/src中新增数据sss.txt
[root@client ~]# ls /src /dst #查看源目录和目标目录现有数据
# 输出:
/dst:
a.txt b.txt c.txt ddd.txt src
/src:
a.txt b.txt c.txt sss.txt
[root@client ~]# rsync -av --delete /src/ /dst #删除目标多余数据,实现完全同步
# 示例输出:
sending incremental file list
deleting src/a.txt
deleting src/
deleting ddd.txt
./
sss.txt
sent 172 bytes received 70 bytes 484.00 bytes/sec
total size is 0 speedup is 0.00
5. rsync远程同步(client主机操作)
分上行同步(本地→远程) 和 下行同步(远程→本地),服务端要求:开启sshd服务、提供远程用户名/密码、本地与远程可通信。
(1)上行同步(client→server,本地同步至远程)
bash
[root@client ~]# rsync -avz /boot/ root@192.168.8.101:/opt #client远程同步至server,-z压缩传输
[root@server ~]# ls /opt #server主机查看同步结果
[root@client ~]# touch /boot/test01.txt #client主机新增数据
[root@server ~]# touch /opt/server.txt #server主机新增多余数据
[root@client ~]# rsync -av --delete /boot/ root@192.168.8.101:/opt #完全同步,删除远程多余数据
[root@client ~]# ls /boot #client主机查看源数据
[root@server ~]# ls /opt #server主机查看同步结果,与源数据一致
(2)下行同步(server→client,远程同步至本地)
bash
[root@client ~]# rsync -avz root@192.168.8.101:/boot/ /opt/ #server远程同步至client,-z压缩传输
[root@client ~]# ls /opt #client主机查看同步结果
[root@server ~]# touch /boot/srv.txt #server主机新增数据
[root@client ~]# touch /opt/client.txt #client主机新增多余数据
[root@client ~]# rsync -av --delete root@192.168.8.101:/boot/ /opt/ #完全同步,删除本地多余数据
[root@server ~]# ls /boot #server主机查看源数据
[root@client ~]# ls /opt #client主机查看同步结果,与源数据一致
6. 核心小结
- 掌握rsync本地同步的核心语法,重点区分源目录加/与不加/的差异,这是使用rsync的关键;
- 牢记
-a为核心必加选项,实现文件属性的完整保留; - 远程同步分上行/下行,结合
-avz实现高效压缩同步,--delete实现源与目标的完全一致。
四、inotify+rsync实时同步
1. 核心背景
普通rsync为手动/定时同步 ,时间间隔难以把控,数据同步时效性差;
Linux内核自带inotify机制 ,提供事件响应式的文件系统通知机制 ,可实时监控目录/文件的变化(如创建、删除、修改);
需安装inotify-tools控制工具(含inotifywait/inotifywatch),才能调用inotify机制实现监控,结合rsync可实现数据变化即触发同步的实时效果,是生产环境数据实时备份的核心方案。
2. 源码编译安装inotify-tools工具(client主机)
实时同步依赖inotify-tools,以下为完整的编译安装命令(与前文一致,保留原代码):
bash
[root@client ~]# dnf -y install gcc gcc-c++ make #安装依赖包
[root@client ~]# tar -xf inotify-tools-3.13.tar.gz -C /usr/src/ #1.解压源码包至/usr/src/
[root@client ~]# cd /usr/src/inotify-tools-3.13/ #切换解压目录
[root@client inotify-tools-3.13]# ./configure #2.初始化,生成Makefile
[root@client inotify-tools-3.13]# make #3.编译
[root@client inotify-tools-3.13]# make install #4.编译安装
[root@client inotify-tools-3.13]# ls /usr/local/bin/inotifywa* #查看是否安装成功,验证程序存在
3. inotifywait监控工具(client主机)
inotifywait是inotify机制的核心监控工具,用于实时捕获目录/文件的变化事件,命令格式 :inotifywait [选项] 目标目录
(1)inotifywait常用选项
| 选项 | 核心作用 |
|---|---|
-m |
持续监控 |
-r |
递归监控 |
-q |
减少输出 |
-e |
指定事件 |
(2)inotifywait监控实操(双窗口操作)
bash
# 第一个窗口:执行监控命令,持续递归静默监控/opt目录
[root@client ~]# inotifywait -mrq /opt/ #第一个窗口监控/opt变化
# 第二个窗口:对/opt执行写操作,触发监控事件
[root@client ~]# touch /opt/a.txt #第二个窗口在/opt目录中创建a.txt文件(触发create创建动作)
# 第一个窗口:立即给出监控提示,输出结果:
/opt/ CREATE a.txt
/opt/ OPEN a.txt
/opt/ ATTRIB a.txt
/opt/ CLOSE_WRITE,CLOSE a.txt
4. inotify+rsync实时同步完整实现(client→server)
核心思路:SSH免密认证(避免同步输密码)+ Shell脚本(监控+同步)+ 后台运行(不占用终端) ,实现client主机/boot目录实时同步至server主机/opt目录,步骤如下:
(1)前置条件:client主机实现无密码远程连接server主机
bash
[root@client ~]# ssh-keygen #生成SSH密钥对,一路回车默认生成(~/.ssh/id_rsa私钥、id_rsa.pub公钥)
[root@client ~]# ssh-copy-id root@192.168.8.101 #将公钥拷贝至server主机
root@192.168.8.101's password: 123456 #输入server主机root密码,输入无回显
[root@client ~]# ssh root@192.168.8.101 #远程连接验证,无需密码即为成功
[root@server ~]# exit #退出server主机
(2)编写Shell脚本,实现监控+实时同步
bash
[root@client ~]# vim /root/inotifywait.sh #编写shell脚本
# 脚本完整内容(保留原代码,无删减):
#!/bin/bash
while inotifywait -rqq /boot/ #监控/boot目录变化,-rqq表示递归+静默模式
do
rsync -az --delete /boot/ root@192.168.8.101:/opt/ #触发变化后执行实时同步,实现完全一致
done
[root@client ~]# chmod +x /root/inotifywait.sh #为脚本赋予可执行权限(x权限)
[root@client ~]# /root/inotifywait.sh & #后台运行脚本,&表示不占用当前终端
(3)验证实时同步效果
bash
[root@client ~]# touch /boot/haha.txt #在client被监控的/boot目录下增加数据,自动触发同步
[root@server ~]# ls /opt #server主机验证,/opt目录中已实时出现haha.txt,同步成功
5. 核心小结
- 理解inotify机制的核心作用:事件响应式的实时文件监控,区别于传统的定时轮询;
- 掌握
inotifywait核心选项的使用,-mrq为最常用的监控组合选项; - 实现实时同步的关键是SSH免密认证,避免同步过程中手动输入密码;
- Shell脚本通过
while循环实现"监控事件→触发同步"的自动化,&实现脚本后台运行,不占用终端。
五、整体课程核心总结
本次Day06内容为Linux云计算/运维必备的核心实操技能,四大模块层层递进,从基础的远程文件传输到高级的实时数据同步,所有操作均基于生产环境需求设计,核心掌握要点:
- scp远程拷贝 :掌握上传/下载完整命令,牢记目录拷贝加
-r,理解SSH加密传输的特性,能完成本地与远程的文件/目录双向传输; - 源码编译安装:掌握6步标准流程,能独立配置本地YUM仓库、安装编译依赖,理解各步骤的核心作用,可完成开源软件的自定义安装;
- rsync同步 :掌握本地/远程同步的完整命令,区分源目录加/与不加/的差异,熟记核心选项(
-a/--delete/-z),理解增量拷贝的优势,能实现源与目标的高效同步/完全同步; - inotify+rsync实时同步:掌握inotifywait监控工具的使用,能独立配置SSH免密认证,编写简单的Shell脚本实现"监控→触发同步",理解生产环境实时数据备份的核心逻辑。