2018-08-30笔记

rsync工具介绍

文件同步工具

类似于cp,又和cp不太一样,rsync可以增量更新,同时也支持远程。

  • 将/etc/passwd文件同步到/tmp/1.txt
    rsync -av /etc/passwd /tmp/1.txt
[root@linux7-128 ~]#  rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
passwd

sent 2545 bytes  received 31 bytes  5152.00 bytes/sec
total size is 2471  speedup is 0.96

a:包含很多选项
v:可视化过程

  • 远程拷贝到另外一台机器
    rsync -av 文件路径 [用户名@]IP地址:文件路径
[root@linux7-128 ~]#  rsync -av /etc/passwd [email protected]:/tmp/2.txt
root@192.168.220.128's password: 
sending incremental file list
passwd

sent 2545 bytes  received 31 bytes  93.67 bytes/sec
total size is 2471  speedup is 0.96

用户名@可以省略,不加的话默认以当前终端用户,不管对方机器有没有此用户

rsync格式

 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

rsync常用选项

rsync常用选项

 -a 包含-rtplgoD
 -r 同步目录时要加上,类似cp时的-r选项
 -v 同步时显示一些信息,让我们知道同步的过程
 -l 保留软连接
 -L 加上该选项后,同步软链接时会把源文件给同步
 -p 保持文件的权限属性
 -o 保持文件的属主
 -g 保持文件的属组
 -D 保持设备文件信息
 -t 保持文件的时间属性
 --delete 删除DESTSRC没有的文件
 --exclude 过滤指定文件,如--exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不同步
 -P 显示同步过程,比如速率,比-v更加详细
 -u 加上该选项后,如果DEST中的文件比SRC新,则不同步
 -z 传输时压缩

示例

  • 拷贝一个目录
[root@linux7-128 ~]# rsync -av test/ /tmp/test_dest/
sending incremental file list
created directory /tmp/test_dest/
./
1.txt
1txt -> 1.txt
xaa
xab
xac
xad
xae
xaf
xag

sent 54105 bytes  received 170 bytes  108550.00 bytes/sec
total size is 53639  speedup is 0.99
[root@linux7-128 ~]# ls test/
1txt  1.txt  xaa  xab  xac  xad  xae  xaf  xag
[root@linux7-128 ~]# ls /tmp/test_dest/
1txt  1.txt  xaa  xab  xac  xad  xae  xaf  xag

拷贝目录时,目录后面要加/

  • 把软链接的源文件也拷贝过去
[root@linux7-128 ~]# rsync -avL test/ /tmp/test_dest/
sending incremental file list
1txt

sent 26971 bytes  received 31 bytes  54004.00 bytes/sec
total size is 80451  speedup is 2.98
[root@linux7-128 ~]# ls /tmp/test_dest/
1txt  1.txt  xaa  xab  xac  xad  xae  xaf  xag

这时候就不再是软链接

[root@linux7-128 ~]# ll test/
总用量 68
lrwxrwxrwx 1 root root     5 6月  14 20:58 1txt -> 1.txt
-rw-r--r-- 1 root root 26817 5月  23 23:25 1.txt
-rw-r--r-- 1 root root  3867 5月  31 23:18 xaa
-rw-r--r-- 1 root root  4193 5月  31 23:18 xab
-rw-r--r-- 1 root root  4139 5月  31 23:18 xac
-rw-r--r-- 1 root root  3864 5月  31 23:18 xad
-rw-r--r-- 1 root root  4287 5月  31 23:18 xae
-rw-r--r-- 1 root root  3901 5月  31 23:18 xaf
-rw-r--r-- 1 root root  2566 5月  31 23:18 xag
[root@linux7-128 ~]# ll /tmp/test_dest/
总用量 96
-rw-r--r-- 1 root root 26817 5月  23 23:25 1txt
-rw-r--r-- 1 root root 26817 5月  23 23:25 1.txt
-rw-r--r-- 1 root root  3867 5月  31 23:18 xaa
-rw-r--r-- 1 root root  4193 5月  31 23:18 xab
-rw-r--r-- 1 root root  4139 5月  31 23:18 xac
-rw-r--r-- 1 root root  3864 5月  31 23:18 xad
-rw-r--r-- 1 root root  4287 5月  31 23:18 xae
-rw-r--r-- 1 root root  3901 5月  31 23:18 xaf
-rw-r--r-- 1 root root  2566 5月  31 23:18 xag
  • 删除目标目录中多余的文件
[root@linux7-128 ~]# ls test/
1txt  1.txt  xaa  xab  xac  xad  xae  xaf  xag
[root@linux7-128 ~]# ls /tmp/test_dest/
1txt  1.txt  new.txt  xaa  xab  xac  xad  xae  xaf  xag
[root@linux7-128 ~]# rsync -av --delete test/ /tmp/test_dest/
sending incremental file list
./
deleting new.txt
1txt -> 1.txt

sent 127 bytes  received 18 bytes  290.00 bytes/sec
total size is 53639  speedup is 369.92
[root@linux7-128 ~]# ls /tmp/test_dest/
1txt  1.txt  xaa  xab  xac  xad  xae  xaf  xag
  • 过滤指定文件
[root@linux7-128 ~]# ls test/
1txt  1.txt  xaa  xab  xac  xad  xae  xaf  xag

[root@linux7-128 ~]# rsync -av --exclude "*txt" test/ /tmp/test_dest/
sending incremental file list
created directory /tmp/test_dest
./
xaa
xab
xac
xad
xae
xaf
xag

sent 27205 bytes  received 148 bytes  54706.00 bytes/sec
total size is 26817  speedup is 0.98
[root@linux7-128 ~]# ls /tmp/test_dest/
xaa  xab  xac  xad  xae  xaf  xag

多个过滤条件要加多个–exclude,他不支持连写

  • 显示同步过程,比v更详细,比如显示速率
[root@linux7-128 ~]# rm -rf /tmp/test_dest/
[root@linux7-128 ~]# rsync -avP test/ /tmp/test_dest/
sending incremental file list
created directory /tmp/test_dest
./
1.txt
       26817 100%    0.00kB/s    0:00:00 (xfer#1, to-check=8/10)
1txt -> 1.txt
xaa
        3867 100%    3.69MB/s    0:00:00 (xfer#2, to-check=6/10)
xab
        4193 100%    4.00MB/s    0:00:00 (xfer#3, to-check=5/10)
xac
        4139 100%    3.95MB/s    0:00:00 (xfer#4, to-check=4/10)
xad
        3864 100%    3.68MB/s    0:00:00 (xfer#5, to-check=3/10)
xae
        4287 100%    4.09MB/s    0:00:00 (xfer#6, to-check=2/10)
xaf
        3901 100%    3.72MB/s    0:00:00 (xfer#7, to-check=1/10)
xag
        2566 100%    2.45MB/s    0:00:00 (xfer#8, to-check=0/10)

sent 54105 bytes  received 170 bytes  108550.00 bytes/sec
total size is 53639  speedup is 0.99
  • 当目标目录文件较新时,不同步

同步之后,现在源目录和目标目录文件相同


[root@linux7-128 ~]# touch /root/test/123
[root@linux7-128 ~]# rsync -avP test/ /tmp/test_dest/
sending incremental file list
./
123
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=8/11)

sent 179 bytes  received 34 bytes  426.00 bytes/sec
total size is 53639  speedup is 251.83
[root@linux7-128 ~]# ls test/
123  1txt  1.txt  xaa  xab  xac  xad  xae  xaf  xag
[root@linux7-128 ~]# ls /tmp/test_dest/
123  1txt  1.txt  xaa  xab  xac  xad  xae  xaf  xag
[root@linux7-128 ~]# cat test/123 
[root@linux7-128 ~]# cat /tmp/test_dest/123
[root@linux7-128 ~]# 

现在编辑一下目标目录的123文件

[root@linux7-128 ~]# vim /tmp/test_dest/123

wewrdfrefdfrf
sdfsdfsdfsd
~                                                                                                                        
~                                                                                                                        

:wq   
[root@linux7-128 ~]# cat /tmp/test_dest/123 
wewrdfrefdfrf
sdfsdfsdfsd

再次同步,加上-u选项

[root@linux7-128 ~]# rsync -avPu test/ /tmp/test_dest/
sending incremental file list
./

sent 140 bytes  received 15 bytes  310.00 bytes/sec
total size is 53639  speedup is 346.06
[root@linux7-128 ~]# cat test/123 
[root@linux7-128 ~]# cat /tmp/test_dest/123 
wewrdfrefdfrf
sdfsdfsdfsd

这时候,由于目标目录的123较新,所以它没有被更新

rsync通过ssh同步

rsync通过ssh方式同步
rsync -av 文件路径 [用户名@]IP地址:文件路径

  • 本机同步到其他机器
终端1
[root@linux7-128 ~]# rsync -av /etc/passwd 192.168.220.129:/tmp/pw.txt
sending incremental file list
passwd

sent 2545 bytes  received 31 bytes  5152.00 bytes/sec
total size is 2471  speedup is 0.96
终端2
[root@linux7-129 ~]# ls /tmp/
htr     systemd-private-3dc7ac55aead45208dca9ba91326f5b5-chronyd.service-8xX0hB  systemd-private-3dc7ac55aead45208dca9ba91326f5b5-vgauthd.service-rvVWsr
pw.txt  systemd-private-3dc7ac55aead45208dca9ba91326f5b5-cups.service-xviBSU     systemd-private-3dc7ac55aead45208dca9ba91326f5b5-vmtoolsd.service-JglrVt
  • 其他机器同步到本机
[root@linux7-128 ~]# rsync -av 192.168.220.129:/tmp/hu.txt /tmp/hu.txt
receiving incremental file list
hu.txt

sent 30 bytes  received 2550 bytes  5160.00 bytes/sec
total size is 2471  speedup is 0.96
[root@linux7-128 ~]# ls /tmp/
1.cap   systemd-private-72c2ca7e6439444a9d467593efb50d39-chronyd.service-WtaCAF
1.txt   systemd-private-72c2ca7e6439444a9d467593efb50d39-cups.service-CS7taq
2.txt   systemd-private-72c2ca7e6439444a9d467593efb50d39-vgauthd.service-BmZHot
hu.txt  systemd-private-72c2ca7e6439444a9d467593efb50d39-vmtoolsd.service-A7U6mh
my.txt  test_dest
  • 如果对方机器端口不是22,可以指定端口
    rsync -av -e "ssh -p 22" test1/ 192.168.133.132:/tmp/test2/
[root@linux7-128 ~]# rsync -av -e "ssh -p 22" /etc/passwd 192.168.220.129:/tmp/pw.txt
sending incremental file list

sent 31 bytes  received 12 bytes  86.00 bytes/sec
total size is 2471  speedup is 57.47

知识点:
ssh -p 22 目标主机IP
登录到目标主机

[root@linux7-128 ~]# ssh -p 22 192.168.220.129
Last login: Thu Jun 14 21:39:30 2018 from 192.168.220.1
[root@linux7-129 ~]# exit
登出
Connection to 192.168.220.129 closed.
[root@linux7-128 ~]# 

rsync通过服务同步

rsync 通过服务的方式同步
1.要编辑配置文件/etc/rsyncd.conf

rsyncd.conf样例

port=873
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.133.130
[test]
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.133.132 1.1.1.1 2.2.2.2  192.168.133.0/24

/tmp/rsync要先创建一个

[root@linux7-128 ~]# mkdir /tmp/rsync
[root@linux7-128 ~]# chmod 777 /tmp/rsync

2.启动服务rsync –daemon

[root@linux7-128 ~]# rsync --daemon
[root@linux7-128 ~]# ps -aux |grep rsync
root      3207  0.0  0.0 114696   548 ?        Ss   22:03   0:00 rsync --daemon
root      3225  0.0  0.0 112720   980 pts/0    S+   22:03   0:00 grep --color=auto rsync
[root@linux7-128 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
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      1195/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      997/sshd            
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      999/cupsd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1105/master         
tcp        0      0 192.168.220.128:873     0.0.0.0:*               LISTEN      3207/rsync          
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      997/sshd            
tcp6       0      0 ::1:631                 :::*                    LISTEN      999/cupsd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1105/master   

3.其他主机上使用rsync服务来同步文件

[root@linux7-129 ~]# rsync -avP /etc/passwd 192.168.220.128::test/pw
rsync: failed to connect to 192.168.220.128 (192.168.220.128): No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(122) [sender=3.0.9]

这里的test就是我们在配置文件里所定义的,代表/tmp/rsync这个路径
然后我们发现传输不通,首先看能否ping通

[root@linux7-129 ~]# ping 192.168.220.128
PING 192.168.220.128 (192.168.220.128) 56(84) bytes of data.
64 bytes from 192.168.220.128: icmp_seq=1 ttl=64 time=0.666 ms
64 bytes from 192.168.220.128: icmp_seq=2 ttl=64 time=0.535 ms
64 bytes from 192.168.220.128: icmp_seq=3 ttl=64 time=0.723 ms
^C
--- 192.168.220.128 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 0.535/0.641/0.723/0.081 ms

可以ping通,然后看Telnet能否ping通端口(没有Telnet的话安装一下)

[root@linux7-129 ~]# telnet 192.168.220.128 873
Trying 192.168.220.128...
telnet: connect to address 192.168.220.128: No route to host

目标主机的873端口不通,使用iptables查看规则

有规则,关闭firewalld

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

同样的关闭目标主机的firewalld后,就可以ping通端口了

[root@linux7-129 ~]# telnet 192.168.220.128 873
Trying 192.168.220.128...
Connected to 192.168.220.128.
Escape character is '^]'.
@RSYNCD: 30.0

退出 :Ctrl+],然后输入quit

这时候就可以使用同步命令进行同步,注意这里可以将配置文件这两行注释掉,不然会让定义密码

#auth users=test
#secrets file=/etc/rsyncd.passwd
[root@linux7-129 ~]# rsync -avP /etc/passwd 192.168.220.128::test/pw
sending incremental file list
passwd
        2127 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)

sent 2197 bytes  received 27 bytes  4448.00 bytes/sec
total size is 2127  speedup is 0.96

同步完成

4.去目标主机查看

[root@linux7-128 ~]# ls /tmp/rsync/
pw

5.反向同步可以

[root@linux7-129 ~]# rsync -avP  192.168.220.128::test/pw /tmp/pw_new
receiving incremental file list
pw
        2127 100%    2.03MB/s    0:00:00 (xfer#1, to-check=0/1)

sent 45 bytes  received 2224 bytes  4538.00 bytes/sec
total size is 2127  speedup is 0.94
[root@linux7-129 ~]# ls /tmp/
hu.txt  systemd-private-3dc7ac55aead45208dca9ba91326f5b5-chronyd.service-8xX0hB  systemd-private-3dc7ac55aead45208dca9ba91326f5b5-vmtoolsd.service-JglrVt
pw_new  systemd-private-3dc7ac55aead45208dca9ba91326f5b5-cups.service-xviBSU
pw.txt  systemd-private-3dc7ac55aead45208dca9ba91326f5b5-vgauthd.service-rvVWsr
  • 配置文件详解

 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@linux7-128 ~]# cat /var/log/rsync.log
2018/06/14 22:03:29 [3207] rsyncd version 3.0.9 starting, listening on port 873
2018/06/14 22:36:15 [3669] name lookup failed for 192.168.220.129: Name or service not known
2018/06/14 22:36:15 [3669] connect from UNKNOWN (192.168.220.129)
2018/06/14 22:38:25 [3688] name lookup failed for 192.168.220.129: Name or service not known
......
2018/06/14 22:53:40 [3959] connect from UNKNOWN (192.168.220.129)
2018/06/14 14:53:40 [3959] rsync on test/pw from unknown (192.168.220.129)
2018/06/14 14:53:40 [3959] building file list
2018/06/14 14:53:40 [3959] sent 2239 bytes  received 46 bytes  total size 2127
2018/06/15 00:28:16 [3857] sent 0 bytes  received 0 bytes  total size 0
  • 查看模块名

rsync --port=873 192.168.220.129::

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

rsync传输时设置密码

  • 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/ test@192.168.202.130::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 test@192.168.202.130::test/
这里需要加上--password-file=/etc/rsync_pass.txt
  • hosts allow

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

linux系统日志

 /var/log/messages
 /etc/logrotate.conf 日志切割配置文件
 参考https://my.oschina.net/u/2000675/blog/908189
 dmesg命令
 /var/log/dmesg 日志
 last命令,调用的文件/var/log/wtmp
 lastb命令查看登录失败的用户,对应的文件时/var/log/btmp
 /var/log/secure
  • /var/log/messages
    linux系统一个总的日志——>除非某些服务,有定义单独的日志
[root@linux7-128 ~]# ls /var/log/messages
/var/log/messages
[root@linux7-128 ~]# ls /var/log/messages*
/var/log/messages           /var/log/messages-20180527  /var/log/messages-20180612
/var/log/messages-20180520  /var/log/messages-20180604

系统中存有一个日志切割机制,日志的滚动,在增长到一定级别了,就会自动切割.在查看日志的时候,会发现日志自动切割了。

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

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

[root@linux7-128 ~]# cat /etc/logrotate.conf
# see "man logrotate" for details
# 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

# 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@linux7-128 ~]#  ls /etc/logrotate.d
bootlog  cups       iscsiuiolog  libvirtd.qemu  numad  psacct  sssd    wpa_supplicant
chrony   glusterfs  libvirtd     nginx          ppp    samba   syslog  yum
[root@linux7-128 ~]#  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
}

dmesg命令

dmesg命令,会把系统硬件相关的日志列出来,这个日志是保存在内存中的,并不是一个文件。
假如你的网卡有问题了,硬盘损坏了,都会记录在这个日志中

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

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

last命令

查看你正确的登录历史,调用的文件/var/log/wtmp
记录是是谁,在哪里,来源IP,时间,登录的时长都会有记录
/var/log/wtmp日志是一个二进制文件,不能直接cat查看的,只能用last命令去查看

[root@linux7-128 ~]# last
root     pts/0        192.168.220.1    Thu Jun 21 09:50   still logged in   
reboot   system boot  3.10.0-693.el7.x Thu Jun 21 09:45 - 10:29  (00:44)    
reboot   system boot  3.10.0-693.el7.x Fri Jun 15 19:56 - 10:29 (5+14:32)   
root     pts/0        192.168.220.1    Thu Jun 14 20:25 - 00:28  (04:02)    
reboot   system boot  3.10.0-693.el7.x Thu Jun 14 20:11 - 10:29 (6+14:18)   
root     pts/0        192.168.220.1    Wed Jun 13 20:15 - 21:57  (01:41)    
reboot   system boot  3.10.0-693.el7.x Wed Jun 13 18:41 - 21:57  (03:16)    
root     pts/0        192.168.220.1    Wed Jun 13 13:08 - 13:48  (00:40)    
reboot   system boot  3.10.0-693.el7.x Wed Jun 13 13:07 - 13:48  (00:41)    
root     pts/1        192.168.220.1    Tue Jun 12 21:00 - 23:08  (02:07)    
............
[root@linux7-128 ~]# last /var/log/wtmp

wtmp begins Sat Apr 28 04:39:25 2018

lastb命令

查看登录失败的用户
对应的文件时/var/log/btmp 日志
/var/log/btmp也是二进制文件,不能直接cat的

[root@linux7-128 ~]# lastb

btmp begins Fri Jun  1 20:07:01 2018

安全日志

  • /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
  • screen命令,多个虚拟终端同时运行
    screen可以执行多个虚拟终端,同时运行
    若想进入到其中一个,指定id即可
[root@linux7-128 ~]# screen -ls
There is a screen on:
    1978.pts-0.linux7-128   (Detached)
1 Socket 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/u013946328/article/details/80757027