概述
由 rsync 简述 可知,Rsync 是一个增量备份的工具,每一次执行 rsync 命令,就能实现一次数据的单向同步。若要实现实时的单向数据同步,则需要搭配 inotify-tools 这个程序工具。但搭配 inotify-tools 有一个前提条件,即需要满足免密验证登陆。
提示
不论是使用 Rsync 协议完成身份验证,还是使用 SSH 协议完成身份验证,两者都能实现免密验证登陆。
SSH 协议的免密验证登陆
环境说明如下:
| OS | IPv4 地址(C 类私有地址) | 角色 |
|---|---|---|
| Rocky Linux 8.10 | 192.168.100.20/24 | Rsync 服务器 |
| Rocky Linux 10.3 | 192.168.100.21/24 | 客户端 |
步骤如下:
- 在客户端中生成密钥对,其中私钥文件为 id_rsa,公钥文件为 id_rsa.pub,客户端需要保管好私钥文件
- 将生成的公钥文件上传到服务器上,路径位置以及名称为
~/.ssh/authorized_keys,这里的~代表相应用户的家目录(主目录)
假设我需要服务器的 root(UID=0) 用户完成免密验证登陆,以下为操作步骤:
# 在客户端中生成密钥对
Shell (192.168.100.21)> ssh-keygen -t rsa -b 2048
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase for "/root/.ssh/id_rsa" (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:V/N5q7DBIqIbBkrlWS9SWRTO5vaisJsDbWUMaWffupk root@HOST100
The key's randomart image is:
+---[RSA 2048]----+
| . .+. |
| + o= |
| ..=+.+. o |
| o +++. . . o . |
| o.+o. +S . o .|
|o..o. o.... ..|
|. o+ . o+o + . |
| ..* oEo . + . |
| *+. . . |
+----[SHA256]-----+
Shell (192.168.100.21)> ls -l /root/.ssh/id*
-rw------- 1 root root 1823 Nov 4 19:08 /root/.ssh/id_rsa
-rw-r--r-- 1 root root 394 Nov 4 19:08 /root/.ssh/id_rsa.pub
# 使用 scp 命令将公钥文件上传给服务器
Shell (192.168.100.21)> scp -P 22 /root/.ssh/id_rsa.pub root@192.168.100.20:/tmp/
# 将公钥文件的内容重定向到 authorized_keys 文件中
Shell (192.168.100.20)> cat /tmp/id_rsa.pub > /root/.ssh/authorized_keys
# 配置 OpenSSH 的服务器端
## 关闭 SSH 服务器端的密码认证登陆,这里使用的 sed 命令的替换
Shell (192.168.100.20)> sed -i '/PasswordAuthentication/s/yes/no/g' /etc/ssh/sshd_config
## 开启公钥验证,去掉文件行的开头 "#" 和空格
Shell (192.168.100.20)> sed -i '/PubkeyAuthentication/s/^# //g' /etc/ssh/sshd_config
# 确认配置
Shell (192.168.100.20)> grep -E "^Password|Pubkey" /etc/ssh/sshd_config
PubkeyAuthentication yes
PasswordAuthentication no
# 重启 SSH 服务器端的守护进程
Shell (192.168.100.20)> systemctl restart sshd.service
在客户端中尝试下 SSH 协议的免密验证登陆,成功:
Shell (192.168.100.21)> ssh -p 22 -i /root/.ssh/id_rsa root@192.168.100.20
Last login: Wed Nov 4 19:10:20 2026 from 192.168.100.10
Shell (192.168.100.20)> who
root pts/1 2026-11-04 19:33 (192.168.100.21)
RSYNC 协议的免密验证登陆
在客户端中,可以为 RSYNC 的系统变量 RSYNC_PASSWORD 赋值,默认情况下,该系统变量的值为空值。
Shell (192.168.100.21)> echo "$RSYNC_PASSWORD"
Shell (192.168.100.21)>
RSYNC 服务器中的虚拟用户 li 密码为 246810,需要将这个密码赋值为 RSYNC_PASSWORD 系统变量,同时将该系统变量声明为全局。
# 查看 Rsync 服务器的密码文件内容
Shell (192.168.100.20)> cat /etc/rsyncd_users.db
li:246810
# 查看 Rsync 服务器的配置文件
Shell (192.168.100.20)> cat /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 = no
dont compress = *.gz *.bz2 *.zip
auth users = li
secrets file = /etc/rsyncd_users.db
# 查看 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
# 确认 Rsync 服务器的守护进程处于运行状态
Shell (192.168.100.20)> systemctl status rsyncd.service
# 赋值
Shell (192.168.100.21)> export RSYNC_PASSWORD=246810
# 尝试下载,成功
## 这里因为 Rsync 服务器中的 /rsync/ 目录没有出现新文件,所以传输的文件列表并没有显示出来。
Shell (192.168.100.21)> rsync -avz li@192.168.100.20::share /root/
receiving incremental file list
./
sent 30 bytes received 154 bytes 368.00 bytes/sec
total size is 24 speedup is 0.13
提示
您可以将变量的声明与赋值操作写入到 **/etc/profile** 文件中,使该系统变量永久生效。写入的文本内容为
export RSYNC_PASSWORD=246810版权声明:「自由转载-保持署名-非商业性使用-禁止演绎 3.0 国际」(CC BY-NC-ND 3.0)
用一杯咖啡支持我们,我们的每一篇[文档]都经过实际操作和精心打磨,而不是简单地从网上复制粘贴。期间投入了大量心血,只为能够真正帮助到您。
暂无评论










