美文网首页
Rsync 服务器安装配置

Rsync 服务器安装配置

作者: Spareribs | 来源:发表于2017-05-05 23:45 被阅读384次

    rsync安装

    # 检查是否安装过rsync, 如果已经安装过需要卸载
    rpm -qa|grep rsync
    # 安装了rsync 卸载
    rpm -e rsync
    # 下载最新rpm包(注意操作系统和位数的匹配) 
    # rsync的rpm包列表地址:http://pkgs.repoforge.org/rsync
    wget http://pkgs.repoforge.org/rsync/rsync-3.1.1-1.el6.rfx.x86_64.rpm
    # rpm包安装
    rpm -ivh rsync-3.1.1-1.el6.rfx.x86_64.rpm
    

    rsync服务端配置

    启动配置

    • 选择rsync服务器启动方式
      • rsync服务器负载比较高,则使用独立启动模式
      • rsync服务器负责较低,使用xinetd运行方式
    • 创建配置文件rsyncd.conf
    • 对于非匿名方式访问的rsync服务器创建配置口令(建议配置需要口令访问)

    CentOS 默认以xinetd模式运行rsync,rsync的xinetd配置文件是/etc/xinetd.d/rsync 如果配置rsync以xinetd模式运行,执行如下命令

    chkconfig rsync on
    service xinetd restart
    # 如果执行 service xinetd restart 发现 xinetd: unrecognized service 则未安装xinetd服务
    # 执行 yum install xinetd 安装 xinetd服务
    # 安装之后启动 xinetd服务(service xinetd start)
    

    编辑rsync的xinetd配置文件/etc/xinetd.d/rsync文件,修改参数 server_args = --daemon --config=/etc/rsyncd/rsyncd.conf
    可以配置rsync服务器启动时的参数
    如果使用独立运行模式,则执行如下命令

    /usr/bin/rsync --daemon
    #编辑 /etc/rc.local文件 加入 /usr/bin/rsync --daemon 保证每次开机启动都会自动启动rsync服务
    

    配置文件

    # 创建rsync服务目录
    mkdir /etc/rsyncd
    # 创建配置文件
    touch /etc/rsyncd/rsyncd.conf
    # 创建密码文件
    touch /etc/rsyncd/rsyncd.passwd
    #权限修改
    chown root:root /etc/rsyncd/rsyncd.passwd
    chmod 600 /etc/rsyncd/rsyncd.passwd
    

    配置文件语法介绍

    • 模块
      以[模块名称]开始
    • 参数配置行
      格式 name = value
      其中 value的值可以是string(可以不使用引号)或者boolean(0/1,true/false,yes/no)
    • 以#开始是注释行
    • 以\是续行符

    全局参数([模块名称]之外的配置均为全局配置)

    • address
      在独立运行时,用于指定的服务器运行的 IP 地址。由 xinetd 运行时将忽略此参数,使用命令行上的 –address 选项替
      代默认值 本地所有IP
    • port
      指定 rsync 守护进程监听的端口号。 由 xinetd 运行时将忽略此参数,使用命令行上的–port 选项替代。
      默认值是 873
    • motd file
      指定一个消息文件,当客户连接服务器时该文件的内容显示给客户。
      默认值无
    • pid file
      rsync 的守护进程将其 PID 写入指定的文件。
      默认值 无
    • log file
      指定 rsync 守护进程的日志文件,而不将日志发送给 syslog。
      默认值 无
    • syslog facility
      指定 rsync 发送日志消息给 syslog 时的消息级别默认值 daemon
      socket options指定自定义 TCP 选项。
      默认值无

    模块参数 模块参数主要用于定义 rsync 服务器哪个目录要被同步。模块声明的格式必须为 [module] 形式,这个名字就是在 rsync 客户端看到的名字,类似于 Samba 服务器提供的共享名。而服务器真正同步的数据是通过 path 来指定的。可以根据自己的需要,来指定多个模块,模块中可以定义以下参数:

    • 基本模块参数
      • path指定当前模块在 rsync 服务器上的同步路径,该参数是必须指定的
      • comment给模块指定一个描述,该描述连同模块名在客户连接得到模块列表时显示给客户
    • 模块控制参数
      • use chroot
        若为 true,则 rsync 在传输文件之前首先 chroot 到 path 参数所指定的目录下。这样做的原因是实现额外的安全防护,但是缺点是需要 root 权限,并且不能备份指向 path 外部的符号连接所指向的目录文件。
        默认值true
      • uid
        指定该模块以指定的 UID 传输文件。
        默认值 nobody
      • gid
        指定该模块以指定的 GID 传输文件。
        默认值 nobody
      • max connections
        定该模块的最大并发连接数量以保护服务器,超过限制的连接请求将被告知随后再试。
        默认值 0 不限制
      • read only
        指定是否允许客户上传文件。若为 true 则不允许上传;若为 false 并且服务器目录也具有读写权限则允许上传。
        默认值 true
      • write only
        指定是否允许客户下载文件。若为 true 则不允许下载;若为 false 并且服务器目录也具有读权限则允许下载。
        默认值 false
    • 模块认证参数
      • hosts allow
        用一个主机列表指定哪些主机客户允许连接该模块。不匹配主机列表的主机将被拒绝默认值 *

    客户主机列表定义可以是以下形式:
    单个IP地址 例如:192.168.0.1整个网段 例如:192.168.0.0/24,192.168.0.0/255.255.255.0可解析的单个主机名 例如:centos,centos.bsmart.cn域内的所有主机 例如:.bsmart.cn“”则表示所有。多个列表项要用空格间隔。

    • hosts deny
      用一个主机列表指定哪些主机客户不允许连接该模块。
      默认值 空
    • auth users
      指定由空格或逗号分隔的用户名列表,只有这些用户才允许连接该模块。这里的用户和系统用户没有任何关系。用户名和口令以明文方式存放在 - - secrets file
      参数指定的文件中默认匿名
    • secrets file
      指定一个 rsync 认证口令文件。只有在 auth users 被定义时,该文件才起作用。
      默认值 空
    • strict modes
      指定是否监测口令文件的权限。若为 true 则口令文件只能被 rsync 服务器运行身份的用户访问,其他任何用户不可以访问该文件。
      默认值 true
      > - rsync 认证口令文件的权限一定是 600,否则客户端将不能连接服务器。
    • rsync 认证口令文件中每一行指定一个 用户名:口令 对,格式为:username:passwd
    • 一般来说口令最好不要超过8个字符。若您只配置匿名访问的 rsync 服务器,则无需设置上述参数。

    配置文件实例

    # GLOBAL OPTIONS
    uid = root                         
    gid = root                                  
    
    use chroot = no                     
    
    read only = yes                            
    
    #limit access to private LANs
    hosts allow=172.16.0.0/255.255.0.0 192.168.1.0/255.255.255.0 10.0.1.0/255.255.255.0
    hosts deny=*                                
    max connections = 5                      
    
    pid file = /var/run/rsyncd.pid             
    
    secrets file = /etc/rsyncd/rsyncd.secrets   
    #lock file = /var/run/rsync.lock           
    
    #motd file = /etc/rsyncd/rsyncd.motd        
    
    #This will give you a separate log file
    #log file = /var/log/rsync.log               
    
    #This will log every file transferred - up to 85,000+ per user, per sync
    transfer logging = yes                    
    
    log format = %t %a %m %f %b
    syslog facility = local3
    timeout = 300
    
    # MODULE OPTIONS
    [davidhome]                               
    path = /home/david/                       
    list=yes                                   
    ignore errors                              
    auth users = david                          
    comment = David home                  
    exclude = important/ 
    
    [chinatmp]
    path = /tmp/china/
    list=no
    ignore errors
    auth users = china
    comment = tmp_china
    

    密码文件

    david:asdf #格式 用户名:口令
    china:jk #该用户不要求是系统用户
    

    查看rsync服务是否启动 netstat -an | grep 873

    rsync 客户端

    # 安装客户端yum -y install rsync
    # 同步命令
    # -a 参数,相当于-rlptgoD
    # -r 是递归 -l 是链接文件,意思是拷贝链接文件;-p 表示保持文件原有权限
    # -t 保持文件原有时间;-g 保持文#件原有用户组;-o 保持文件原有属主;-D 相当于块设备文件
    # -z 传输时压缩;
    # -P 传输进度;
    # -v 传输时的进度等信息,和-P有点关系,自己试试。可以看文档;
    # 同步
    rsync -avzP david@172.16.1.135::davidhome /tmp/david/
    # 客户端数据和服务器端数据保持一致
    rsync -avzP --delete david@172.16.1.135::davidhome /tmp/david/
    # 指定传输时候的密码文件,密码文件权限 600
    rsync -avzP --delete --password-file=/tmp/rsync.password david@172.16.1.135::davidhome /tmp/david/
    

    新增的步骤

    # 修改限制的IP地址
    hosts allow=172.16.0.0/255.255.0.0 192.168.1.0/255.255.255.0 10.0.1.0/255.255.255.0
    # 创建用户
    useradd -m -G wheel -s /bin/bash david
    # 修改目录所属组
    chown david:david /home/david
    # 修改目录权限
    chmod 775 /home/david/ -R
    
    # 自动同步
    
    crontab -e
    
    * * * * * /usr/bin/rsync -azv --delete rsync://yancho@192.168.1.107/test /home/backup/ --password-file=/etc/test
    ## 密码设置
    #vi /etc/test123                         #指定访问密码#chmod 600 /etc/test
    

    遇到的问题

    selinux的问题

    rsync: opendir "." (in davidhome) failed: Permission denied (13)
    
    # 需要关闭selinux
    

    文章参考:CentOS 6.5下rsync服务器安装配置

    相关文章

      网友评论

          本文标题:Rsync 服务器安装配置

          本文链接:https://www.haomeiwen.com/subject/jqxetxtx.html