基于 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
作者:田思丽
贡献者:史蒂文·斯宾塞,安娜·吉尔诺娃