2018-06-15(平常运维四)

10.28 rsync工具介绍

rsync命令是一个远程数据同步工具,可经过LAN/WAN快速同步多台主机间的文件。rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不一样部分,而不是每次都整份传送,所以速度至关快.
命令格式和用法html

rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [USER@]host:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST
rsync [OPTION]... [USER@]HOST::SRC DEST
rsync [OPTION]... SRC [USER@]HOST::DEST
rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
对应于以上六种命令格式,rsync有六种不一样的工做模式:
一、拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工做模式。如:rsync -a /data /backup
二、使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包含单个冒号":"分隔符时启动该模式。如:rsync -avz *.c foo:src
三、使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径包含单个冒号":"分隔符时启动该模式。如:rsync -avz foo:src/bar /data
四、从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含"::"分隔符时启动该模式。如:rsync -av root@192.168.66.130::www /databack
五、从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含"::"分隔符时启动该模式。如:rsync -av /databack root@192.168.66.130::www
六、列远程机的文件列表。这相似于rsync传输,不过只要在命令中省略掉本地机信息便可。如:rsync -v rsync://192.168.66.130/wwwlinux

10.29/10.30 rsync经常使用选项

经常使用选项算法

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

经常使用选项的操做:vim

一、将root下的src目录同步到tmp下,并更改命称为dest,保持文件属性一致,在源目录和目标目录的后边要加上/,其中src下ln.txt为软链接文件安全

[root@luo src]# ll /root/src/
总用量 0
-rw-r--r-- 1 root root  0 6月  18 11:15 1.txt
-rw-r--r-- 1 root root  0 6月  18 11:15 2.txt
-rw-r--r-- 1 root root  0 6月  18 11:15 aa.txt
-rw-r--r-- 1 root root  0 6月  18 11:15 bb.txt
lrwxrwxrwx 1 root root 11 6月  18 11:17 ln.txt -> /tmp/cc.txt
[root@luo src]# ll /tmp/
总用量 0
-rw-r--r-- 1 root root  0 6月  18 11:16 cc.txt
drwx------ 3 root root 17 6月  15 20:34 systemd-private-bd3913ea59c345b0b0a13da70a90c32a-chronyd.service-9Gwi3G
drwx------ 3 root root 17 6月  15 20:34 systemd-private-bd3913ea59c345b0b0a13da70a90c32a-vgauthd.service-YNBlPW
drwx------ 3 root root 17 6月  15 20:34 systemd-private-bd3913ea59c345b0b0a13da70a90c32a-vmtoolsd.service-RlrcGd
[root@luo src]# rsync -avP /root/src/ /tmp/dest/                   #加上-P选项后能够看到详细的传输速率和进度
sending incremental file list
created directory /tmp/dest
./
1.txt
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=4/6)
2.txt
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=3/6)
aa.txt
              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=2/6)
bb.txt
              0 100%    0.00kB/s    0:00:00 (xfr#4, to-chk=1/6)
ln.txt -> /tmp/cc.txt
sent 317 bytes  received 130 bytes  894.00 bytes/sec
total size is 11  speedup is 0.02
[root@luo src]# ll /tmp/dest/
总用量 0
-rw-r--r-- 1 root root  0 6月  18 11:15 1.txt
-rw-r--r-- 1 root root  0 6月  18 11:15 2.txt
-rw-r--r-- 1 root root  0 6月  18 11:15 aa.txt
-rw-r--r-- 1 root root  0 6月  18 11:15 bb.txt
lrwxrwxrwx 1 root root 11 6月  18 11:17 ln.txt -> /tmp/cc.txt

能够看到自动建立了目标目录,而且软连接原来指向哪同步后也是指向哪bash

二、加上-L选项以后,l选项会失效,会把软连接的源文件同步服务器

[root@luo src]# rsync -avPL /root/src/ /tmp/dest/
sending incremental file list
ln.txt
              4 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/6)
sent 185 bytes  received 35 bytes  440.00 bytes/sec
total size is 4  speedup is 0.02
[root@luo src]# ll /tmp/dest/                            #能够看到软连接文件已经消失,虽然仍是ln.txt但实际为源文件/tmp/cc.txt的内容
总用量 4
-rw-r--r-- 1 root root 0 6月  18 11:15 1.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 2.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 aa.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 bb.txt
-rw-r--r-- 1 root root 4 6月  18 11:30 ln.txt
[root@luo src]# cat /tmp/cc.txt 
111
[root@luo src]# cat /tmp/dest/ln.txt 
111

三、--delete 删除DEST中SRC没有的文件,若是要让目标目录和源目录保持一致,可使用这个选项架构

[root@luo src]# ll /root/src/
总用量 0
-rw-r--r-- 1 root root 0 6月  18 11:15 1.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 2.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 aa.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 bb.txt
[root@luo src]# ll /tmp/dest/                          #能够看到这里多个3.txt
总用量 4
-rw-r--r-- 1 root root 0 6月  18 11:15 1.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 2.txt
-rw-r--r-- 1 root root 0 6月  18 11:38 3.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 aa.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 bb.txt
[root@luo src]# rsync -avP --delete /root/src/ /tmp/dest/     #加上--delete选项后输出删除了3.txt
sending incremental file list
deleting 3.txt
./
sent 121 bytes  received 28 bytes  298.00 bytes/sec
total size is 0  speedup is 0.00
[root@luo src]# ll /tmp/dest/                             #能够看到和源目录/root/src/保持了一致
总用量 0
-rw-r--r-- 1 root root 0 6月  18 11:15 1.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 2.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 aa.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 bb.txt
[root@luo src]# ll /root/src/
总用量 0
-rw-r--r-- 1 root root 0 6月  18 11:15 1.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 2.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 aa.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 bb.txt

四、在拷贝时添加--exclude,能够过滤指定的文件,支持多个--exclude命令共同使用dom

[root@luo src]# rm -rf /tmp/dest/*                       #先清空dest下的文件
[root@luo src]# ll /tmp/dest/
总用量 0
[root@luo src]# ll /root/src/
总用量 0
-rw-r--r-- 1 root root 0 6月  18 11:15 1.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 2.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 aa.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 bb.txt
[root@luo src]# rsync -avP --exclude "2.txt" --exclude "a*" /root/src/ /tmp/dest/           #这里过滤了2.txt和a开头的
sending incremental file list
./
1.txt
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=1/3)
bb.txt
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=0/3)
sent 166 bytes  received 57 bytes  446.00 bytes/sec
total size is 0  speedup is 0.00
[root@luo src]# ll /tmp/dest/                       #能够看到只同步了1.txt和bb.txt,2.txt和a开头的已通过滤了
总用量 0
-rw-r--r-- 1 root root 0 6月  18 11:15 1.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 bb.txt

五、-u 加上该选项后,若是DEST中的文件比SRC新,则不一样步

[root@luo src]# cat /root/src/1.txt                   源文件1.txt为空
[root@luo src]# echo "1111" >/tmp/dest/1.txt      更新下目标文件1.txt内容
[root@luo src]# cat /tmp/dest/1.txt 
1111
不加-u选项同步,会把目标下的1.txt文件覆盖
[root@luo src]# rsync -avP /root/src/ /tmp/dest/
sending incremental file list
1.txt
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=3/5)
2.txt
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=2/5)
aa.txt
              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=1/5)
sent 235 bytes  received 73 bytes  616.00 bytes/sec
total size is 0  speedup is 0.00
[root@luo src]# cat /tmp/dest/1.txt        #能够看到这里也为空
加上-u选项再测试
[root@luo src]# !ec
echo "1111" >/tmp/dest/1.txt                              #更新下目标的内容
[root@luo src]# rsync -avPu /root/src/ /tmp/dest/                #加上-u选项后,目标比源新,不一样步
sending incremental file list

sent 114 bytes  received 12 bytes  252.00 bytes/sec
total size is 0  speedup is 0.00
[root@luo src]# cat /tmp/dest/1.txt                                 #能够看到没有同步
1111

10.31 rsync经过ssh同步

rsync不启动服务能够经过ssh同步,保证两台机器A和B能够通讯,而且都要安装rsync,这里以192.168.66.130和192.168.66.131为例:

[root@luo src]# hostname                主机名luo的ip为66.130
luo
[root@localhost ~]# hostname              主机名为localhost的66.131
localhost.localdomain

需求:把130机器/root/src/目录同步到远程131机器/root/下并更名为src1

[root@luo src]# ll /root/src/
总用量 0
-rw-r--r-- 1 root root 0 6月  18 11:15 1.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 2.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 aa.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 bb.txt
[root@luo src]# rsync -avPz /root/src/ root@192.168.66.131:/root/src1/    #加上-z选项会使用压缩,同步过去后再解压
root@192.168.66.131's password: 
sending incremental file list
created directory /root/src1
./
1.txt
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=3/5)
2.txt
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=2/5)
aa.txt
              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=1/5)
bb.txt
              0 100%    0.00kB/s    0:00:00 (xfr#4, to-chk=0/5)
sent 265 bytes  received 128 bytes  60.46 bytes/sec
total size is 0  speedup is 0.00
切换到131机器
[root@localhost ~]# ll /root/src1/                        #能够看到已经同步完成
总用量 0
-rw-r--r-- 1 root root 0 6月  18 11:15 1.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 2.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 aa.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 bb.txt

咱们也能够去远程主机拉文件,把131机器/root/src1/目录拉到130机器的/tmp/src1/

[root@luo src]# rsync -avP 192.168.66.131:/root/src1/ /tmp/src1/        #不指定远程主机的用户,默认使用当前主机的用户
root@192.168.66.131's password: 
receiving incremental file list
created directory /tmp/src1
./
1.txt
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=3/5)
2.txt
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=2/5)
aa.txt
              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=1/5)
bb.txt
              0 100%    0.00kB/s    0:00:00 (xfr#4, to-chk=0/5)
sent 103 bytes  received 277 bytes  108.57 bytes/sec
total size is 0  speedup is 0.00

默认ssh为22端口,若是远程主机的ssh修改成其它的,例如:1122,能够经过-e指定ssh端口号

rsync -av -e "ssh -p 1122" test1/ 192.168.66.131:/tmp/test1/

10.32/10.33 rsync经过服务同步

rsync还能够经过c/s架构方式同步,即客户端和服务端链接,在使用该方法进行同步以前须要先在服务的开启相应服务同时要监听一个端口(可自定义),默认是监听873端口,开启服务以前编辑rsync配置文件“/etc/rsyncd.conf”(将服务添加进去)而后使用命令:
“rsync –daemon(服务名称)”启动服务。
或者将配置文件写到一个自定义文件中,使用命令“rsync –configfile”指定配置文件的路径启动服务。 配置完成后客户端能够经过指定端口与服务端进行通讯。

rsync --configfile=/usr/local/rsyncd.conf --daemon

服务端 192.168.66.130(主机名luo)
客户端 192.168.66.131 (主机名localhost)
配置文件具体步骤:

1.在/etc/rsyncd.conf配置文件中添加:

[root@luo ~]# vi /etc/rsyncd.conf
port=873                               
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.66.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.66.131

说明:
配置文件分为两部分:全局配置部分和模块配置部分,全局部分就是几个参数而已,就像rsyncd.conf中port, log
file, pid file,
address这些都属于全局配置,而[test]如下部分就是模块配置部分了。一个配置文件中能够有多个模块,模块名自定义,格式就像rsyncd.conf中的这样。其实模块中的一些参数例如use
chroot, max connections, udi, gid, auth users, secrets file以及hosts
allow均可以配置成全局的参数。
参数意义:

port 指定启动端口,默认873;
log file 指定日志文件;
pid file 指定pid文件,这个文件的做用涉及到服务的启动以及中止等进程管理操做;
address 指定启动rsyncd服务的IP,假如你的机器有多个IP,就能够指定其中一个启动rsyncd服务,默认是在所有IP上启动;
[test] 指定模块名,自定义;
path 指定数据存放的路径;
use chroot true|false
默认是true,意思是在传输文件之前首先chroot到path参数所指定的目录下。这样作的缘由是实现额外的安全防御,可是缺点是须要以roots权限,而且不能备份指向外部的符号链接所指向的目录文件。默认状况下chroot值为true,若是你的数据当中有软链接文件的话建议设置成false;
max connections 指定最大的链接数,默认是0即没有限制;
read only ture|false 若是为true则不能上传到该模块指定的路径下;
list 指定当用户查询该服务器上的可用模块时,该模块是否被列出,设定为true则列出,false则隐藏;
uid/gid 指定传输文件时,以哪一个用户/组的身份传输;
auth users 指定传输时要使用的用户名;
secrets file 指定密码文件,格式为用户名:密码该参数连同上面的参数若是不指定则不使用密码验证,注意该密码文件的权限必定要是600;
hosts allow 指定被容许链接该模块的主机,能够是IP或者网段,若是是多个,之间用空格隔开;

2.启动服务

[root@luo ~]# rsync --daemon

说明:若是想开机启动,请把 rsync --daemon --configfile=/etc/rsyncd.conf 写入到/etc/rc.d/rc.local文件
说明:若是启动完此服务进程以后,想杀死此进程须要使用killall rsync(语法为 killall 进程名)
3.检测是否启动服务

[root@luo src]# rsync --daemon
[root@luo src]# ps aux |grep 'rsync'
root       2461  0.0  0.0 114740   552 ?        Ss   12:41   0:00 rsync --daemon
root       2470  0.0  0.0 112720   972 pts/1    R+   12:41   0:00 grep --color=auto rsync

4.检查监听端口

[root@luo src]# netstat -lntp |grep rsync
tcp        0      0 192.168.66.130:873      0.0.0.0:*               LISTEN      2461/rsync
  1. 建立数据存放的路径并赋予权限(也就是配置文件中的path,若是有这个文件就无需建立了)
    [root@luo src]# mkdir /tmp/rsync
    [root@luo src]# chmod 777 /tmp/rsync                   #为了方便测试把目录权限设为777
    [root@luo src]# ll -d /tmp/rsync/
    drwxrwxrwx 2 root root 6 6月  18 12:43 /tmp/rsync/

    6.为了避免影响实验过程,还须要把两台机器的firewalld服务关闭,并设置成不开机启动。

[root@luo src]# systemctl stop firewalld //两台机器都须要设置
7.在客户机131上查看服务主机130的873端口是不是通的

[root@localhost ~]# telnet 192.168.66.130 873
Trying 192.168.66.130...
Connected to 192.168.66.130.
Escape character is '^]'.
@RSYNCD: 31.0
^] //按Ctrl+] 退出telnet

7.由于在服务机131的配置文件上指定了传输的用户和密码文件,修改其内容,并必定要把权限设置为600

[root@luo src]# cat /etc/rsyncd.passwd 
test:test123                                                          #这个格式为用户名:密码
[root@luo src]# cat /etc/rsyncd.passwd 
test:test123
[root@luo src]# chmod 600 !$
chmod 600 /etc/rsyncd.passwd
[root@luo src]# ll !$
ll /etc/rsyncd.passwd
-rw------- 1 root root 13 6月  18 14:06 /etc/rsyncd.passwd

8.准备完成后在localhost131客户端上测试

[root@localhost ~]# rsync -avP /tmp/1.txt test@192.168.66.130::test/2.txt
Password:                                                                          #这里输入服务器/etc/rsyncd.passwd上定义的密码
sending incremental file list
1.txt
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/1)
sent 87 bytes  received 35 bytes  34.86 bytes/sec
total size is 0  speedup is 0.00
切换到服务器上查看
[root@luo src]# ll /tmp/rsync/2.txt
-rw-r--r-- 1 root root 0 6月  18 14:11 /tmp/rsync/2.txt                       #能够看到已经传输过来了

注意:
刚刚提到配置文件/etc/rsyncd.conf中有一个选项叫作 “use chroot” 默认为true,若是是true,同步的文件中若是有软链接,则会有问题。若是有软连接须要设置成false便可。
修改完rsyncd.conf配置文件后,不须要重启rsyncd服务,这是rsync的一个特定机制,配置文件时即时生效的,不用重启服务,改动端口号要想生效要重启服务。
前面都有输入密码,这样一样也不能写入脚本中自动执行,其实这种方式也是能够不用手动输入密码的,它有两种实现方式。

第一种,指定密码文件

在客户端上,也就是localhost上,编辑一个密码文件:

[root@localhost ~]# vi /usr/rsync.pass
[root@localhost ~]# cat /usr/rsync.pass 
test123
修改密码文件的权限:
[root@localhost ~]# chmod 600 !$
chmod 600 /usr/rsync.pass

在同步的时候,指定一下密码文件,就能够省去输入密码的步骤了:

[root@localhost ~]# rsync -avP /tmp/1.txt test@192.168.66.130::test/3.txt --password-file=/usr/rsync.pass 
sending incremental file list
1.txt
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/1)
sent 87 bytes  received 35 bytes  244.00 bytes/sec
total size is 0  speedup is 0.00

第二种:在rsync服务器端不指定用户

在服务端130修改配置文件rsyncd.conf, 去掉关于认证帐户的配置项(auth user 和 secrets file这两行)

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

而后咱们再到客户端测试:

[root@localhost ~]# rsync -avP /tmp/1.txt 192.168.66.130::test/4.txt
sending incremental file list
1.txt
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/1)
sent 87 bytes  received 35 bytes  244.00 bytes/sec
total size is 0  speedup is 0.00

注意,这里不用再加test这个用户了,默认是以当前用户的身份拷贝的,如今已经不须要输入密码了。
九、更改端口873为8730

1.编辑rsync配置文件(/etc/rsyncd.conf),将端口改成port=8730
2.重启rsync服务
3.安装killall工具:yum install -y psmisc
4.杀死全部rsync进程:

[root@luo ~]# killall rsync

5.启动:

[root@luo ~]# rsync --daemon

6.检测进程和端口:

[root@luo src]# ps aux |grep 'rsync'
root       3094  0.0  0.0 114740   556 ?        Ss   14:40   0:00 rsync --daemon
root       3103  0.0  0.0 112720   972 pts/1    R+   14:40   0:00 grep --color=auto rsync
[root@luo src]# netstat -lnp |grep rsync
tcp        0      0 192.168.66.130:8730     0.0.0.0:*               LISTEN      3094/rsync

能够看到已经绑定了8730端口
在客户机localhost上进行测试

root@localhost ~]# rsync -avP /tmp/1.txt 192.168.66.130::test/5.txt
rsync: failed to connect to 192.168.66.130 (192.168.66.130): Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(125) [sender=3.1.2]

说明: 由于绑定的是8730的端口,因此此时同步数据会报错!能够经过--port参数指定对应的端口:

[root@localhost ~]# rsync -avP --port 8730 /tmp/1.txt 192.168.66.130::test/5.txt
sending incremental file list
1.txt
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/1)
sent 87 bytes  received 35 bytes  244.00 bytes/sec
total size is 0  speedup is 0.00

说明: 使--port选项指定端口号。这个要和经过ssh同步数据用-e指定端口区分开来

10.34 linux系统日志

日志主要的功能有:审计和监测,还能够实时的监测系统状态,监测和追踪侵入者等等。
一、/var/log/message, 它是核心系统日志文件,包含了系统启动时的引导消息,以及系统运行时的其余状态消息

[root@localhost ~]# ls /var/log/messages
messages           messages-20180509  messages-20180513  messages-20180615  messages-20180618

能够看到这些日志后面都跟了一些日期,这就证实它是按日期切割的,那他是这样作到的呢

logrotate,他就是用来切割日志的,为了防止日志无限制的增长。它的配置文件/etc/logrotate.conf

二、dmesg命令
将系统里面的硬件先关的日志列出来,它保存在内存中,它并非一个文件。好比咱们的硬盘或者网卡出现问题,他都会记录在这里。咱们除了看/var/log/messages外,还要运行这个命令查看硬件的故障和错误。参数-c能够清空内容,但重启后又会自动添加内容。

三、/var/log/dmesg
这是一个日志文件,他是记录系统启动的日志,和dmesg命令没有关系。

四、last命令
调用的文件/var/log/wtmp,用来查看正确的登录历史,是一个二进制文件,不能cat查看,只能用last来查看。

五、lastb命令
查看登陆失败的用户,对应的文件时/var/log/btmp /var/log/secure,记录登陆失败的日志。是一个二进制文件,不能cat查看。

六、/var/log/secure安全日志
会记录登陆相关以及pam相关等等的日志。若是有人暴力破解咱们设备,那么也会记录到这个文件中

10.35 screen工具

在工做中,咱们也许会有这样的需求,要执行一个命令或者脚本,可是须要几个小时甚至几天。这就要考虑一个问题,就是中途断网或出现其余意外状况,执行的任务中断了怎么办?你能够把命令或者脚本丢到后台运行,不过也不保险。下面就介绍两种方法来避免这样的问题发生。
一、使用nohup

[root@luo ~]# vim /usr/local/sbin/sleep.sh
[root@luo ~]# cat !$
cat /usr/local/sbin/sleep.sh
#! /bin/bash
sleep 100
[root@luo ~]# nohup sh /usr/local/sbin/sleep.sh &
[1] 3703

直接加一个 ‘&’ 虽然丢到后台了,可是当退出该终端时颇有可能这个脚本也会退出的,而在前面加上 nohup 就没有问题了,nohup的做用就是不挂断地运行命令。
二、screen工具的使用

简单来讲,screen是一个能够在多个进程之间多路复用一个物理终端的窗口管理器。screen中有会话的概念,用户能够在一个screen会话中建立多个screen窗口,在每个screen窗口中就像操做一个真实的SSH链接窗口那样。
一、打开一个会话,直接输入screen命令而后回车,进入screen会话窗口。若是你没有screen命令,就安装下:

[root@luo ~]# screen
-bash: screen: 未找到命令
[root@luo ~]# yum install -y screen

而后输入screen就进入到screen会话窗口,在会话窗口查看已经打开的会话:

[root@localhost ~]# screen -ls
There is a screen on:
    1396.pts-0.localhost    (Detached)
1 Socket in /var/run/screen/S-root.

先按Ctrl +a 再按d退出该screen会话,只是退出,并无结束。结束的话输入Ctrl +d 或者输入exit。
退出后还想再次登陆某个screen会话,使用sreen -r [screen 编号],这个编号就是上例中那个1396。当只有一个screen会话时,后面的编号是能够省略的。当你有某个须要长时间运行的命令或者脚本时就打开一个screen会话,而后运行该任务。按ctrl +a 再按d退出会话,不影响终端窗口上的任何操做。

扩展:

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