前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >学习笔记0420----文件同步工具-rsync

学习笔记0420----文件同步工具-rsync

作者头像
嘻哈记
发布2022-01-11 10:49:15
7130
发布2022-01-11 10:49:15
举报
文章被收录于专栏:运维学习交流运维学习交流

文件同步工具

预习内容

10.28 rsync工具介绍 10.29/10.30 rsync常用选项 10.31 rsync通过ssh同步 10.32/10.33 rsync通过服务同步 10.34 linux系统日志 10.35 screen工具 扩展

  1. Linux日志文件总管logrotate http://linux.cn/article-4126-1.html
  2. xargs用法详解 http://blog.csdn.net/zhangfn2011/article/details/6776925

1.rsync工具介绍

Linux系统下有很多数据备份工具, 常用的是rsync,从字面意思理解为remote sync (远程同步). rsync不仅可以远程同步数据(类似于scp),而且可以本地同步数据(类似于cp),但不同于cp或者scp的一点是, 它不会覆盖以前的数据 (如果数据已经存在),而是先判断已经存在的数据和新数据的差异,只有数据不同时才会把不相同的部分覆盖。

Rsync是-款快速且功能非常多的文件复制工具。它可以在本地复制,通过任何远程shell或从另-台主机复制到从远程 rsync守护进程。它提供了大量的选项控制其行为的每个方面并允许非常灵活指定要复制的文件集。它是着名的其增量转换算法,可减少发送的数据量通过发远只有来源之间的差异在网络上文件和目标中的现有文件.。Rsync很广泛用于备份和镜像以及作为改进的复制命令日常使用。

系统最小化安装是不包含rsync这个命令的,所以先安装下rsync:

代码语言:javascript
复制
[root@linux-02 ~]# yum install -y rsync-3.1.2-4.el7.x86_64
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.zju.edu.cn
 * extras: mirrors.163.com
 * updates: mirrors.163.com
base                                                                                                | 3.6 kB  00:00:00     
extras                                                                                              | 3.4 kB  00:00:00     
updates                                                                                             | 3.4 kB  00:00:00     
正在解决依赖关系
--> 正在检查事务
---> 软件包 rsync.x86_64.0.3.1.2-4.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

===========================================================================================================================
 Package                    架构                        版本                               源                         大小
===========================================================================================================================
正在安装:
 rsync                      x86_64                      3.1.2-4.el7                        base                      403 k

事务概要
===========================================================================================================================
安装  1 软件包

总下载量:403 k
安装大小:815 k
Downloading packages:
rsync-3.1.2-4.el7.x86_64.rpm                                                                        | 403 kB  00:00:04     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : rsync-3.1.2-4.el7.x86_64                                                                               1/1 
  验证中      : rsync-3.1.2-4.el7.x86_64                                                                               1/1 

已安装:
  rsync.x86_64 0:3.1.2-4.el7                                                                                               

完毕!

使用rsync命令复制本地的文件到本地其他目录,-a包含多个选项, -v 选项是可视化过程,可以查看到发送的文件大小,接收了多大,速度,文件总大小,加速了多少时间。

代码语言:javascript
复制
[root@linux-02 ~]# rsync -av /root/a.txt  /tmp/1.txt
sending incremental file list
a.txt

sent 372 bytes  received 35 bytes  814.00 bytes/sec
total size is 282  speedup is 0.69

[root@linux-02 ~]# rsync -av /root/111/  /tmp/111
sending incremental file list
./

sent 43 bytes  received 19 bytes  124.00 bytes/sec
total size is 0  speedup is 0.00

rsync除了可以复制本地的文件和目录,还可以远程复制,目标机器必须也要安装了rsync,命令用法如下,复制本机的文件到另外一个机器。

代码语言:javascript
复制
[root@linux-02 ~]# rsync -av  /root/a.txt  root@192.168.141.130:/root/
The authenticity of host '192.168.141.130 (192.168.141.130)' can't be established.
RSA key fingerprint is SHA256:QbOsyZRk1yI6nmj7FKmNO8WxmrY9phyOACZtjFylZgA.
RSA key fingerprint is MD5:1e:0b:98:01:4c:14:23:25:e9:16:4e:ea:38:30:9b:f4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.141.130' (RSA) to the list of known hosts.
root@192.168.141.130's password: 
sending incremental file list
a.txt

sent 367 bytes  received 34 bytes  89.11 bytes/sec
total size is 282  speedup is 0.70
[root@linux-02 ~]# 

rsync的命令可以复制本机文件到本机,还可以复制本机文件到远程机器,具体格式如下:

代码语言:javascript
复制
 rsync格式  SRC 源文件  DEST目标文件
 rsync [OPTION] … SRC   DEST
 rsync [OPTION] … SRC   [user@]host:DEST
 rsync [OPTION] … [user@]host:SRC   DEST
 rsync [OPTION] … SRC   [user@]host::DEST
 rsync [OPTION] … [user@]host::SRC   DEST

2.rsync 常用选项

选项

含义

-a

包含-rtplgoD

-r

同步目录时要加上,类似cp时的-r选项

-v

同步时显示一些信息,让我们知道同步的过程

-l

保留软连接

-L

加上该选项后,同步软链接时会把源文件给同步

-p

保持文件的权限属性

-o

保持文件的属主

-g

保持文件的属组

-D

保持设备文件信息

-t

保持文件的时间属性

–delete

删除DEST中SRC没有的文件

–exclude

过滤指定文件,如–exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不同步

-P

显示同步过程,比如速率,比-v更加详细

-u

加上该选项后,如果DEST中的文件比SRC新,则不同步

-z

传输时压缩

3.rsync通过ssh同步

默认在使用远程软件去连接服务器的时候使用的是22端口,可以使用下面的方式使用rsync同步文件。

代码语言:javascript
复制
## 通过ssh把文件从A级器推送到B机器 ##
[root@linux-01 ~]# rsync -avP /root/ceshi/  192.168.241.89:/tmp/rsync/
The authenticity of host '192.168.241.89 (192.168.241.89)' can't be established.
ECDSA key fingerprint is SHA256:phzPMTk8az1R0dk6tK3c1m00ux7VDz3WGCnSYUbm6Zw.
ECDSA key fingerprint is MD5:e8:bc:cd:c1:ed:6c:68:9e:0e:99:45:a2:1c:05:64:f1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.241.89' (ECDSA) to the list of known hosts.
root@192.168.241.89's password: 
sending incremental file list
created directory /tmp/rsync
./
pass.txt
          2,319 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=10/12)
firewalld/
firewalld/firewalld.conf
          2,006 100%  979.49kB/s    0:00:00 (xfr#2, to-chk=8/12)
firewalld/lockdown-whitelist.xml
            271 100%  132.32kB/s    0:00:00 (xfr#3, to-chk=7/12)
firewalld/helpers/
firewalld/icmptypes/
firewalld/ipsets/
firewalld/services/
firewalld/zones/
firewalld/zones/public.xml
            315 100%  153.81kB/s    0:00:00 (xfr#4, to-chk=1/12)
firewalld/zones/public.xml.old
            315 100%  153.81kB/s    0:00:00 (xfr#5, to-chk=0/12)

sent 5,815 bytes  received 179 bytes  1,332.00 bytes/sec
total size is 5,226  speedup is 0.87
[root@linux-01 ~]# 

## 通过ssh把文件从B机器拉回到A机器 ##
[root@linux-01 ~]# rsync -avP   192.168.241.89:/tmp/rsync/pass.txt    ./
root@192.168.241.89's password: 
receiving incremental file list
pass.txt
          2,319 100%    2.21MB/s    0:00:00 (xfr#1, to-chk=0/1)

sent 43 bytes  received 2,413 bytes  701.71 bytes/sec
total size is 2,319  speedup is 0.94
[root@linux-01 ~]# ls
anaconda-ks.cfg  ceshi  pass.txt
[root@linux-01 ~]# 

如果服务的的ssh不是22端口该如何操作呢,需要添加一个-e的选项,双引号中添加上ssh -p 端口号,这样就可以使用rsync来同步操作了。

代码语言:javascript
复制
[root@linux-01 ~]# rsync -avP -e "ssh -p 22"  /root/ceshi/  192.168.241.89:/tmp/rsync/
root@192.168.241.89's password: 
sending incremental file list

sent 341 bytes  received 18 bytes  102.57 bytes/sec
total size is 5,226  speedup is 14.56
[root@linux-01 ~]# 

其实ssh -p 22 192.168.241.89就是远程登录到服务器,测试如下

代码语言:javascript
复制
## 在linux-01机器上使用命令远程登录linux-02机器 ##
[root@linux-01 ~]# ssh -p 22 192.168.241.89
root@192.168.241.89's password: 
Last login: Sat Apr 20 10:03:05 2019 from 192.168.241.1
welcome to linux!
[root@linux-02 ~]# 

4.rsync通过服务同步

  • 解决思路
  • 1.修改A机器的配置文件/etc/rsyncd.conf
  • 2.启动服务rsync --daemon
  • 3.传输格式:rsync -av test1/ 192.168.241.88::module/dir/
    • 同步报错 :路由是否同,端口是否同,是否是防火墙问题。

rsyncd.conf配置文件详解 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。 –port 指定端口 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

server端在机器A(ip地址192.168.241.88),在其上添加修改配置文件,启动rsync服务,查看服务的监听端口

代码语言:javascript
复制
[root@linux-01 ~]# vim /etc/rsyncd.conf 
# 在配置文件后面追加下列内容
port=873   //指定rsync的端口号,如果修改需要重新启动服务
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.241.88  //地址填写本机
[rsync]
path=/tmp/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.241.88   //需要填写传输到此机器文件的源机器的ip地址,下文因为此处填写错误,报错。

[root@linux-01 ~]# rsync --daemon
[root@linux-01 ~]# ps aux | grep rsync
root       8954  0.0  0.0 114744   568 ?        Ss   10:57   0:00 rsync --daemon
root       8964  0.0  0.0 112724   984 pts/1    S+   10:58   0:00 grep --color=auto rsync
[root@linux-01 ~]# 
[root@linux-01 ~]# netstat  -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 192.168.241.88:873      0.0.0.0:*               LISTEN      8954/rsync          
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      7232/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      6844/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      6849/cupsd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      7199/master         
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      7516/sshd: root@pts 
tcp        0      0 127.0.0.1:6011          0.0.0.0:*               LISTEN      8196/sshd: root@pts 
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      6844/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      6849/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      7199/master         
tcp6       0      0 ::1:6010                :::*                    LISTEN      7516/sshd: root@pts 
tcp6       0      0 ::1:6011                :::*                    LISTEN      8196/sshd: root@pts 
[root@linux-01 ~]# 

机器B同步文件到机器A,发现在同步的时候报错,提示没有路由,查看下ping A机器的ip地址是否可以通;可以通需要查看防火墙的放行规则;我们暂且把firewalld给停掉,同时把机器A的防火墙也停掉。这样做,发现在运行命令的还是有报错,一点是我们配置文件当中的目录没有创建,另一点是配置文件中的"hosts allow " 所填写的ip地址错误,修改即可。

代码语言:javascript
复制
[root@linux-02 ~]# rsync -av /root/rsync/   192.168.241.88::rsync/tb89
rsync: failed to connect to 192.168.241.88 (192.168.241.88): No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(125) [sender=3.1.2]

## 查看是否可以ping通 ##
[root@linux-02 ~]# ping 192.168.241.88
PING 192.168.241.88 (192.168.241.88) 56(84) bytes of data.
64 bytes from 192.168.241.88: icmp_seq=1 ttl=64 time=0.636 ms
64 bytes from 192.168.241.88: icmp_seq=2 ttl=64 time=0.758 ms
^C
--- 192.168.241.88 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1014ms
rtt min/avg/max/mdev = 0.636/0.697/0.758/0.061 ms
[root@linux-02 ~]# 

## 查看防火墙的规则 ##
[root@linux-02 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     udp  --  virbr0 *       0.0.0.0/0            0.0.0.0/0            udp dpt:53
    0     0 ACCEPT     tcp  --  virbr0 *       0.0.0.0/0            0.0.0.0/0            tcp dpt:53
    0     0 ACCEPT     udp  --  virbr0 *       0.0.0.0/0            0.0.0.0/0            udp dpt:67
    0     0 ACCEPT     tcp  --  virbr0 *       0.0.0.0/0            0.0.0.0/0            tcp dpt:67
  931 86398 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
    2   146 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
   95 14462 INPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
   95 14462 INPUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
   95 14462 INPUT_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID
   87 13974 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 ACCEPT     all  --  *      virbr0  0.0.0.0/0            192.168.122.0/24     ctstate RELATED,ESTABLISHED
    0     0 ACCEPT     all  --  virbr0 *       192.168.122.0/24     0.0.0.0/0           
    0     0 ACCEPT     all  --  virbr0 virbr0  0.0.0.0/0            0.0.0.0/0           
    0     0 REJECT     all  --  *      virbr0  0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
    0     0 REJECT     all  --  virbr0 *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
    0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 FORWARD_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FORWARD_IN_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FORWARD_IN_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FORWARD_OUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FORWARD_OUT_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 666 packets, 73523 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     udp  --  *      virbr0  0.0.0.0/0            0.0.0.0/0            udp dpt:68
  768 90999 OUTPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain FORWARD_IN_ZONES (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 FWDI_public  all  --  ens33  *       0.0.0.0/0            0.0.0.0/0           [goto] 
    0     0 FWDI_public  all  --  +      *       0.0.0.0/0            0.0.0.0/0           [goto] 

Chain FORWARD_IN_ZONES_SOURCE (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD_OUT_ZONES (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 FWDO_public  all  --  *      ens33   0.0.0.0/0            0.0.0.0/0           [goto] 
    0     0 FWDO_public  all  --  *      +       0.0.0.0/0            0.0.0.0/0           [goto] 

Chain FORWARD_OUT_ZONES_SOURCE (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD_direct (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FWDI_public (2 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 FWDI_public_log  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FWDI_public_deny  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FWDI_public_allow  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain FWDI_public_allow (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FWDI_public_deny (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FWDI_public_log (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FWDO_public (2 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 FWDO_public_log  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FWDO_public_deny  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FWDO_public_allow  all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain FWDO_public_allow (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FWDO_public_deny (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FWDO_public_log (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT_ZONES (1 references)
 pkts bytes target     prot opt in     out     source               destination         
   74  9875 IN_public  all  --  ens33  *       0.0.0.0/0            0.0.0.0/0           [goto] 
    9  1783 IN_public  all  --  +      *       0.0.0.0/0            0.0.0.0/0           [goto] 

Chain INPUT_ZONES_SOURCE (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT_direct (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain IN_public (2 references)
 pkts bytes target     prot opt in     out     source               destination         
   95 14462 IN_public_log  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
   95 14462 IN_public_deny  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
   95 14462 IN_public_allow  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain IN_public_allow (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    8   488 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22 ctstate NEW

Chain IN_public_deny (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain IN_public_log (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT_direct (1 references)
 pkts bytes target     prot opt in     out     source               destination         
[root@linux-02 ~]# systemctl stop firewalld.service 
[root@linux-02 ~]# 

## 把A机器的防火墙也停止掉 ##
[root@linux-01 ~]# systemctl  stop firewalld.service 
[root@linux-01 ~]# 

## 重新执行同步命令,发现还是有报错 ##
[root@linux-02 ~]# rsync -av /root/rsync/   192.168.241.88::rsync/tb89
@ERROR: access denied to rsync from UNKNOWN (192.168.241.89)
rsync error: error starting client-server protocol (code 5) at main.c(1648) [sender=3.1.2]
[root@linux-02 ~]# 

## 上面出现报错的原因可能是:https://www.jb51.net/article/31920.htm ,在做了配置文件没有添加这个目录,添加下目录 ##
[root@linux-01 ~]# mkdir /tmp/rsync/
[root@linux-01 ~]# chmod 777 /tmp/rsync/
[root@linux-01 ~]# 

## 提示还是报错,最后解决原因是配置文件的(hosts allow=192.168.241.88) 这个错了,应该为B机器的ip地址,修改完毕,在使用传输命令的时候提示输入密码,说明我们已经配置成功 ## 
[root@linux-02 ~]# rsync -av /root/rsync/   192.168.241.88::rsync/tb89
@ERROR: access denied to rsync from UNKNOWN (192.168.241.89)
rsync error: error starting client-server protocol (code 5) at main.c(1648) [sender=3.1.2]

[root@linux-02 ~]# rsync -av /root/rsync/   192.168.241.88::rsync/tb89
Password: 

配置文件当中我们有指定密码的文件,该参数连同上面的参数如果不指定,则不使用密码验证,我们可以把配置文件中的“auth users=test 和 secrets file=/etc/rsyncd.passwd”先给注释,可以直接同步文件,此处如果不注释掉,相当于指定用户密码来进行同步,是比较安装的,用户密码配置文件格式:用户:密码。

代码语言:javascript
复制
[root@linux-01 ~]# vim  /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

# uid = nobody
# gid = nobody
# use chroot = yes

# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#        path = /home/ftp
#        comment = ftp export area
port=873
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.241.88
[rsync]
path=/tmp/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.241.89

## 在机器B上执行同步命令,可以查看到同步的文件已经同步完成 ##
[root@linux-02 ~]# rsync -av /root/rsync/   192.168.241.88::rsync/tb89
sending incremental file list
created directory /tb89
./
pass.txt
firewalld/
firewalld/firewalld.conf
firewalld/lockdown-whitelist.xml
firewalld/helpers/
firewalld/icmptypes/
firewalld/ipsets/
firewalld/services/
firewalld/zones/
firewalld/zones/public.xml
firewalld/zones/public.xml.old

sent 5,816 bytes  received 174 bytes  570.48 bytes/sec
total size is 5,226  speedup is 0.87
[root@linux-02 ~]# 

配置文件当中修改list为false,可以隐藏模块名称

代码语言:javascript
复制
[root@linux-02 ~]# rsync -av /root/rsync/   192.168.241.88::
rsync          	
[root@linux-02 ~]# 

## 修改list=false 即可隐藏这个模块名称 ##

在客户端B上设置“–password-file=/etc/rsync_passwd.txt”不需要输入密码同步;这个操作适用于脚本,因为是需要和用户打交道的地方。

代码语言:javascript
复制
## 创建一个密码文件,只需要输入server端的密码,保存即可 ##
[root@linux-02 ~]# vim /etc/rsync_passwd.txt
[root@linux-02 ~]# chmod 600 /etc/rsync_passwd.txt 
[root@linux-02 ~]# rsync -avP /root/rsync/ --password-file=/etc/rsync_passwd.txt    192.168.241.88::rsync/tb02
sending incremental file list
created directory /tb02
./
pass.txt
          2,319 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=10/12)
firewalld/
firewalld/firewalld.conf
          2,006 100%  979.49kB/s    0:00:00 (xfr#2, to-chk=8/12)
firewalld/lockdown-whitelist.xml
            271 100%  132.32kB/s    0:00:00 (xfr#3, to-chk=7/12)
firewalld/helpers/
firewalld/icmptypes/
firewalld/ipsets/
firewalld/services/
firewalld/zones/
firewalld/zones/public.xml
            315 100%  153.81kB/s    0:00:00 (xfr#4, to-chk=1/12)
firewalld/zones/public.xml.old
            315 100%  153.81kB/s    0:00:00 (xfr#5, to-chk=0/12)

sent 5,816 bytes  received 174 bytes  3,993.33 bytes/sec
total size is 5,226  speedup is 0.87

5.系统日志

日志记最了系统每天发生的各种各样的事情,七以瞄测系统状况、排查军就故障等,你可以通过他来检查错误发生的原因. 日志的主要功能是审计和监测,还可以实时的监测系统状态, 监测和追踪侵入者等等.

5.1 系统日志切割

系统日常日志/var/log/message;它是核心系统日志文件,包含了系统启动时的引导消息,以及系统运行时的其他状态消息。IO错误、 网络错误和真他系统错误都会记录到这个文件中. 另外其他信息, 比如某个人的身份切换为root以及用户自定义安装的软件(apache)的日志也会在这里列出。

代码语言:javascript
复制
[root@linux-01 ~]# ll /var/log/messages
-rw-------. 1 root root 11680 4月  20 17:20 /var/log/messages

[root@linux-01 ~]# du -sh !$
du -sh /var/log/messages
12K	/var/log/messages

[root@linux-01 ~]# ls /var/log/messages*
/var/log/messages  /var/log/messages-20190303  /var/log/messages-20190312  /var/log/messages-20190420
[root@linux-01 ~]# 

在查看日志的时候,可以查看到日志被切割,这是什么原因呢?是因为系统中有一个logrotate服务,会自动切割日志,防止一个日志文件无限制的增加。

代码语言:javascript
复制
[root@linux-01 ~]# cat /etc/logrotate.conf 
# see "man logrotate" for details
# rotate log files weekly
weekly  //每周切割一次

# keep 4 weeks worth of backlogs
rotate 4  //4周一次轮换

# create new (empty) log files after rotating old ones
create   //创建新的

# use date as a suffix of the rotated file
dateext  //以它为后缀名

# uncomment this if you want your log files compressed
#compress  //是否需要压缩,压缩文件格式为.tar.gz

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
	minsize 1M
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

# system-specific logs may be also be configured here.  //系统特定的日志就在此处配置


[root@linux-01 ~]# ls /etc/logrotate.d/
bootlog  chrony  cups  glusterfs  iscsiuiolog  libvirtd  libvirtd.qemu  numad  ppp  psacct  samba  sssd  syslog  wpa_supplicant  yum
  • syslog文件会为cron, maillog, messages, secure, spoole这几个日志进行切割
  • messages日志是由syslogd服务决定的,所以kill -HUP就会重新加载这个日志
  • 还有一个脚本,shell命令行,在把日志切割后(挪走),改名字生成新的日志
  • Linux系统有-个特点,-个服务写-个文件的时候,并不是按照文件名去写的,而是根据inode来写的
代码语言:javascript
复制
[root@linux-01 ~]# cat /etc/logrotate.d/syslog 
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
    missingok
    sharedscripts
    postrotate
	/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript
}
[root@linux-01 ~]# 

5.2 dmesg命令和日志

dmesg命令显示linux内核的环形缓冲区信息,我们可以从中获得诸如系统架构、cpu、挂载的硬件,RAM等多个运行级别的大量的系统信息。当计算机启动时,系统内核(操作系统的核心部分)将会被加载到内存中。在加载的过程中会显示很多的信息,在这些信息中我们可以看到内核检测硬件设备。

代码语言:javascript
复制
## 这个日志是保存在内存中的,如果网卡,硬盘硬盘设备损坏,都会记录到这个日志中 ##
[root@linux-01 ~]# dmesg  >  1.txt &&  tail -10f 1.txt  
[   21.705910] virbr0: port 1(virbr0-nic) entered listening state
[   21.706034] IPv6: ADDRCONF(NETDEV_UP): virbr0: link is not ready
[   21.939927] virbr0: port 1(virbr0-nic) entered disabled state
[ 4475.234487] Ebtables v2.0 unregistered
[ 6132.941872] hrtimer: interrupt took 30718844 ns
[ 6138.527461] sched: RT throttling activated
[ 7236.889644] e1000: ens33 NIC Link is Down
[ 7240.894546] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[11215.055445] e1000: ens33 NIC Link is Down
[11228.655532] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None

## dmesg -c 清空内存中的当前日志 ##
[root@linux-01 ~]# dmesg -c  | head   // 并没有日志产生 
[root@linux-01 ~]# 

查看下/var/log/dmesg 日志,包含内核缓冲信息(kernel ring buffer)。在系统启动时,会在屏幕上显示许多与硬件有关的信息。

代码语言:javascript
复制
[root@linux-01 ~]# tail -20f  /var/log/dmesg
[   12.083499] XFS (sda2): Ending clean mount
[   12.222580] input: PC Speaker as /devices/platform/pcspkr/input/input5
[   12.712893] cryptd: max_cpu_qlen set to 1000
[   12.789277] AVX version of gcm_enc/dec engaged.
[   12.789281] AES CTR mode by8 optimization enabled
[   12.903329] alg: No test for __gcm-aes-aesni (__driver-gcm-aes-aesni)
[   12.923177] alg: No test for __generic-gcm-aes-aesni (__driver-generic-gcm-aes-aesni)
[   13.201356] ppdev: user-space parallel port driver
[   13.357443] floppy0: no floppy controllers found
[   13.357506] work still pending
[   13.831001] device-mapper: uevent: version 1.0.3
[   13.831141] device-mapper: ioctl: 4.37.1-ioctl (2018-04-03) initialised: dm-devel@redhat.com
[   14.077935] type=1305 audit(1555725356.320:3): audit_pid=6161 old=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:auditd_t:s0 res=1
[   14.131453] RPC: Registered named UNIX socket transport module.
[   14.131457] RPC: Registered udp transport module.
[   14.131458] RPC: Registered tcp transport module.
[   14.131460] RPC: Registered tcp NFSv4.1 backchannel transport module.
[   14.966374] Bluetooth: BNEP (Ethernet Emulation) ver 1.3
[   14.966378] Bluetooth: BNEP filters: protocol multicast
[   14.966385] Bluetooth: BNEP socket layer initialized

5.3 last命令

  • last命令是调用的/var/log/wtmp 日志
  • 日志内容包含:用户、登录终端、源IP、时间、登录时长
  • /var/log/wtmp日志是-个二进制文件,不能直接查看的,只能用last命令去查看
代码语言:javascript
复制
[root@linux-01 ~]# last | tail -20f
reboot   system boot  3.10.0-957.el7.x Tue Mar 12 20:23 - 22:47 (1+02:23)   
root     pts/0        192.168.241.1    Sun Mar  3 11:40 - crash (9+08:42)   
root     pts/0        192.168.241.1    Fri Mar  1 21:11 - 23:24  (02:13)    
reboot   system boot  3.10.0-957.el7.x Fri Mar  1 21:11 - 22:47 (12+01:36)  
root     pts/1        192.168.241.1    Thu Feb 28 23:32 - crash  (21:38)    
whdong00 :0           :0               Thu Feb 28 21:25 - crash  (23:45)    
whdong00 pts/1        :0               Thu Feb 28 21:22 - 21:22  (00:00)    
root     pts/0        192.168.241.1    Thu Feb 28 21:21 - crash  (23:49)    
whdong00 :0           :0               Thu Feb 28 21:21 - 21:25  (00:04)    
root     tty1                          Thu Feb 28 21:17 - 21:17  (00:00)    
reboot   system boot  3.10.0-957.el7.x Thu Feb 28 21:16 - 22:47 (13+01:30)  
root     pts/0        192.168.241.1    Thu Feb 28 21:01 - crash  (00:15)    
root     tty1                          Thu Feb 28 20:52 - 21:01  (00:09)    
reboot   system boot  3.10.0-957.el7.x Thu Feb 28 20:52 - 22:47 (13+01:54)  
root     tty1                          Thu Feb 28 20:46 - crash  (00:05)    
reboot   system boot  3.10.0-957.el7.x Thu Feb 28 20:46 - 22:47 (13+02:00)  
root     tty1                          Fri Mar  1 04:33 - 20:46  (-7:-47)   
reboot   system boot  3.10.0-957.el7.x Fri Mar  1 04:33 - 20:46  (-7:-46)   

wtmp begins Fri Mar  1 04:33:09 2019
[root@linux-01 ~]# 

5.4 lastb 命令查看登录失败的用户

  • lastb命令是调用的/var/log/btmp 日志
  • 日志内容包含:用户、登录终端、登录IP、时间、登录时长
  • /var/log/btmp日志也是-个二进制文件,不能直接查看的,只能用lastb命令去查看
代码语言:javascript
复制
[root@linux-01 ~]# lastb
root     ssh:notty    192.168.241.89   Sat Apr 20 17:58 - 17:58  (00:00)    
root     ssh:notty    192.168.241.89   Sat Apr 20 17:58 - 17:58  (00:00)    

btmp begins Sat Apr 20 17:58:48 2019
[root@linux-01 ~]# 

5.5 系统安全日志

/var/log/secure比如登录操作系统,验证成功会在这里记录-个日志,失败也会去记录

代码语言:javascript
复制
[root@linux-01 ~]# tail -20f  /var/log/secure
Apr 20 11:10:27 linux-01 polkitd[6209]: Registered Authentication Agent for unix-process:9129:448474 (system bus name :1.85 [/usr/bin/pkttyagent --notify-fd 5 --fallback], object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale zh_CN.UTF-8)
Apr 20 11:10:28 linux-01 polkitd[6209]: Unregistered Authentication Agent for unix-process:9129:448474 (system bus name :1.85, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale zh_CN.UTF-8) (disconnected from bus)
Apr 20 11:56:29 linux-01 sshd[7516]: pam_unix(sshd:session): session closed for user root
Apr 20 12:40:16 linux-01 sshd[9750]: Accepted password for root from 192.168.241.1 port 55096 ssh2
Apr 20 12:40:17 linux-01 sshd[9750]: pam_unix(sshd:session): session opened for user root by (uid=0)
Apr 20 16:37:34 linux-01 sshd[10429]: Accepted password for root from 192.168.241.1 port 55185 ssh2
Apr 20 16:37:34 linux-01 sshd[10429]: pam_unix(sshd:session): session opened for user root by (uid=0)
Apr 20 17:30:20 linux-01 sshd[8196]: pam_unix(sshd:session): session closed for user root
Apr 20 17:58:46 linux-01 unix_chkpwd[11337]: password check failed for user (root)
Apr 20 17:58:46 linux-01 sshd[11335]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.241.89  user=root
Apr 20 17:58:46 linux-01 sshd[11335]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root"
Apr 20 17:58:48 linux-01 sshd[11335]: Failed password for root from 192.168.241.89 port 33498 ssh2
Apr 20 17:58:50 linux-01 unix_chkpwd[11338]: password check failed for user (root)
Apr 20 17:58:50 linux-01 sshd[11335]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root"
Apr 20 17:58:52 linux-01 sshd[11335]: Failed password for root from 192.168.241.89 port 33498 ssh2
Apr 20 17:58:52 linux-01 sshd[11335]: Connection closed by 192.168.241.89 port 33498 [preauth]
Apr 20 17:58:52 linux-01 sshd[11335]: PAM 1 more authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.241.89  user=root

6. screen工具----虚拟终端

当我们需要执行一个任务很长时间的时候,我们不可以与从远程终端当中退出,有什么办法可以让任务继续进行,并且任务不会断开呢,可以使用“nohup command >> 1.log &”这样一种方法把任务放置于后台进行,当想查看屏幕上的信息的时候是无法查看到,这就可以使用screen工具来开启一个新的虚拟终端。系统默认screen是没有安装的,我们需要安装下。

代码语言:javascript
复制
[root@linux-01 ~]# yum install -y screen
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * extras: mirrors.cn99.com
 * updates: mirrors.cn99.com
base                                                                                                                       | 3.6 kB  00:00:00     
extras                                                                                                                     | 3.4 kB  00:00:00     
updates                                                                                                                    | 3.4 kB  00:00:00     
正在解决依赖关系
--> 正在检查事务
---> 软件包 screen.x86_64.0.4.1.0-0.25.20120314git3c2946.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

==================================================================================================================================================
 Package                     架构                        版本                                                     源                         大小
==================================================================================================================================================
正在安装:
 screen                      x86_64                      4.1.0-0.25.20120314git3c2946.el7                         base                      552 k

事务概要
==================================================================================================================================================
安装  1 软件包

总下载量:552 k
安装大小:914 k
Downloading packages:
screen-4.1.0-0.25.20120314git3c2946.el7.x86_64.rpm                                                                         | 552 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : screen-4.1.0-0.25.20120314git3c2946.el7.x86_64                                                                                1/1 
  验证中      : screen-4.1.0-0.25.20120314git3c2946.el7.x86_64                                                                                1/1 

已安装:
  screen.x86_64 0:4.1.0-0.25.20120314git3c2946.el7                                                                                                

完毕!
代码语言:javascript
复制
## 运行命令screen即可开启一个虚拟终端 ##
[root@linux-01 ~]# screen 

## 在虚拟终端中运行一个vmstat 1 ,使用ctrl+a+d可以退出虚拟终端## 
[root@linux-01 ~]# screen  
[detached from 11624.pts-2.linux-01]

## 如何回到这个终端呢 ##
[root@linux-01 ~]# screen  -ls   //查看虚拟终端有几个
There is a screen on:
	11624.pts-2.linux-01	(Detached)
1 Socket in /var/run/screen/S-root.

[root@linux-01 ~]# screen  -r   11624  // 使用-r选项加上终端的ID号即可回到终端

## 如果有多个screen很久了,不知道那个是我需要的,该怎么办呢,可以在创建screen的时候加上-S选项,指定一个名字 ## 
[root@linux-01 ~]# screen  -S  xihaji 
[detached from 11755.xihaji]
[root@linux-01 ~]# screen  -ls
There are screens on:
	11755.xihaji	(Detached)
	11691.pts-2.linux-01	(Detached)
	11624.pts-2.linux-01	(Detached)
3 Sockets in /var/run/screen/S-root.

[root@linux-01 ~]#  screen -r 11755  //这样可以很方便的找到自己的的screen 

课后总结

1.rsync的使用场景

2.日志切割工具logrotate https://linux.cn/article-4126-1.html

3.xargs https://blog.csdn.net/gb4215287/article/details/78037520

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-04-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文件同步工具
  • 预习内容
    • 1.rsync工具介绍
      • 2.rsync 常用选项
        • 3.rsync通过ssh同步
          • 4.rsync通过服务同步
            • 5.系统日志
              • 5.1 系统日志切割
              • 5.2 dmesg命令和日志
              • 5.3 last命令
              • 5.4 lastb 命令查看登录失败的用户
              • 5.5 系统安全日志
            • 6. screen工具----虚拟终端
            • 课后总结
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档