跳至内容

基于 rsync 协议的演示

在 vsftpd 中,有虚拟用户(管理员自定义的伪装用户),因为使用匿名用户和本地用户不安全。我们知道,基于 SSH 协议的服务器必须保证有用户体系。当有很多同步需求时,可能需要创建很多用户。这显然不符合 GNU/Linux 的运维标准(用户越多越不安全),在 rsync 中,出于安全考虑,有 rsync 协议认证登录的方法。

如何做到?

只需要在配置文件中写上相应的参数和值即可。在 Rocky Linux 8 中,需要手动创建文件/etc/rsyncd.conf.

[root@Rocky ~]# touch /etc/rsyncd.conf
[root@Rocky ~]# vim /etc/rsyncd.conf

这个文件的一些参数和值如下,这里有更多的参数说明

项目 描述
address = 192.168.100.4 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 结尾。文件内容格式是 "用户名: 密码",一行一个

技巧

密码文件的权限必须是600.

往里面写一些文件内容/etc/rsyncd.conf,并将用户名和密码写到 /etc/rsyncd_users.db 中,权限是 600

[root@Rocky ~]# cat /etc/rsyncd.conf
address = 192.168.100.4
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
[root@Rocky ~]# ll /etc/rsyncd_users.db
-rw------- 1 root root 9 November 2 16:16 /etc/rsyncd_users.db
[root@Rocky ~]# cat /etc/rsyncd_users.db
li:13579

你可能需要先 dnf -y install rsync-daemon 才能启动服务:systemctl start rsyncd.service

[root@Rocky ~]# systemctl start rsyncd.service
[root@Rocky ~]# netstat -tulnp
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      691/sshd            
tcp        0      0 192.168.100.4:873       0.0.0.0:*               LISTEN      4607/rsync          
tcp6       0      0 :::22                   :::*                    LISTEN      691/sshd            
udp        0      0 127.0.0.1:323           0.0.0.0:*                           671/chronyd         
udp6       0      0 ::1:323                 :::*                                671/chronyd  

拉取/下载

在服务器上创建一个文件以供验证:[root@Rocky]# touch /rsync/rsynctest.txt

客户端进行如下操作

[root@fedora ~]# rsync -avz li@192.168.100.4::share /root
Password:
receiving incremental file list
./
rsynctest.txt
sent 52 bytes received 195 bytes 7.16 bytes/sec
total size is 883 speedup is 3.57
[root@fedora ~]# ls
aabbcc anaconda-ks.cfg fedora rsynctest.txt

成功!除了上述基于 rsync 协议的写法,你也可以这样写:rsync://li@10.1.2.84/share

推送/上传

[root@fedora ~]# touch /root/fedora.txt
[root@fedora ~]# rsync -avz /root/* li@192.168.100.4::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(784) [sender = 3.2.3]

提示读取错误,这是因为服务器的 "read only = yes" 。将其修改为 "no" 并重启服务[root@Rocky ~]# systemctl restart rsyncd.service

再次尝试,提示权限不足

[root@fedora ~]# rsync -avz /root/* li@192.168.100.4::share
Password:
sending incremental file list
fedora.txt
rsync: mkstemp " /.fedora.txt.hxzBIQ " (in share) failed: Permission denied (13)
sent 206 bytes received 118 bytes 92.57 bytes/sec
total size is 883 speedup is 2.73
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1330) [sender = 3.2.3]

我们的虚拟用户这里是li,默认映射到系统用户nobody,当然也可以改成其他系统用户。也就是说,nobody 对 /rsync/ 目录没有写入权限。当然,我们可以使用 [root@Rocky ~]# setfacl -mu:nobody:rwx /rsync/ ,再次尝试,成功。

[root@fedora ~]# rsync -avz /root/* li@192.168.100.4::share
Password:
sending incremental file list
fedora.txt
sent 206 bytes received 35 bytes 96.40 bytes/sec
total size is 883 speedup is 3.66

作者:李天赐

贡献者:Steven Spencer, Ganna Zhyrnova