rsync服務器的搭建
Rsync(remote synchronize)是一個遠程數據同步工具,簡要的概括就是主機於主機之間的文件目錄數據的一個同步。下面就是rsync服務器的搭建過程。
系統環境
平臺:Centos 6.5
rsync版本:rsync-3.0.6-12.el6.x86_64
rsync服務器:172.23.216.86
rsync客戶端:172.23.216.87
服務端安裝rsync服務
1.檢查rsync是否已經安裝,若已安裝,使用rpm -e 命令卸載。
[root@021rjsh216087s ~]# rpm -qa|grep rsync rsync-3.0.6-12.el6.x86_64
2.安裝這裏我們選擇yum安裝。
yum -y install rsync
3.安裝完成後,我們要在/etc目錄下創建並編輯這三個文件。
[root@021rjsh216086s run]# ll /etc/rsyncd* -rw-r--r-- 1 root root 569 Jan 5 11:25 /etc/rsyncd.conf --配置文件 -rw-r--r-- 1 root root 0 Jan 4 15:23 /etc/rsyncd.motd --歡迎文件(不重要) -rw------- 1 root root 12Jan 4 16:27 /etc/rsyncd.password --用戶的密碼文件
4.修改/etc/rsyncd.conf 文件
#This is rsync conf###### uid = root #必須是系統的用戶,可以不是root gid = root #必須是系統用用戶,可以不是root max connections = 200 timeout = 300 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log motdfile = /etc/rsyncd.motd [hellowhome] #同步的模塊的名稱 path = /home/hello #模塊的家目錄, read only = false #true表示只讀,false表示非只讀 list = yes hosts allow = * #允許訪問的ip hosts deny = 10.10.10.10 #拒絕訪問的ip auth users = hello #訪問的用戶(可以不是系統用戶) secrets file = /etc/rsyncd.password #密碼文件 exclude = www/ xxx/ #這是除了home目錄的下www 跟xxx目錄 comment = ...Hello.. #歡迎內容
註意:在搭建服務時,要把註釋刪除,或者另起一行,因為當有些配置項(auth users...)後面有空格,rsync會無法使用,這種空格問題很隱蔽,很難發現,所以要避免。
5.修改/etc/rsyncd.password 文件,修改完成之後,要把文件權限賦成600,否則無法訪問。
hello:123456 #格式【用戶名(auth users):密碼】
[root@021rjsh216086s run]# chmod 600 /etc/rsyncd.password [root@021rjsh216086s run]# ll /etc/rsyncd.password -rw------- 1 root root 12 Jan 4 16:27 /etc/rsyncd.password
6.啟動rsync 服務。
rsync --daemon --config=/etc/rsyncd.conf #啟動服務
cat /var/run/rsyncd.pid|xargs kill -9 && rm -rf /var/run/rsyncd.pid #停止服務
7.查看873端口是否成功啟動
[root@021rjsh216086s run]# netstat -an | grep 873 #查873端口狀態 tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN tcp 0 0 :::873 :::* LISTEN [root@021rjsh216086s run]# lsof -i tcp:873 #根據端口查服務 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rsync 31537 root 4u IPv4 1110177 0t0 TCP *:rsync (LISTEN) rsync 31537 root 5u IPv6 1110178 0t0 TCP *:rsync (LISTEN)
客戶端操作
服務端裝完之後,在客戶端也需要進行一些配置。
1.先用yum裝一下rsync這個軟件。
yum -y install rsync
2.測試一下是否成功
[root@021rjsh216087s ~]# rsync -avz -P hello@172.23.216.86:: ***************************************** * * * Rsync * * * * * ***************************************** hellohome loading...Hello..
3.列出服務器上的文件信息
[root@021rjsh216087s ~]# rsync -a hello@172.23.216.86::hellohome #hello為auth users,hellohome為模塊名字,密碼在/etc/rsyncd.password中,列出的信息是/home/hello下的內容 ***************************************** * * * Rsync * * * * * ***************************************** Password: drwx------ 4096 2018/01/04 18:03:28 . -rw-r--r-- 18 2016/05/11 07:21:42 .bash_logout -rw-r--r-- 176 2016/05/11 07:21:42 .bash_profile -rw-r--r-- 124 2016/05/11 07:21:42 .bashrc -rw-r--r-- 569 2018/01/04 17:53:57 rsyncd.conf drwxr-xr-x 4096 2010/11/12 09:04:19 .gnome2 drwxr-xr-x 4096 2017/04/17 14:24:01 .mozilla drwxr-xr-x 4096 2010/08/18 23:44:21 .mozilla/extensions drwxr-xr-x 4096 2010/08/18 23:44:21 .mozilla/plugins drwxr-xr-x 4096 2018/01/04 18:03:28 newdir drwxr-xr-x 4096 2018/01/04 17:58:18 newdir/test drwxr-xr-x 4096 2018/01/04 17:55:57 newdir/test/newdir
4.下載服務器上的文件
[root@021rjsh216087s ~]# rsync -avzP hello@172.23.216.86::hellohome/newdir /tmp/tmp #將hello用戶家目錄下的newdir文件夾拷貝到 本地/tmp/tmp 文件夾下 Password: receiving incremental file list created directory /tmp/tmp newdir/ newdir/test/ newdir/test/newdir/ sent 74 bytes received 411 bytes 194.00 bytes/sec total size is 0 speedup is 0.00 [root@021rjsh216087s ~]# ll /tmp/tmp #查看下載的內容 total 4 drwxr-xr-x 3 504 505 4096 Jan 4 18:03 newdir
5.上傳文件到服務器
[root@021rjsh216087s ~]# rsync -avzP /tmp/abc hello@172.23.216.86::hellohome # 將本地tmp/abc文件下的內容上傳到hello用戶的家目錄下
註意:在上傳時,若寫成【 /tmp/abc 】,上傳的是tmp目錄下的abc文件夾,連abc一同上傳;
若寫成【 /tmp/abc/ 】,上傳的是tmp目錄下的abc目錄下的內容,abc這個文件夾不會上傳。下載時情況也一樣。
6.客戶端配置密碼文件登錄
在客戶端上的新建一個密碼文件/etc/rsyncd.password,在文件內只寫登錄用戶(auth users)的密碼,例如用戶hello,其密碼是123,那麽只在文件中寫123.
[root@021rjsh216087s ~]# chmod 600 /etc/rsyncd.password #也要將密碼文件的權限賦成600,要不會失敗 [root@021rjsh216087s ~]# ll /etc/rsyncd.password -rw------- 1 root root 4 Jan 4 17:00 /etc/rsyncd.password
之後,再用rsync時,加上--password-file=/etc/rsyncd.password,就可以不用輸入密碼登錄了。
[root@021rjsh216087s ~]# rsync -avzP --password-file=/etc/rsyncd.password hello@172.23.216.86::hellohome/newdir /tmp/tmp receiving incremental file list sent 65 bytes received 402 bytes 311.33 bytes/sec total size is 0 speedup is 0.00
至此,算是完成。
參考文檔:rsync服務與搭建詳解
Rsync的文件同步實現
CentOS 6.3下rsync服務器的安裝與配置
rsync關於auth failed on module xxx的解決方法
rsync入門
rsync服務器的搭建