Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

rsync 服務端與客戶端配置實戰(CentOS 6.8)

環境:CentOS release 6.8 (Final),內核 2.6.32-642.el6.x86_64,已配置 EPEL 源(https://mirrors.aliyun.com/repo/epel-6.repo),SELinux 已 Disabled,iptables 未運行。

服務端

1.1 確認是否安裝 rsync

[root@rsync ~]# rpm -qa rsync
rsync-3.0.6-12.el6.x86_64

若未安裝:yum install -y rsync

1.2 添加 rsync 服務用戶

用來管理本地目錄,禁止登入 shell:

[root@rsync ~]# useradd -s /sbin/nologin -M rsync
[root@rsync ~]# id rsync
uid=501(rsync) gid=501(rsync) groups=501(rsync)

1.3 生成 rsyncd.conf

此檔案預設不存在,需自行建立:

cat >> /etc/rsyncd.conf <<EOF
#rsync_config_______start
#created by syaving 2016-9-22
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[backup]
path = /backup
ignore errors
read only = false
list = false
hosts allow=172.16.1.0/24
#hosts deny=0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
#rsync_config_______end
EOF

1.4 建立認證賬號與密碼檔

依 auth users 配置遠程連接賬戶,並依 secrets file 產生密碼檔:

[root@rsync ~]# echo "rsync_backup:rsync" > /etc/rsync.password
[root@rsync ~]# cat /etc/rsync.password
rsync_backup:rsync

1.5 密碼檔權限設為 600

[root@rsync ~]# chmod 600 /etc/rsync.password
[root@rsync ~]# ls -ld /etc/rsync.password
-rw-------. 1 root root 20 Sep 22 19:27 /etc/rsync.password

1.6 建立共享目錄並授權

[root@rsync ~]# mkdir /backup -p
[root@rsync ~]# chown -R rsync.rsync /backup
[root@rsync ~]# ll -ld /backup/
drwxr-xr-x. 12 rsync rsync 4096 Sep 23 19:17 /backup/

注意:如果沒有 /backup 目錄,就會出現 chdir failed。

1.7 啟動並檢查

[root@rsync ~]# rsync --daemon
[root@rsync ~]# ps -ef | grep rsync | grep -v grep
failed to create pid file /var/run/rsyncd.pid: File exists
root 25117 1 0 Sep27 ? 00:00:00 rsync --daemon
[root@rsync ~]# lsof -i :873
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsync 25117 root 4u IPv4 35555 0t0 TCP *:rsync (LISTEN)
rsync 25117 root 5u IPv6 35556 0t0 TCP *:rsync (LISTEN)

1.8 開機自啟動

[root@rsync ~]# echo "/usr/bin/rsync --daemon" >> /etc/rc.local
[root@rsync ~]# tail -1 /etc/rc.local
/usr/bin/rsync --daemon

客戶端

1.1 確認已安裝

[root@rsync ~]# rpm -qa rsync
rsync-3.0.6-12.el6.x86_64

1.2 建立密碼檔

客戶端只需密碼本身:

[root@rsync ~]# echo "rsync" > /etc/rsync.password
[root@rsync ~]# cat /etc/rsync.password
rsync

1.3 權限設為 600

[root@rsync ~]# chmod 600 /etc/rsync.password
[root@rsync ~]# ls -ld /etc/rsync.password
-rw-------. 1 root root 20 Sep 22 19:27 /etc/rsync.password

1.4 建立同步目錄

[root@rsync ~]# mkdir /backup -p
[root@rsync ~]# ll -ld /backup/

1.5 推送與拉取

推送:

rsync -avz --delete /tmp/ rsync_backup@10.0.0.8::rsync \
    --password-file=/etc/rsync.password
# --delete 請謹慎使用

rsync -avz /tmp/ rsync://rsync_backup@10.0.0.8/backup/ \
    --password-file=/etc/rsync.password

拉取:

rsync -avz rsync_backup@10.0.0.8::rsync /tmp/ \
    --password-file=/etc/rsync.password

rsync -avz rsync://rsync_backup@10.0.0.8/backup/ /tmp/ \
    --password-file=/etc/rsync.password

提示:上述的 backup 是模塊名,不是路徑。

原文出自「宋某人c」博客:https://syaving.blog.51cto.com/5614476/1862916

Copyright © Linux教程網 All Rights Reserved