rsync工具介绍、rsync常用选项、rsync通过ssh同步、rsync通过服务同步、linux系统日志、screen工具

一、rsync工具介绍

安装rsync包——>yum install -y rsync
rsync命令,它是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。

比如,有A、B目录,想要把A目录里面的数据拷贝到B目录下去,并且A目录中数据一直在更新
需求,每小时拷贝一次
实现,可使用rsync命令
优势:实现增量的拷贝,并支持远程

rsync -av /etc/passwd /tmp/1.txt //把/etc/passwd文件拷贝到/tmp下并改名为1.txt

-a参数中包含了很多选项,后面会详细介绍
-v查看到可视化过程
查看到发送了多少个字节
多少字节每秒
文件一共有多大
速度是多少

rsync命令

[root@han01 ~]# rsync -av /etc/passwd /tmp/1.txt    //把/etc/passwd文件拷贝到/tmp下并改名为1.txt 
sending incremental file list
passwd

sent 957 bytes  received 31 bytes  1976.00 bytes/sec
total size is 883  speedup is 0.89
[root@han01 ~]#

rsync命令,远程命令同步/拷贝
-比如,拷贝到对方机器root用户下 - 然后在root后加IP,并用 : 冒号分开,再跟文件的根路径

- 并输入root用户的密码

rsync -av /etc/passwd [email protected]:/tmp/1.txt //将/etc/passwd文件拷贝到root用户192.168.202.130IP地址下,并用 : 冒号分开,再跟文件的根路径

[root@han01 ~]# rsync -av /etc/passwd [email protected]:/tmp/1.txt    //将/etc/passwd文件拷贝到root用户192.168.202.130IP地址下,并用 : 冒号分开,再跟文件的根路径
root@192.168.202.130's password:         //这里写用户的密码
sending incremental file list

sent 31 bytes  received 12 bytes  0.43 bytes/sec
total size is 883  speedup is 20.53

rsync格式

rsync [OPTION] … SRC DEST

[OPTION]表示它的选项
SRC表示源目录
DEST表示目标目录,或者是目标文件

rsync [OPTION] … SRC [user@]host:DEST //拷贝到远程的服务器上去

user@可省略,那就会默认当前终端的用户

rsync [OPTION] … [user@]host:SRC DEST //先写远程的机器/目录,然后拷贝到本地的目录下
rsync [OPTION] … SRC [user@]host::DEST //这里的两个冒号,可以是目标,可以是源
rsync [OPTION] … [user@]host::SRC DEST

Linux文件同步工具-rsync

rsync常用选项
-a 包含-rtplgoD参数选项
-r 同步目录时要加上,类似cp时的-r选项
-v 同步时显示一些信息,让我们知道同步的过程
-l 保留软连接

若是拷贝的原目录里面有一个软链接文件,那这个软链接文件指向到了另外一个目录下
在加上-l,它会把软链接文件本身拷贝到目标目录里面去

-L 加上该选项后,同步软链接时会把源文件给同步
-p 保持文件的权限属性
-o 保持文件的属主
-g 保持文件的属组
-D 保持设备文件信息

/dev/sdb1 这样的设备文件有它的特殊性,如果不加-D 可能拷贝过去就是一个非常普通的文件,不能当设备来用

-t 保持文件的时间属性
–delete 删除DEST中SRC没有的文件
–exclude 过滤指定文件,如–exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不同步
-P 显示同步过程,比如速率,比-v更加详细
-u 加上该选项后,如果DEST中的文件比SRC新,则不同步
update
-z 传输时压缩

rsync命令,同步目录

rsync -av /root/111 /tmp/111_dest //同步一个目录
在同步目录的时候,在目录的最后面加一个斜杠/

[root@han01 ~]# ls 111
a.txt  haha  hanfeng
[root@han01 ~]# ls /tmp/
[root@han01 ~]# rsync -av /root/111/ /tmp/111_dest/    //同步一个目录
sending incremental file list
created directory /tmp/111_dest
./
a.txt
haha/
hanfeng/

sent 256312 bytes  received 42 bytes  512708.00 bytes/sec
total size is 256144  speedup is 1.00

在加入参数 -L 后,会把参数 -l 的含义给覆盖掉

-L会把软链接所指向的源文件给拷贝过去

[root@han01 ~]# rsync -avL /root/111/ /tmp/111_dest/
sending incremental file list

sent 88 bytes  received 14 bytes  204.00 bytes/sec
total size is 256144  speedup is 2511.22

rsync命令,删除目标中源文件中没有的内容

rsync -avL –delete /root/111/ /tmp/111_dest/

–delete会把多余的文件除去

[root@han01~]# ls 111/
a.txt  haha  hanfeng
[root@han01 ~]# ls /tmp/111_dest/
a.txt  haha  hanfeng
[root@han01 ~]# touch /tmp/111_dest/new.txt
[root@han01 ~]# rsync -avL --delete /root/111/ /tmp/111_dest/
sending incremental file list
./
deleting new.txt

sent 91 bytes  received 17 bytes  216.00 bytes/sec
total size is 256144  speedup is 2371.70
[root@han01 ~]# ls /tmp/111_dest/
a.txt  haha  hanfeng

rsync命令,过滤所有txt文件

[root@han01 ~]# rsync -avL --exclude "*.txt" /root/111/ /tmp/111_dest/
sending incremental file list
./
2.txt.swp
4913
haha/
hanfeng/

sent 184 bytes  received 61 bytes  490.00 bytes/sec
total size is 0  speedup is 0.00

可多次过滤文件

[root@han01 ~]# !rm
rm -rf /tmp/111_dest/*
[root@hf ~]# rsync -avL --exclude "*.txt" --exclude="2*" /root/111/ /tmp/111_dest/
sending incremental file list
./
4913
haha/
hanfeng/

sent 131 bytes  received 42 bytes  346.00 bytes/sec
total size is 0  speedup is 0.00

在添加文件后,再次同步,会只同步里面不相同的文件,而相同的文件则不会再次同步

[root@han01 ~]# cd 111
[root@han01 111]# touch 6.dest 123
[root@han01 111]# rsync -avL --exclude "*.txt" --exclude="2*" /root/111/ /tmp/111_dest/
sending incremental file list
./
123
6.dest

sent 187 bytes  received 55 bytes  484.00 bytes/sec
total size is 0  speedup is 0.00

rsync命令,参数-P

rsync -avP /root/111/ /tmp/111_dest/

在传输过程中,会告诉你传输了多少,传输的速度是多少

[root@han01 ~]# !rm
rm -rf /tmp/111_dest/*
[root@han01 ~]# rsync -avP /root/111/ /tmp/111_dest/
sending incremental file list
./
123
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=6/8)
2.txt.swp
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=5/8)
4913
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=4/8)
6.dest
           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=3/8)
a.txt
      256144 100%   53.26MB/s    0:00:00 (xfer#5, to-check=2/8)
haha/
hanfeng/

sent 256522 bytes  received 118 bytes  513280.00 bytes/sec
total size is 256144  speedup is 1.00

rsync命令,参数-u

[root@han01 ~]# cd /tmp/111_dest/
[root@han01 111_dest]# ls
123  2.txt.swp  4913  6.dest  a.txt  haha  hanfeng
[root@han01 111_dest]# vim 49134913中添加内容

[root@han01 111_dest]# rsync -avPu /root/111/ /tmp/111_dest/
sending incremental file list
./

sent 145 bytes  received 17 bytes  324.00 bytes/sec
total size is 256144  speedup is 1581.14
[root@han01 111_dest]# cat 4913
dsgsdfascs
dsafszcdrw
etfbcgrhc
cbcvbtyegvdgdh
gxdgdfhch
[root@han01 111_dest]# cat /root/111/4913

sync命令,参数-z

在远程传输很多文件的时候,加上-z 参数,可以节省带宽,增加速度的

[root@han01 111_dest]# rsync -avPz /root/111/ /tmp/111_dest/
sending incremental file list
4913
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=4/8)

sent 178 bytes  received 33 bytes  422.00 bytes/sec
total size is 256144  speedup is 1213.95

rsync通过ssh方式同步

rsync -av test1/ 192.168.133.132:/tmp/test2/

rsync -av -e “ssh -p 22” test1/ 192.168.133.132:/tmp/test2/

在两个虚拟机上都安装rsync包:yum install -y rsync
将文件传输到另一台虚拟机
在终端打开两个不同ip的虚拟机,并且两个虚拟机是可以互通ping通的
在han01的虚拟机中,ping另一台虚拟机

[root@han01 ~]# ping 192.168.74.130
PING 192.168.74.130 (192.168.74.130) 56(84) bytes of data.
64 bytes from 192.168.74.130: icmp_seq=1 ttl=64 time=1.42 ms
64 bytes from 192.168.74.130: icmp_seq=2 ttl=64 time=0.873 ms
^C
--- 192.168.74.130 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1004ms
rtt min/avg/max/mdev = 0.873/1.147/1.422/0.276 ms



在han02的虚拟机上,ping第一台虚拟机
[root@han02 ~]# ping 192.168.74.129
PING 192.168.74.129 (192.168.74.129) 56(84) bytes of data.
64 bytes from 192.168.74.129: icmp_seq=1 ttl=64 time=1.05 ms
64 bytes from 192.168.74.129: icmp_seq=2 ttl=64 time=0.725 ms
^C
--- 192.168.74.129 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.725/0.891/1.058/0.169 ms

在han01的终端虚拟机上,将文件传输到han02的虚拟机上
在han01的虚拟上,传文件到han02的虚拟机上

[root@han01 ~]# rsync -av /etc/passwd 192.168.74.130:/tmp/hanfeng.txt
root@192.168.74.130's password:          //输入192.168.74.130虚拟机的密码,就是hf-02的密码
sending incremental file list
passwd

sent 1100 bytes  received 31 bytes  323.14 bytes/sec
total size is 1026  speedup is 0.91



在han02虚拟机上查看
[root@han02 ~]# ls /tmp/hanfeng.txt
/tmp/hanfeng.txt
[root@han02 ~]# cat !$
cat /tmp/hanfeng.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
mysql:x:1000:1000::/home/mysql:/bin/bash
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin

rsync命令,将另一台虚拟机文件传输到本机上

将han02机器中的文件传输到本机上

[root@han01 ~]# rsync -avP 192.168.74.130:/tmp/hanfeng.txt /tmp/12345.txt
root@192.168.74.130's password:        
receiving incremental file list
hanfeng.txt
        1026 100% 1001.95kB/s    0:00:00 (xfer#1, to-check=0/1)

sent 30 bytes  received 1110 bytes  78.62 bytes/sec
total size is 1026  speedup is 0.90

rsync命令,参数-e 指定端口传输文件

rsync -avP -e “ssh -p 22” /etc/passwd 192.168.74.130:/tmp/hanfeng.txt //指定对方的22端口,就可以连接对面的22端口

[root@han01 ~]# rsync -avP -e "ssh -p 22" /etc/passwd 192.168.74.130:/tmp/hanfeng.txt
root@192.168.74.130's password: 
sending incremental file list

sent 31 bytes  received 12 bytes  7.82 bytes/sec
total size is 1026  speedup is 23.86

ssh命令

ssh -p 22 192.168.74.130 //连接ip为192.168.74.130的虚拟机

输入exit退出

[root@han01 ~]# ssh -p 22 192.168.74.130
root@192.168.74.130's password: 
Last login: Wed Dec  6 05:14:27 2017 from 192.168.74.1
[root@han02 ~]# exit
登出
Connection to 192.168.74.130 closed.


二、rsync 通过服务的方式同步

要编辑配置文件/etc/rsyncd.conf

启动服务rsync –daemon

格式:rsync -av test1/ 192.168.202.130::module/dir/

rsync同步文件

rsync 通过服务的方式同步,首先需要开启一个服务,是cs架构,客户端和服务端

服务端,开启一个rsync服务,并且一个端口,默认是873——>(端口是可以自定义的)

格式:rsync -av test1/ 192.168.133.130::module/dir/

在启动服务之前,首先要编辑配置文件,文件的默认地址是在 /etc/rsyncd.conf——>也可以更改路径,但是在更改路径后,

就需要在启动服务的时候,去rsync –daemon//conf=后跟路径

然后启动服务rsync –daemon


从han01机器的文件传输到han02机器

rsyncd.conf样例

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地址用空格隔开

首先在虚拟机han01上打开/etc/rsyncd.conf文件

[root@han01 ~]# vim /etc/rsyncd.conf

然后将上述代码复制到 /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

启动服务rsync –daemon

[root@han01 ~]# rsync --daemon

在han01虚拟机上检查服务是否已经启动

[root@han01 ~]# 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

在hf虚拟机上检查监听的端口是否为873

会看到监听的端口为873

这里并且指定监听的IP,若是不写监听的IP,就会绑定0.0.0.0,就是所有的网段,全部的IP

[root@han01 ~]# 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  

在hf虚拟机上检查path指定的路径

这里若是指定root,权限就会有可能不好把握,所以放在tmp目录下

修改/etc/rsyncd.conf目录中的

将path=/root/rsync改为path=/tmp/rsync

并新建目录mkdir=/tmp/rsync

[root@han01 ~]# vim /etc/rsyncd.conf

将path=/root/rsync改为path=/tmp/rsync

[root@han01 ~]# mkdir /tmp/rsync

在han01虚拟机上设置权限——>这里设置的777权限是为了方便测试

[root@han01 ~]# chmod 777 /tmp/rsync

在另一台虚拟机hf-02上,运行

rsync -avP /tmp/hanfeng.txt 192.168.202.130::test/hanfeng-02.txt

192.168.202.130它是hf虚拟机的IP

::后面跟模块的名称——>模块名称就是/etc/rsyncd.conf中设置的 test 模块

这里模块名称代表的路径就是/tmp/rsync

[root@han02 ~]# 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]

在hf-02虚拟机上,会提示没有路由到远程机器上去,遇到这种问题,首先在han02虚拟机上检查网络连通性

在hf-02虚拟机上,会提示没有路由到远程机器上去,遇到这种问题,首先在hf-02虚拟机上检查网络连通性

[root@han02 ~]# 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

这时在hf-02虚拟机上,检查是否为端口的问题

telnet 命令,用于登录远程主机,对远程主机进行管理。

安装telnet包——>yum install -y telnet

telnet 192.168.202.130 873 //检查端口是否相通的命令

[root@han02 ~]# telnet 192.168.202.130 873
Trying 192.168.202.130...
telnet: connect to address 192.168.202.130: No route to host

这时候会发现873端口是不通的

首先检查是否为iptables的问题

[root@han02 ~]# 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  

会看到是iptables的问题,我们需要停掉firewalld 服务停掉

[root@han02 ~]# systemctl stop firewalld
[root@han02 ~]# 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 

这时会发现端口通了

若想退出,按ctrl+],然后在quit就退出了

[root@han02 ~]# 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.

这时在han02虚拟机上,执行rsync -avP /tmp/hanfeng.txt 192.168.202.130::test/hanfeng-02.txt命令,会看到提示要输出密码——因为在/etc/rsyncd.conf文件中,有定义密码

这时可以在han01虚拟机中的/etc/rsyncd.conf文件中,注释掉输入密码

#auth users=test
#secrets file=/etc/rsyncd.passwd

这时在han02虚拟机上执行 rsync -avP /tmp/hanfeng.txt 192.168.202.130::test/hanfeng-02.txt

这时就不需要输入密码,就可以传输文件了

[root@han02 ~]# 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

在han01虚拟机中,检查文件

[root@han01 ~]# ls /tmp/rsync
hanfeng-02.txt

从han02机器的文件拉到han01机器

[root@han02 ~]# 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

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,如果你的数据当中有软连接文件,建议你设置成falsemax 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

查看日志文件命令cat /var/log/rsync.log

[root@han01 ~]# 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@han01 ~]# rsync --port=873 192.168.202.130::
test            

若将 list 改为 false ,则模块名不会列出,会被隐藏

[root@han01 ~]# rsync --port=873 192.168.202.130::  

rsync传输时设置密码

auth users=test 设置用户名为test
secrets file=/etc/rsyncd.passwd 并在/etc/rsyncd.passwd中设置密码文件

在/etc/rsyncd.passwd文件中编辑

格式为test:hanfeng,然后保存退出

然后修改权限为600

chmod 600 /etc/rsyncd.passwd

rsync -avP /tmp/test/ [email protected]::test/

这时候就需要输入用户名了 test@

在rsync传输文件的时候,在写shell脚本输入密码不好,比如每天凌晨半夜更新数据库文件啥的,那就会很麻烦

这时候在hf-02客户端也定义一个密码文件

vim /etc/rsync_pass.txt

格式:只写一个密码即可 hanfeng 并保存——>客户端上只需写一个密码就行
并修改权限为600

chmod 600 /etc/rsync_pass.txt

rsync -avP /tmp/test/ –password-file=/etc/rsync_pass.txt [email protected]::test/

这里需要加上--password-file=/etc/rsync_pass.txt

hosts allow

用来定义你允许那些机器去做同步,允许哪一个IP连接,若是多个IP,就用空格隔开,也可以写IP段192.168.133.0/24

三、linux系统日志

/var/log/messages //是linux系统一个总的日志——>除非某些服务,有定义单独的日志
/etc/logrotate.conf 日志切割配置文件
参考日志文件文章
dmesg命令
/var/log/dmesg 日志
last命令,调用的文件/var/log/wtmp
lastb命令查看登录失败的用户,对应的文件时/var/log/btmp
/var/log/secure

系统日志

/var/log/messages //是linux系统一个总的日志——>除非某些服务,有定义单独的日志

系统中存有一个日志切割机制,日志的滚动,在增长到一定级别了,就会自动切割

[root@han01 ~]# 
[root@han01 ~]# ls /var/log/messages
/var/log/messages
[root@han01 ~]# less !$
less /var/log/messages
[root@han01 ~]# du -sh !$
du -sh /var/log/messages
388K    /var/log/messages

在查看日志的时候,会发现日志自动切割了

linux系统中有一个logrotate服务,会自动切割日志,防止无限制的增加

[root@han01 ~]# ls /var/log/messages*
/var/log/messages           /var/log/messages-20171120  /var/log/messages-20171203
/var/log/messages-20171112  /var/log/messages-20171127

查看日志文件cat /etc/logrotate.conf

[root@han01 ~]# cat /etc/logrotate.conf
# see "man logrotate" for details        //会告诉你查看logrotate的帮助文档
# rotate log files weekly
weekly            //每周切割一次

# keep 4 weeks worth of backlogs
rotate 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.

查看 /etc/logrotate.d/syslog 文件

它会为cron,maillog,messages,secure,spooler这几个日志进行切割

messages日志是由 syslogd 服务决定的,所以 kill -HUP 就会重新加载这个日志

还有一个脚本,shell命令行,在把日志切割后(挪走),改名字生成新的日志

Linux系统有一个特点,一个服务写一个文件的时候,并不是按照文件名去写的,而是根据inode来写的

[root@han01 ~]# ls /etc/logrotate.d
ppp  samba  syslog  wpa_supplicant  yum
[root@hf ~]# cat /etc/logrotate.d/syslog
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
    sharedscripts
    postrotate
    /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript
}

dmesg命令

dmesg命令会把系统硬件相关的日志列出来,这个日志是保存在内存中的,并不是一个文件

假如你的网卡有问题了,硬盘损坏了,都会记录在这个日志中

dmesg -c //清空当前日志,但是一重启这个系统,又会生成这些日志

/var/log/dmesg日志文件

/var/log/dmesg 这是一个日志文件,这个日志文件和 dmesg命令 没有任何关联,它是系统启动的一个日志,记录的信息

last命令

last命令,查看你正确的登录历史,调用的文件/var/log/wtmp

记录是是谁,在哪里,来源IP,时间,登录的时长都会有记录

/var/log/wtmp日志是一个二进制文件,不能直接cat查看的,只能用last命令去查看

[root@han01 ~]# last
root     pts/0        192.168.74.1     Thu Dec  7 01:41   still logged in   
reboot   system boot  3.10.0-123.el7.x Thu Dec  7 01:36 - 05:38  (04:01)    
root     pts/1        192.168.74.1     Wed Dec  6 05:11 - 05:13  (00:01)    
root     pts/0        192.168.74.1     Wed Dec  6 01:27 - crash (1+00:09)   
reboot   system boot  3.10.0-123.el7.x Wed Dec  6 01:26 - 05:38 (1+04:11)   
root     pts/0        192.168.74.1     Tue Dec  5 01:38 - crash  (23:48)    
reboot   system boot  3.10.0-123.el7.x Tue Dec  5 01:37 - 05:38 (2+04:00)   
root     pts/0        192.168.74.1     Mon Dec  4 03:23 - down   (02:57)    
root     pts/0        192.168.74.1     Sun Dec  3 17:42 - 03:23  (09:40)    


[root@han01 ~]# ls /var/log/wtmp
/var/log/wtmp

lastb命令

lastb命令,查看登录失败的用户,对应的文件时/var/log/btmp 日志

/var/log/btmp也是二进制文件,不能直接cat的

[root@han01 ~]# lastb

btmp begins Sat Dec  2 04:25:01 2017
[root@han01 ~]# ls /var/log/btmp
/var/log/btmp

安全日志

/var/log/secure

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

[root@han01 ~]# ls /var/log/secure
/var/log/secure


四、screen工具

为了不让一个任务意外中断
nohup command &
screen是一个虚拟终端
yum install -y screen
screen直接回车就进入了虚拟终端
ctral a组合键再按d退出虚拟终端,但不是结束
screen -ls 查看虚拟终端列表
screen -r id 进入指定的终端
screen -S aming
screen -r aming

screen

screen,虚拟的一个屏幕,也可以理解为一个虚拟的终端

需求,执行一个脚本,需要一天一夜,而且脚本会输出一些东西出来,这就意味着这个脚本不能中途断开,保证脚本不中断,有两种方法:

方法一:把这个任务丢到后台去,然后加一个日志的输出

命令nohup command &——>nohup 加执行命令 加日志 再加一个&符号
这时即使你的终端断开,依旧会在后台执行——>但虽然解决了任务中断的问题,但是没有办法实时查看输出的内容

方法二:screen工具,可以把要执行的命令,放到这个终端里,然后在退出pts/0之前,可以把screen丢到后台去,随时用, 随时查看

安装screen包——>yum install -y screen

在安装完成后,直接敲screen命令,回车,会进入到screen,进入到一个窗口,这个窗口就是一个虚拟终端
在虚拟终端,执行vmstat 1命令,然后 ctrl+a键 同时按,随后再按 d键 ,就把screen丢到后台了
执行screen ls 命令,列出当前所有的session
执行screen -r 再加查看到的id 命令,再回到虚拟终端
这时若不需要screen了,直接杀死,按 exit 即可
再次 screen -ls 查看,会发现没有screen
[root@han01 ~]# screen
在虚拟终端下执行了vmstat 1,按 ctrl+a键 同时按,随后再按 d键 ,就把screen丢到后台了

[detached from 2841.pts-0.han01]
[root@han01 ~]# screen -ls        //列出当前所有的session
There is a screen on:
    2841.pts-0.han01    (Detached)
1 Socket in /var/run/screen/S-root.

[root@han01 ~]# screen -r 2841    //重新回到虚拟终端——>若不需要screen了,直接杀死,按 exit 即可
[screen is terminating]
[root@han01 ~]# screen -ls        //再次查看,会发现没有screen
No Sockets found in /var/run/screen/S-root.

screen命令,多个虚拟终端同时运行

screen可以执行多个虚拟终端,同时运行

若想进入到其中一个,指定id即可

[root@han01 ~]# screen
[detached from 2880.pts-0.han01]
[root@han01 ~]# screen
[detached from 2903.pts-0.han01]
[root@han01 ~]# screen
[detached from 2926.pts-0.han01]
[root@han01 ~]# screen -ls
There are screens on:
    2926.pts-0.han01    (Detached)
    2903.pts-0.han01    (Detached)
    2880.pts-0.han01    (Detached)
3 Sockets in /var/run/screen/S-root.

但是时间久了,有可能会忘记某一个screen是运行的是什么(因为名字是相同的),我们可以给screen作业的名称

screen -S “test_screen” //指定screen作业的名称

screen -r 后可以加id号,可以是screen作业的名称

拓展:

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

猜你喜欢

转载自blog.csdn.net/iamfishhh/article/details/80756139