前言¶
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(服务器) | 192.168.100.4/24 |
Fedora 34(客户端) | 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 [端口]'
。
推送/上传¶
[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
作者:tianci li
贡献者:Steven Spencer, Ganna Zhyrnova