跳至内容

前言

rsync 在进行数据同步之前,需要进行用户认证。**认证主要有两种协议方式:SSH 协议和 rsync 协议 (rsync 协议默认端口为 873)**

  • SSH 协议验证登录方式:以 SSH 协议为基础进行用户身份认证(也就是以 GNU/Linux 本身的用户与密码进行验证),然后再进行数据同步。
  • rsync 协议验证登录方式:以 rsync 协议进行用户身份认证(非 GNU/Linux 系统用户,类似于 vsftpd 的虚拟用户),然后再进行数据同步。

在具体演示 rsync 同步之前,你一定要学会使用 rsync 命令,在 Rocky Linux 8 中,rsync 的 rpm 包是默认安装的,版本为 3.1.3-12,如下所示

[root@Rocky ~]# rpm -qa|grep rsync
rsync-3.1.3-12.el8.x86_64
Basic format: rsync [options] original location target location
Commonly used options:
-a: archive mode, recursive and preserves the attributes of the file object, which is equivalent to -rlptgoD (without -H, -A, -X)
-v: Display detailed information about the synchronization process
-z: compress when transferring files
-H: Keep hard link files
-A: retain ACL permissions
-X: retain chattr permissions
-r: Recursive mode, including all files in the directory and subdirectories
-l: still reserved for symbolic link files
-p: Permission to retain file attributes
-t: time to retain file attributes
-g: retain the group belonging to the file attribute (only for super users)
-o: retain the owner of the file attributes (only for super users)
-D: Keep device files and other special files

笔者个人使用:rsync -avz 源路径 目标路径

环境描述

项目 描述
Rocky Linux 8(Server) 192.168.100.4/24
Fedora 34(client) 192.168.100.5/24

可以使用 Fedora 34 来进行上传和下载

graph LR;
RockyLinux8-->|pull/download|Fedora34;
Fedora34-->|push/upload|RockyLinux8;

当然也可以使用 Rocky Linux 8 来进行上传和下载

graph LR;
RockyLinux8-->|push/upload|Fedora34;
Fedora34-->|pull/download|RockyLinux8;

基于 SSH 协议的演示

技巧

这里 Rocky Linux 8 和 Fedora 34 都使用 root 用户进行登录,Fedora 34 作为客户端,Rocky Linux 8 作为服务器。

拉取/下载

因为是基于 SSH 协议,所以我们先在服务器上创建一个用户

[root@Rocky ~]# useradd testrsync
[root@Rocky ~]# passwd testrsync

在客户端上,我们进行拉取/下载,服务器上的文件为 /rsync/aabbcc

[root@fedora ~]# rsync -avz testrsync@192.168.100.4:/rsync/aabbcc /root
testrsync@192.168.100.4 ' s password:
receiving incremental file list
aabbcc
sent 43 bytes received 85 bytes 51.20 bytes/sec
total size is 0 speedup is 0.00
[root@fedora ~]# cd
[root@fedora ~]# ls
aabbcc

传输成功

技巧

如果服务器端的 SSH 端口不是默认的 22,我们可以通过类似的方式指定端口---rsync -avz -e 'ssh -p [port]'

推送/上传

[root@fedora ~]# touch fedora
[root@fedora ~]# rsync -avz /root/* testrsync@192.168.100.4:/rsync/
testrsync@192.168.100.4 ' s password:
sending incremental file list
anaconda-ks.cfg
fedora
rsync: mkstemp " /rsync/.anaconda-ks.cfg.KWf7JF " failed: Permission denied (13)
rsync: mkstemp " /rsync/.fedora.fL3zPC " failed: Permission denied (13)
sent 760 bytes received 211 bytes 277.43 bytes/sec
total size is 883 speedup is 0.91
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1330) [sender = 3.2.3]

提示权限不足,如何解决?

首先查看 /rsync/ 目录的权限,发现是没有“w”写权限的,我们可以使用 setfacl 对其进行授权

[root@Rocky ~ ] # ls -ld /rsync/
drwxr-xr-x 2 root root 4096 November 2 15:05 /rsync/
[root@Rocky ~ ] # setfacl -mu:testrsync:rwx /rsync/
[root@Rocky ~ ] # getfacl /rsync/
getfacl: Removing leading ' / ' from absolute path names
# file: rsync/
# owner: root
# group: root
user::rwx
user:testrsync:rwx
group::rx
mask::rwx
other::rx

再次尝试,成功!

[root@fedora ~ ] # rsync -avz /root/* testrsync@192.168.100.4:/rsync/
testrsync@192.168.100.4 ' s password:
sending incremental file list
anaconda-ks.cfg
fedora
sent 760 bytes received 54 bytes 180.89 bytes/sec
total size is 883 speedup is 1.08

作者:李天赐

贡献者:Steven Spencer, Ganna Zhyrnova