一、服务端配置
1.安装rsync
yum –y install rsync
#启动rsync服务
systemctl start rsyncd
#添加到开机自启动中
systemctl enable rsyncd
#查看端口是否监听
netstat -anput|grep rsync
#如果系统提示没有netstat命令那么需要安装net-tools(yum -y install net-tools)
2.修改配置文件
vim /etc/rsyncd.conf
1 # /etc/rsyncd: configuration file for rsync daemon mode
2
3 # See rsyncd.conf man page for more options.
4
5 # configuration example:
6 uid = root
7 gid = root
8 use chroot = no
9 max connections = 4
10 lock file=/var/run/rsyncd.lock
11 log file = /var/log/rsyncd.log
12 # pid file = /var/run/rsyncd.pid
13 exclude = lost+found/
14 transfer logging = yes
15 timeout = 900
16 ignore nonreadable = yes
17 dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
18 #规则名称,作为测试用所以这里的命名就比较随意。
19 [Rsynctest]
20 ##同步的路径
21 path=/data/share
22 ##规则描述
23 comment=测试
24 ignore errors
25 ##是否只读
26 read only=no
27 ##是否只写
28 write only=no
29 list=no
30 ##配置同步时候的身份,但是该身份是在rsync里面定义的,并非是本机实际用户。
31
32 auth users=rsynctest
33 ##用来认证客户端的秘钥文件 格式 USERNAME:PASSWD 此文件权限一定需要改为600,且属主必须与运行
rsync的用户一致。
34
35 secrets file=/etc/rsyncd.passwd
36 ##允许所有主机访问
37 hosts allow=*
3.给rsync定义身份
echo 'rsynctest:123456'>/etc/rsyncd.passwd
#修改身份文件权限
chmod 600 /etc/rsyncd.passwd
注意此处的用户名rsytest必须和配置文件中的用户名一致(auth users=rsynctest)密码可以自己设定(123456)。
4.将rsyncd服务重新启动使修改后的配置生效;同时为了测试效果创建共享文件。
#重启rsyncd服务
systemctl restart rsyncd
#创建共享文件夹
mkdir /data/share
#在共享文件中写入内容
echo "This is a test file" >>/data/share/Readme.txt
二、客户端配置
1.安装rsync
yum -y install rsync
2.创建密码
echo '123456' >>/etc/rsyncd-test.passwd
#注意此处的密码要和服务器中身份认证中的密码一致
#修改权限
chmod 600 /etc/rsyncd-test.passwd
3.开始同步
rsync -auv --password-file=/etc/rsyncd-test.passwd rsynctest@192.168.10.11::Rsynctest /data/share/
#--password-file=rsyncd-test.passwd 文件的位置
#rsynctest 是服务端身份认证中的用户名
#192.168.10.11 服务端IP地址
#Rsynctest 规则名称
#/data/share/ 同步的文件存放的位置