環境:Centos 6.9安全
兩臺服務器,A(192.168.223.129) 和 B(192.168.223.130)。A 做爲服務端,B做爲客戶端從A服務器同步目錄。把A的/usr/src 目錄下的內容同步到B的/rsync/ 目錄。服務器
首先配置下epel 源:tcp
rpm -ivh https://mirrors.ustc.edu.cn/epel/epel-release-latest-6.noarch.rpm測試
全都先安裝下rsync:ui
yum install rsyncspa
而後,A 先建立/etc/rsyncd.conf 配置文件(默認沒有),內容以下:3d
uid = nobody gid = nobody #hosts allow = * hosts allow = 192.168.223.130 use chroot = no pid file = /var/run/rsyncd.pid log file = /var/log/rsyncd.log [tools]
path = /usr/src
list = no
comment = Rsync share test
auth users = haha #同步的帳戶
secrets file = /etc/rsync_users
exclude = blank.png ; spinner.gif ; WEB-INF #同步時排除哪些文件和目錄
read only = false
timeout = 300
echo "haha:1234567" >/etc/rsync_users #配置同步須要的用戶和密碼code
chmod 600 /etc/rsync_users #必需要修改權限,否則會報錯blog
開啓服務端:進程
rsync --daemon --config=/etc/rsyncd.conf
會已守護進程的方式後臺運行。
能夠把這句寫到/etc/rc.local 中,開機啓動。
rsync 監聽端口是873,說明服務端已經配置好了。
接下來是B服務器客戶端:
客戶端不用配置配置文件,直接能夠從服務端同步目錄,命令以下:
/usr/bin/rsync -avzP --delete --password-file=/etc/rsync.pass haha@192.168.223.129::tools /rsync/
注意:客戶端要生成/etc/rsync.pass 這個密碼文件(路徑隨意),內容是同步帳號的密碼,即:echo "1234567" >/etc/rsync.pass, 而且權限要是600,否則會報錯。
如圖,從A 服務器同步過來兩個文件夾和一個文件。
問題:
一、通常防火牆都是默認開啓的,能夠用 iptables -I INPUT -p tcp --dport 873 -j ACCEPT 命令開放本機873端口,而且經過命令 /etc/init.d/iptables save 保存防火牆配置。也能夠暫時經過 /etc/init.d/iptables stop 關閉防火牆,或永久關閉 chkconfig iptables off(重啓後也不會開啓,除非/etc/init.d/iptables start 開啓)
二、確保客戶端的/etc/rsync.pass 和服務端的 /etc/rsync_users 文件權限都是600.
三、肯定/etc/rsync_users 中配置的帳戶和/etc/rsyncd.conf 中配置的auth users= 的值 還有客戶端同步命令中的帳戶名相同。
四、經測試,/etc/rsync_users 中配置的帳戶和密碼都是虛擬的,不用真實在服務器上建立帳號和密碼,但爲了安全,還請設置複雜一點。