概述
本章,您将学习到 Rsync 的基本功能演示。
Rsync 在进行数据同步之前需要对用户的身份进行验证,有两种协议可完成身份验证:
- SSH 协议 - 占用默认端口 22。这种协议会使用 GNU/Linux 本身存在的用户和密码做身份验证,完成认证后进行数据同步
- RSYNC 协议 - 占用默认端口 873。这种协议会使用非 GNU/Linux 的用户和密码做身份验证(类似于 Vsftp 中的虚拟用户),完成认证后进行数据同步
命令的选项说明
对于 Rsync 工具而言,系统管理员主要使用的命令为 rsync,其用法为 rsync [OPTION] 来源位置 目标位置,其中常见的选项有:
- -a - 归档模式,递归且保留文件对象的属性,等同于-rlptgoD(没有-H、-A、-X)
- -v - 显示同步过程的详细信息
- -z - 在传输文件时进行压缩
- -H - 保留硬链接文件
- -A - 保留 ACL 权限
- -X - 保留 chattr 权限
- -r - 递归模式,包含目录以及子目录中所有的文件
- -l - 对于符号链接文件仍然保留
- -p - 保留文件属性的权限
- -t - 保留文件属性的时间
- -g - 保留文件属性的所属组(仅限超级用户使用)
- -o - 保留文件属性的所有者(仅限超级用户使用)
- -D - 保留设备文件以及其他特殊文件
一般常见用法为 rsync -avz 来源位置 目标位置
环境说明
| OS | IPv4 地址(C 类私有地址) |
|---|---|
| Rocky Linux 8.10 | 192.168.100.20/24 |
| Rocky Linux 10.3 | 192.168.100.21/24 |
您可以使用 Rocky Linux 8.10 进行上传与下载:
正在加载 Flowchart 图表...
也可以使用 Rocky Linux 10.3 进行上传与下载:
正在加载 Flowchart 图表...
基于 SSH 协议的演示
此处,Rocky Linux 8.10 和 Rocky Linux 10.3 都使用 root(UID=0) 超级用户进行登陆,其中 Rocky Linux 8.10 是服务器(同步源),而 Rocky Linux 10.3 是客户端(发起端)。
pull/下载
首先在服务器(同步源)中创建一个用户并创建需要同步的目录与文件:
Shell (192.168.100.20)> useradd ruser1
Shell (192.168.100.20)> passwd ruser1
Shell (192.168.100.20)> mkdir -p /rsync/
Shell (192.168.100.20)> echo "Hello!" > /rsync/first1
接着在客户端(发起端)将 /rsync/first1 下载过来:
Shell (192.168.100.21)> rsync -avz rusr1@192.168.100.20:/rsync/first1 /root/
ruser1@192.168.100.20's password:
receiving incremental file list
first1
sent 43 bytes received 96 bytes 21.38 bytes/sec
total size is 7 speedup is 0.05
Shell (192.168.100.21)> ls -l /root/first1
-rw-r--r-- 1 root root 7 Nov 1 20:58 /root/first1
rsync -avz -e 'ssh -p [port]' ... 的方式指定特定的 SSH 端口。push/上传
Shell (192.168.100.21)> echo "RL10.3" > /root/appfile
Shell (192.168.100.21)> rsync -avz /root/appfile ruser1@192.168.100.20:/rsync/
ruser1@192.168.100.20's password:
sending incremental file list
appfile
rsync: mkstemp "/rsync/.appfile.JTwGeJ" failed: Permission denied (13)
sent 107 bytes received 110 bytes 48.22 bytes/sec
total size is 7 speedup is 0.03
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1394) [sender=3.5.0-g5c0688d4]
从输出的文本信息可知,这里是因权限问题而导致的错误。
来到服务器(同步源)这边查看 /rsync/ 目录的信息:
Shell (192.168.100.20)> ls -ld /rsync/
drwxr-xr-x 2 root root 4096 Nov 1 20:58 /rsync/
该目录的权限为 755,且所有者所属组都是 root(UID=0),ruser1 用户对这个目录没有 w 权限,所有会出现错误。
使用 ACL 权限即可:
Shell (192.168.100.20)> setfacl -m u:ruser1:rwx /rsync/
Shell (192.168.100.20)> getfacl /rsync/
getfacl: Removing leading '/' from absolute path names
# file: rsync/
# owner: root
# group: root
user::rwx
user:ruser1:rwx
group::r-x
mask::rwx
other::r-x
在客户端(发起端)中再次尝试,成功!
Shell (192.168.100.21)> rsync -avz /root/appfile ruser1@192.168.100.20:/rsync/
ruser1@192.168.100.20's password:
sending incremental file list
appfile
sent 107 bytes received 35 bytes 31.56 bytes/sec
total size is 7 speedup is 0.05
基于 RSYNC 协议的演示
前面提到,基于 SSH 协议的身份验证需要使用到操作系统中已存在的用户和密码,在实际的生产环境中可能有很多同步需求,这时候创建很多操作系统用户不符合安全要求,难以维护的同时安全风险还比较大。在 Rsync 中,为了安全性考虑,就有了 Rsync 协议的身份验证,类似于 Vsftp 中的虚拟用户(管理员自定义的虚拟用户)。
首先您需要在 192.168.100.20 服务器(同步源)中手动创建 /etc/rsyncd.conf 这个文件,并将一些必要的参数与值写入到该文件中。
常见的参数项的说明如下:
| 参数项 | 说明 |
|---|---|
| address = 192.168.100.20 | Rsync 监听的默认 IP 地址 |
| port = 873 | Rsync 监听的默认端口 |
| pid file = /var/run/rsyncd.pid | 进程 pid 的文件位置 |
| log file = /var/log/rsyncd.log | 日志的文件位置 |
| [share] | 共享名称 |
| comment = rsync | 备注或者描述信息 |
| path = /rsync/ | 所在的系统路径位置 |
| read only = yes | yes 表示只读,no 表示可读可写 |
| dont compress = *.gz *.gz2 *.zip | 哪些文件类型不对它进行压缩 |
| auth users = li | 启用虚拟用户,定义个虚拟用户叫什么。 需要自行创建 |
| secrets file = /etc/rsyncd_users.db | 用来指定虚拟用户的密码文件位置,必须以 .db 结尾。文件的内容格式是 "用户名:密码",一行一个 |
# 写入参数与值
Shell (192.168.100.20)> vim /etc/rsyncd.conf
address = 192.168.100.20
port = 873
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
[share]
comment = rsync
path = /rsync/
read only = yes
dont compress = *.gz *.bz2 *.zip
auth users = li
secrets file = /etc/rsyncd_users.db
# 写入内容到密码文件中
Shell > vim /etc/rsyncd_users.db
li:246810
Shell > chmod 600 /etc/rsyncd_users.db
Shell > ls -l /etc/rsyncd_users.db
-rw------- 1 root root 10 Nov 1 21:43 /etc/rsyncd_users.db
其他一些操作和说明:
# 服务器(同步源)可能还需要另外的软件包
Shell (192.168.100.20)> dnf -y install rsync-daemon
# 开启服务
Shell (192.168.100.20)> systemctl start rsyncd.service
# 查看套接字信息
Shell (192.168.100.20)> ss -tulnp
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
udp UNCONN 0 0 127.0.0.1:323 0.0.0.0:* users:(("chronyd",pid=772,fd=5))
udp UNCONN 0 0 [::1]:323 [::]:* users:(("chronyd",pid=772,fd=6))
udp UNCONN 0 0 [fe80::20c:29ff:feb3:41fd]%ens160:546 [::]:* users:(("NetworkManager",pid=766,fd=24))
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=789,fd=3))
tcp LISTEN 0 5 192.168.100.20:873 0.0.0.0:* users:(("rsync",pid=17993,fd=3))
tcp LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=789,fd=4))
pull/下载
在服务器(同步源)中创建一个文件:
Shell (192.168.100.20)> echo "World" > /rsync/rsync-p.txt
在客户端(发起端)中执行下载操作:
Shell (192.168.100.21)> rsync -avz li@192.168.100.20::share /root/
Password:
receiving incremental file list
./
rsync-p.txt
sent 49 bytes received 169 bytes 87.20 bytes/sec
total size is 20 speedup is 0.09
li@192.168.100.20::share 这种写法外,rsync://li@192.168.100.20/share 也能被允许和接受。push/上传
# 在客户端(发起端)新建一个文件
Shell (192.168.100.21)> echo "369" > /root/new.md
# 尝试上传,提示读取错误
Shell (192.168.100.21)> rsync -avz /root/new.md rsync://li@192.168.100.20/share
Password:
sending incremental file list
rsync: [sender] read error: Connection reset by peer (104)
rsync error: error in socket IO (code 10) at io.c(907) [sender=3.5.0-g5c0688d4]
这里提示读取错误,与服务器(同步源)中的配置项 read only = yes 有关,变更为 no 并重启服务即可:
Shell (192.168.100.20)> sed -i '/read/s/yes/no/g' /etc/rsyncd.conf
Shell (192.168.100.20)> systemctl restart rsyncd.service
在客户端(发起端)中再次尝试:
Shell (192.168.100.21)> rsync -avz /root/new.md rsync://li@192.168.100.20/share
Password:
sending incremental file list
new.md
rsync: mkstemp "/.new.md.VBIFFk" (in share) failed: Permission denied (13)
sent 103 bytes received 114 bytes 62.00 bytes/sec
total size is 4 speedup is 0.02
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1394) [sender=3.5.0-g5c0688d4]
这里提示权限拒绝,请注意!Rsync 协议中的虚拟用户(自定义的虚拟用户)默认会映射为操作系统的 nobody 用户
# 使用 ACL 权限
Shell (192.168.100.20)> setfacl -m u:nobody:rwx /rsync/
Shell (192.168.100.20)> getfacl /rsync/
getfacl: Removing leading '/' from absolute path names
# file: rsync/
# owner: root
# group: root
user::rwx
user:ruser1:rwx
user:nobody:rwx
group::r-x
mask::rwx
other::r-x
客户端(发起端)执行上传操作,成功!
Shell (192.168.100.21)> rsync -avz /root/new.md rsync://li@192.168.100.20/share
Password:
sending incremental file list
new.md
sent 103 bytes received 35 bytes 39.43 bytes/sec
total size is 4 speedup is 0.03










