port=873 //監聽端口默認爲873,也能夠是別的端口 log file=/var/log/rsync.log //指定日誌 pid file=/var/run/rsyncd.pid //指定pid address=192.168.202.130 #能夠定義綁定的ip [test] #爲模塊名,自定義 path=/root/rsync // 指定該模塊對應在哪一個目錄下 use chroot=true //是否限定在該目錄下,默認爲true,當有軟鏈接時,須要改成fasle,若是爲true就限定爲模塊默認目錄 max connections=4 //指定最大能夠鏈接的客戶端數 read only=no //是否爲只讀,若是是no的話,客服端推送給服務端的時候不成功,這時候要改爲yes list=true //是否能夠列出模塊名 rsync --port 8730 172.16.37.139:: 若是爲yes的話會列出客戶端全部的模塊名字。 uid=root //以哪一個用戶的身份來傳輸 gid=root //以哪一個組的身份來傳輸 auth users=test //指定驗證用戶名,能夠不設置,不設置默認不用密碼,設置的話安全性更高點 secrets file=/etc/rsyncd.passwd //指定密碼文件,若是設定驗證用戶,這一項必須設置,設定密碼權限爲400. hosts allow=192.168.133.132 1.1.1.1 2.2.2.2 192.168.133.0/24 //設置能夠容許訪問的主機,能夠是網段,多個Ip地址用空格隔開
[root@hanfeng ~]# vim /etc/rsyncd.conf
port=873 #監聽端口默認爲873,也能夠是別的端口 log file=/var/log/rsync.log pid file=/var/run/rsyncd.pid address=192.168.202.130 [test] path=/root/rsync use chroot=true max connections=4 read only=no list=true uid=root gid=root auth users=test secrets file=/etc/rsyncd.passwd hosts allow=192.168.202.132
[root@hanfeng ~]# rsync --daemon [root@hanfeng ~]#
[root@hanfeng ~]# ps aux |grep rsync root 2473 0.0 0.0 114640 536 ? Ss 21:21 0:00 rsync --daemon root 2475 0.0 0.0 112656 988 pts/0 R+ 21:21 0:00 grep --color=auto rsyn [root@hanfeng ~]#
[root@hanfeng ~]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2033/master tcp 0 0 192.168.202.130:873 0.0.0.0:* LISTEN 2473/rsync tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1149/sshd tcp6 0 0 ::1:25 :::* LISTEN 2033/master tcp6 0 0 :::22 :::* LISTEN 1149/sshd [root@hanfeng ~]#
[root@hanfeng ~]# vim /etc/rsyncd.conf 將path=/root/rsync改成path=/tmp/rsync [root@hanfeng ~]# mkdir /tmp/rsync [root@hanfeng ~]#
[root@hanfeng ~]# chmod 777 /tmp/rsync [root@hanfeng ~]#
[root@hf-02 ~]# rsync -avP /tmp/hanfeng.txt 192.168.202.130::test/hanfeng-02.txt rsync: failed to connect to 192.168.202.130 (192.168.202.130): No route to host (113) rsync error: error in socket IO (code 10) at clientserver.c(122) [sender=3.0.9]
[root@hf-02 ~]# ping 192.168.202.130 PING 192.168.202.130 (192.168.202.130) 56(84) bytes of data. 64 bytes from 192.168.202.130: icmp_seq=1 ttl=64 time=18.3 ms 64 bytes from 192.168.202.130: icmp_seq=2 ttl=64 time=0.335 ms ^C --- 192.168.202.130 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1002ms rtt min/avg/max/mdev = 0.335/9.366/18.398/9.032 ms [root@hf-02 ~]#
[root@hf-02 ~]# telnet 192.168.202.130 873 Trying 192.168.202.130... telnet: connect to address 192.168.202.130: No route to host [root@hf-02 ~]#
[root@hf-02 ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 2278 5435K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 1 84 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 1 52 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 18 3551 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT 1727 packets, 116K bytes) pkts bytes target prot opt in out source destination [root@hf-02 ~]#
[root@hf-02 ~]# systemctl stop firewalld [root@hf-02 ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination [root@hf-02 ~]#
[root@hf-02 ~]# telnet 192.168.202.130 873 Trying 192.168.202.130... Connected to 192.168.202.130. Escape character is '^]'. @RSYNCD: 30.0 ^] telnet> quit Connection closed. [root@hf-02 ~]#
#auth users=test #secrets file=/etc/rsyncd.passwd
[root@hf-02 ~]# rsync -avP /tmp/hanfeng.txt 192.168.202.130::test/hanfeng-02.txt sending incremental file list hanfeng.txt 50 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1) sent 125 bytes received 27 bytes 304.00 bytes/sec total size is 50 speedup is 0.33 [root@hf-02 ~]#
[root@hanfeng ~]# ls /tmp/rsync hanfeng-02.txt [root@hanfeng ~]#
[root@hf-02 ~]# rsync -avP 192.168.202.130::test/hanfeng-02.txt /tmp123.txt receiving incremental file list hanfeng-02.txt 50 100% 48.83kB/s 0:00:00 (xfer#1, to-check=0/1) sent 45 bytes received 159 bytes 408.00 bytes/sec total size is 50 speedup is 0.25 [root@hf-02 ~]#
port:指定在哪一個端口啓動rsyncd服務,默認是873端口。 log file:指定日誌文件。 pid file:指定pid文件,這個文件的做用涉及服務的啓動、中止等進程管理操做。 address:指定啓動rsyncd服務的IP。假如你的機器有多個IP,就能夠指定由其中一個啓動rsyncd服務,若是不指定該參數,默認是在所有IP上啓動。 []:指定模塊名,裏面內容自定義。 path:指定數據存放的路徑。 use chroot true|false:表示在傳輸文件前首先chroot到path參數所指定的目錄下。這樣作的緣由是實現額外的安全防禦,但缺點是須要以roots權限,而且不能備份指向外部的符號鏈接所指向的目錄文件。默認狀況下chroot值爲true,若是你的數據當中有軟鏈接文件,建議你設置成false。 max connections:指定最大的鏈接數,默認是0,即沒有限制。 read only ture|false:若是爲true,則不能上傳到該模塊指定的路徑下。 list:表示當用戶查詢該服務器上的可用模塊時,該模塊是否被列出,設定爲true則列出,false則隱藏。 uid/gid:指定傳輸文件時以哪一個用戶/組的身份傳輸。 auth users:指定傳輸時要使用的用戶名。 secrets file:指定密碼文件,該參數連同上面的參數若是不指定,則不使用密碼驗證。注意該密碼文件的權限必定要是600。格式:用戶名:密碼 hosts allow:表示被容許鏈接該模塊的主機,能夠是IP或者網段,若是是多個,中間用空格隔開。 當設置了auth users和secrets file後,客戶端連服務端也須要用用戶名密碼了,若想在命令行中帶上密碼,能夠設定一個密碼文件 rsync -avL test@192.168.133.130::test/test1/ /tmp/test8/ --password-file=/etc/pass 其中/etc/pass內容就是一個密碼,權限要改成600
[root@hanfeng ~]# cat /var/log/rsync.log 2017/12/06 21:16:31 [2445] rsyncd version 3.0.9 starting, listening on port 873 2017/12/06 21:16:31 [2445] bind() failed: Cannot assign requested address (address-family 2) 2017/12/06 21:16:31 [2445] unable to bind any inbound sockets on port 873 2017/12/06 21:16:31 [2445] rsync error: error in socket IO (code 10) at socket.c(555) [Receiver=3.0.9] 2017/12/06 21:21:22 [2473] rsyncd version 3.0.9 starting, listening on port 873 2017/12/06 23:10:53 [2619] name lookup failed for 192.168.202.132: Name or service not known
[root@hanfeng ~]# rsync --port=873 192.168.202.130:: test [root@hanfeng ~]#
[root@hanfeng ~]# rsync --port=873 192.168.202.130:: [root@hanfeng ~]#
rsync傳輸時設置密碼shell
在/etc/rsyncd.passwd文件中編輯數據庫
而後修改權限爲600vim
rsync -avP /tmp/test/ test@192.168.202.130::test/安全
在rsync傳輸文件的時候,在寫shell腳本輸入密碼很差,好比天天凌晨半夜更新數據庫文件啥的,那就會很麻煩服務器