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

10.28 rsync工具介绍

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

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有六种不同的工作模式:
1、拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。如:rsync -a /data /backup
2、使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包含单个冒号":"分隔符时启动该模式。如:rsync -avz *.c foo:src
3、使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径包含单个冒号":"分隔符时启动该模式。如:rsync -avz foo:src/bar /data
4、从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含"::"分隔符时启动该模式。如:rsync -av [email protected]::www /databack
5、从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含"::"分隔符时启动该模式。如:rsync -av /databack [email protected]::www
6、列远程机的文件列表。这类似于rsync传输,不过只要在命令中省略掉本地机信息即可。如:rsync -v rsync://192.168.66.130/www

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的文件或者目录过滤掉,不同步

常用选项的操作:

1、将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

可以看到自动创建了目标目录,并且软链接原来指向哪同步后也是指向哪

2、加上-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

3、--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

4、在拷贝时添加--exclude,可以过滤指定的文件,支持多个--exclude命令共同使用

[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

5、-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/ [email protected]:/root/src1/    #加上-z选项会使用压缩,同步过去后再解压
[email protected]'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/        #不指定远程主机的用户,默认使用当前主机的用户
[email protected]'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 [email protected]::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 [email protected]::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这个用户了,默认是以当前用户的身份拷贝的,现在已经不需要输入密码了。
9、更改端口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系统日志

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

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

可以看到这些日志后面都跟了一些日期,这就证明它是按日期切割的,那他是这样做到的呢

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

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

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

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

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

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

10.35 screen工具

在工作中,我们也许会有这样的需求,要执行一个命令或者脚本,但是需要几个小时甚至几天。这就要考虑一个问题,就是中途断网或出现其他意外情况,执行的任务中断了怎么办?你可以把命令或者脚本丢到后台运行,不过也不保险。下面就介绍两种方法来避免这样的问题发生。
1、使用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的作用就是不挂断地运行命令。
2、screen工具的使用

简单来说,screen是一个可以在多个进程之间多路复用一个物理终端的窗口管理器。screen中有会话的概念,用户可以在一个screen会话中创建多个screen窗口,在每一个screen窗口中就像操作一个真实的SSH连接窗口那样。
1、打开一个会话,直接输入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

猜你喜欢

转载自blog.51cto.com/13736286/2130268