博客地址:http://www.moonxy.comshell
1、前言vim
日志文件记录了系统天天发生的各类各样的事情,好比监测系统情况、排查问题等。做为系统运维人员能够经过日志来检查错误发生的缘由,或者受到攻击时攻击者留下的痕迹。日志的主要功能是审计和监测,还能够实时地监测系统状态、监测和追踪侵入者等等。centos
2、系统日志文件服务器
Linux日志文件绝大多数存放在 /var/log 目录,其中一些日志文件由应用程序建立,其余的则经过 syslog 来建立。网络
通常状况下,只须要关注核心的系统和应用程序日志。运维
例如:ssh
全局系统活动信息等ide
基于 Debian 的系统,如 Ubuntu 在 /var/log/syslog 中存储。工具
基于 RedHat 的系统,如 RHEL 或 CentOS 在 /var/log/messages 中存储。post
验证和受权信息等
Ubuntu 在 /var/log/auth.log 中存储
RedHat 和 CentOS 在 /var/log/secure 中存储。
2.1 /var/log/messages
日志文件 /var/log/messages 是核心系统日志文件,包含了系统启动时的引导消息,以及系统运行时的其余状态消息。I/O 错误、网络错误和其余系统错误都会记录到这个文件中。还有某我的的身份切换为 root 以及用户自定义安装的软件(Apache)的日志也会出如今此文件中。/var/log/messages 日志文件由 rsyslogd 这个守护进程产生,rsyslog 服务的配置文件为 /etc/rsyslog.conf,这个文件定义了日志的级别。
系统有一个日志轮询的机制,每星期切换一个日志,变成 messages-xxxxxxxx,一个有5个这样的日志文件。好比:
[root@ryan ~]# ll /var/log/messag* -rw------- 1 root root 302275 Jul 11 21:20 /var/log/messages -rw------- 1 root root 201372 May 19 11:30 /var/log/messages-20180519 -rw------- 1 root root 101002 May 20 15:40 /var/log/messages-20180520 -rw------- 1 root root 402954 Jun 8 20:29 /var/log/messages-20180608 -rw------- 1 root root 402833 Jul 8 11:51 /var/log/messages-20180708
/etc/logrotate.conf 是日志的配置文件,可自定义上面的轮询周期和轮询次数等。
2.2 /var/log/secure
/var/log/secure 是和登陆信息有关的日志文件。该日志文件记录验证和受权等方面的信息,好比 ssh 登陆系统成功和失败时,相关的信息都会记录在这个日志里。
[root@ryan ~]# head /var/log/secure Jul 9 04:54:32 ryan sshd[1929]: Did not receive identification string from 197.89.57.193 port 56887 Jul 9 06:33:37 ryan sshd[2495]: Invalid user admin from 117.131.245.4 port 57960 Jul 9 06:33:37 ryan sshd[2495]: input_userauth_request: invalid user admin [preauth] Jul 9 06:33:37 ryan sshd[2495]: pam_unix(sshd:auth): check pass; user unknown Jul 9 06:33:37 ryan sshd[2495]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=117.131.245.4 Jul 9 06:33:39 ryan sshd[2495]: Failed password for invalid user admin from 117.131.245.4 port 57960 ssh2 Jul 9 06:33:39 ryan sshd[2495]: Connection closed by 117.131.245.4 port 57960 [preauth] Jul 9 06:33:42 ryan sshd[2498]: Invalid user admin from 113.173.47.127 port 35463 Jul 9 06:33:42 ryan sshd[2498]: input_userauth_request: invalid user admin [preauth] Jul 9 06:33:42 ryan sshd[2498]: pam_unix(sshd:auth): check pass; user unknown
2.3 /var/log/wtmp
/var/log/wtmp 记录了用户登陆成功的历史,可是这个是二进制文件,不能直接使用 cat、vim 等工具来查看。而是使用 last 命令来查看。以下:
[root@ryan ~]# last | head root pts/0 124.89.91.5x Wed Jul 11 21:29 still logged in root pts/0 124.89.91.5x Sun Jul 8 21:48 - 23:36 (01:48) root pts/0 124.89.91.5x Tue Jun 19 22:44 - 23:42 (00:57) root pts/0 124.89.91.5x Sat May 26 14:29 - 18:07 (03:37) root pts/0 124.89.91.5x Fri May 25 23:52 - 23:59 (00:07) root pts/0 124.89.91.5x Wed May 23 20:03 - 23:39 (03:35) root pts/0 124.89.91.5x Tue May 22 23:08 - 00:06 (00:58) root pts/0 124.89.91.5x Mon May 21 21:42 - 22:47 (01:05) root pts/0 124.89.91.5x Sun May 20 15:47 - 20:55 (05:07) root pts/0 124.89.91.5x Sat May 19 14:34 - 01:37 (11:02)
2.4 /var/log/btmp
/var/log/btmp 记录了用户登陆失败的历史,同时该文件也是二进制文件,须要使用 lastb 命令来查看。以下:
[root@ryan ~]# lastb | head root ssh:notty 221.131.87.254 Wed Jul 11 22:22 - 22:22 (00:00) admin ssh:notty 186.3.164.38 Wed Jul 11 19:19 - 19:19 (00:00) admin ssh:notty 186.3.164.38 Wed Jul 11 19:19 - 19:19 (00:00) admin ssh:notty 180.183.45.21 Wed Jul 11 19:18 - 19:18 (00:00) admin ssh:notty 180.183.45.21 Wed Jul 11 19:18 - 19:18 (00:00) root ssh:notty 58.218.198.161 Wed Jul 11 18:57 - 18:57 (00:00) root ssh:notty 58.218.198.161 Wed Jul 11 18:57 - 18:57 (00:00) root ssh:notty 58.218.198.161 Wed Jul 11 18:57 - 18:57 (00:00) root ssh:notty 58.218.198.161 Wed Jul 11 18:56 - 18:56 (00:00) root ssh:notty 58.218.198.161 Wed Jul 11 18:56 - 18:56 (00:00)
2.5 dmesg
dmesg 命令能够显示系统的启动信息,能够看到硬件的检测或者断开链接的信息,内核缓冲信息(kernel ring buffer)。用 dmesg 查看系统启动信息的文件对应为:/var/log/dmesg。
[root@ryan ~]# dmesg | head [ 0.000000] Initializing cgroup subsys cpuset [ 0.000000] Initializing cgroup subsys cpu [ 0.000000] Initializing cgroup subsys cpuacct [ 0.000000] Linux version 3.10.0-693.2.2.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) ) #1 SMP Tue Sep 12 22:26:13 UTC 2017 [ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-3.10.0-693.2.2.el7.x86_64 root=UUID=eb448abb-3012-4d8d-bcde-94434d586a31 ro crashkernel=auto rhgb quiet net.ifnames=0 console=tty0 console=ttyS0,115200n8 [ 0.000000] e820: BIOS-provided physical RAM map: [ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable [ 0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved [ 0.000000] BIOS-e820: [mem 0x00000000000f0000-0x00000000000fffff] reserved [ 0.000000] BIOS-e820: [mem 0x0000000000100000-0x000000003ffdffff] usable
3、系统服务管理
3.1 ntsysv 服务配置工具
ntsysv 命令用来配置哪些服务开启或者关闭,相似于图形界面,不过是使用键盘来控制的。若是系统没有这个命令,请使用 yum install -y ntsysv 安装它,安装完毕以后运行 ntsysv 命令,以下:
可使用空格来选中或者取消须要自动启动的服务,而后按 Tab 键来调到保存(OK)或者取消(Cancel),在按回车键。通常经常使用的服务如:crond、iptables、irqbalance、microcode_ctl、network、sendmail、sshd、syslog 均须要开启。修改以后须要重启服务器才能生效。
3.2 chkconfig 服务管理工具
Linux 系统全部的预设服务能够经过查看 /etc/init.d/ 目录获得,以下:
[root@ryan ~]# ll /etc/init.d lrwxrwxrwx. 1 root root 11 Nov 12 2017 /etc/init.d -> rc.d/init.d [root@ryan ~]# ls /etc/init.d auditd haldaemon kdump netconsole nfs-rdma rdma saslauthd udev-post crond halt killall netfs postfix restorecond single winbind dnsmasq ip6tables mdmonitor network pppoe-server rsyslog sshd wpa_supplicant functions iptables messagebus NetworkManager rdisc sandbox sysstat
系统的预设服务均可以经过这样的命令实现:service 服务名 start|stop|restart。这里的服务名就是 /etc/init.d/ 目录下的这些文件名了。如启动时可使用:"service crond start",也可使用命令:"/etc/init.d/crond start"。
独立服务启动办法:
1)/etc/init.d/ 或 /etc/rc.d/init.d 独立服务名 start|stop|status|restart
2)service 独立服务名 start|stop|status|restart ——【Redhat系列专有命令】,其实 service 就是去/ etc/init.d/ 这个目录下寻找指定的服务
第一种方法是启动服务的万能方法,而经过 service启动服务是Redhat系列专有命令。start 表示启动服务,stop 表示中止服务,status 表示查看指定服务是否启动,restart 表示重启指定服务。
使用 "chkconfig --list" 列出全部的服务及其各服务在每一个级别下的开启状态,以下:
[root@ryan ~]# chkconfig --list NetworkManager 0:off 1:off 2:on 3:on 4:on 5:on 6:off auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off dnsmasq 0:off 1:off 2:off 3:off 4:off 5:off 6:off haldaemon 0:off 1:off 2:off 3:on 4:on 5:on 6:off ip6tables 0:off 1:off 2:on 3:on 4:on 5:on 6:off iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off kdump 0:off 1:off 2:off 3:on 4:on 5:on 6:off mdmonitor 0:off 1:off 2:on 3:on 4:on 5:on 6:off messagebus 0:off 1:off 2:on 3:on 4:on 5:on 6:off netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off netfs 0:off 1:off 2:off 3:on 4:on 5:on 6:off network 0:off 1:off 2:on 3:on 4:on 5:on 6:off nfs-rdma 0:off 1:off 2:off 3:off 4:off 5:off 6:off postfix 0:off 1:off 2:on 3:on 4:on 5:on 6:off pppoe-server 0:off 1:off 2:off 3:off 4:off 5:off 6:off rdisc 0:off 1:off 2:off 3:off 4:off 5:off 6:off rdma 0:off 1:off 2:off 3:off 4:off 5:off 6:off restorecond 0:off 1:off 2:off 3:off 4:off 5:off 6:off rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off saslauthd 0:off 1:off 2:off 3:off 4:off 5:off 6:off sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off udev-post 0:off 1:on 2:on 3:on 4:on 5:on 6:off winbind 0:off 1:off 2:off 3:off 4:off 5:off 6:off wpa_supplicant 0:off 1:off 2:off 3:off 4:off 5:off 6:off
这里所说的级别(数字0-6)就是 /etc/inittab 里面对应的启动级别,运行级别0、1和6被系统保留。其中0做为 shutdown 动做,1做为重启至单用户模式,6为重启。在通常的 LInux 系统实现中,都是用 二、三、四、5 几个级别。在 CentOS 中,2表示无NFS支持的多用户模式,3表示彻底多用户模式(最经常使用),4保留给用户自定义,5表示图形化登陆方式。
开启或关闭某个服务的命令格式是:
chkconfig --level 级别 服务名 off/on
例如:
# chkconfig --level 345 crond off
--level 后面能够指定多个级别,默认是针对级别二、三、4和5操做的。
chkconfig 还能够把某个服务加入到系统服务或者删除,便可以使用 "chkconfig --add 服务名" 或者 "chkconfig --del 服务名"来操做,以下:
# chkconfig --add crond
# chkconfig --list |grep crond 或者 # chkconfig --list crond
# chkconfig --del crond
4、xargs 与 exec
xargs 与 exec 能够实现相同的功能,exec 主要是和 find 一块儿配合使用,是 find 的一个选项,而 xargs 比 exec 功能更多,经常使用在管道符以后。
4.1 xargs 应用
xargs命令是给其余命令传递参数的一个过滤器,也是组合多个命令的一个工具。它擅长将标准输入数据转换成命令行参数,xargs 可以处理管道或者stdin并将其转换成特定命令的命令参数。xargs也能够将单行或多行文本输入转换为其余格式,例如多行变单行,单行变多行。xargs 的默认命令是 echo,空格是默认定界符。这意味着经过管道传递给 xargs 的输入将会包含换行和空白,不过经过 xargs 的处理,换行和空白将被空格取代。xargs 是构建单行命令的重要组件之一。
好比查找当前目录下建立时间大于10天的文件,而后删除。命令以下:
# find . -mtime +10 |xargs rm -rf
批量命名文件,将当前目录下全部的 txt 文件统一添加后缀_bak。命令以下:
# ls *.txt |xargs -n1 -i{} mv {} {}_bak
以下所示:
[root@ryan mytest]# ll total 12 -rw-r--r-- 1 root root 4 Jul 12 22:05 1.txt -rw-r--r-- 1 root root 4 Jul 12 22:06 2.txt -rw-r--r-- 1 root root 4 Jul 12 22:06 3.bat [root@ryan mytest]# ls *.txt |xargs -n1 -i{} mv {} {}_bak [root@ryan mytest]# ll total 12 -rw-r--r-- 1 root root 4 Jul 12 22:05 1.txt_bak -rw-r--r-- 1 root root 4 Jul 12 22:06 2.txt_bak -rw-r--r-- 1 root root 4 Jul 12 22:06 3.bat
其中,"xargs -n1 -i{}" 相似于 for 循环,-n1 表示对逐个对象进行处理,-i{} 表示用 {} 取代前面的对象,mv {} {}_bak至关于 mv file1.txt file1.txt_bak。
xargs 命令用法
xargs 用做替换工具,读取输入数据从新格式化后输出。
定义一个测试文件,内有多行文本数据:
cat test.txt a b c d e f g h i j k l m n o p q r s t u v w x y z
多行输入单行输出:
cat test.txt | xargs a b c d e f g h i j k l m n o p q r s t u v w x y z
-n选项多行输出:
cat test.txt | xargs -n3 a b c d e f g h i j k l m n o p q r s t u v w x y z
-d选项能够自定义一个定界符:
echo "nameXnameXnameXname" | xargs -dX name name name name
结合-n选项使用:
echo "nameXnameXnameXname" | xargs -dX -n2 name name name name
xargs的一个选项-I,使用-I指定一个替换字符串{},这个字符串在xargs扩展时会被替换掉,当-I与xargs结合使用,每个参数命令都会被执行一次:
cat arg.txt | xargs -I {} ./sk.sh -p {} -l -p aaa -l -p bbb -l -p ccc -l
-i[replace-str] 选项已通过期,使用 -I 选项代替。
4.2 exec 应用
exec 命令用于调用并执行指令的命令。exec 命令一般用在 shell 脚本程序中,能够调用其余的命令。若是在当前终端中使用命令,则当指定的命令执行完毕后会当即退出终端。-exec 做为 find 命令的选项,与 -ok 选项的做用相似,可是会询问用户(Like -exec but ask the user first)。
好比查找当前目录下建立时间大于10天的文件并删除:
# find . -mtime +10 -exec rm -rf {} \;
这个命令中也是用 {} 替代前面 find 出来的文件。后的 \ 做为 ; 的转义符,不然 shell 会把分号做为该行命令的结尾。
-exec 一样能够实现上面提到的批量命名的功能,以下:
# find ./*.txt -exec mv {} {}_bak \;
5、screen 工具
5.1 使用 nohup
nohup
使用 & 命令后,做业被提交到后台运行,当前控制台没有被占用,可是一但把当前控制台关掉(退出账户时),做业可能就会中止运行。nohup 命令能够在你退出帐户以后继续运行相应的进程。nohup 就是不挂起的意思(no hang up)。该命令的通常形式为:
# nohup command &
若是使用 nohup 命令提交做业,那么在缺省状况下该做业的全部输出都被重定向到一个名为 nohup.out 的文件中,除非另外指定了输出文件:
# nohup command > myout.file 2>&1 &
使用了 nohup 以后,不少人就这样无论了,其实这样有可能在当前帐户非正常退出或者结束的时候,命令仍是本身结束了。因此在使用 nohup 命令后台运行命令以后,须要使用exit正常退出当前帐户,这样才能保证命令一直在后台运行。
5.2 screen 工具
screen 是一个能够在多个进程之间多路复用一个物理终端的窗口管理器。screen 中有会话的概念,用户能够在一个screen会话中建立多个screen窗口,在每一个screen窗口中建立多个 screen 窗口,在每个screen窗口中就像操做一个真实的SSH链接窗口同样。
screen 默认没有安装在 Linux 中,可使用以下命令安装:
# yum install -y screen
运行 # screen 命令后会打开一个新的screen会话:
[root@ryan ~]# screen -ls There is a screen on: 1935.pts-1.ryan (Attached) 1 Socket in /var/run/screen/S-root.
以下所示:
而后按 Ctrl+a+d 键(先按 Ctrl+a,再按d)能够退出该 screen 会话,此时只是退出,并无结束,结束 screen 会话要按 Ctrl+a 键或者输入 exit。
[root@ryan ~]# screen [detached] [root@ryan ~]# screen -ls There is a screen on: 1935.pts-1.ryan (Detached) 1 Socket in /var/run/screen/S-root.
退出后若是还想再次登陆某个 screen 会话,可使用命令:screen -r [screen 编号],好比上面的 1935。若是只打开了一个 screen 会话,后面的编号能够省略。
当咱们有某个须要长时间运行的命令或者脚本时就打开一个 screen 会话, 而后运行该任务,按 Ctrl+a 键,在按d退出会话,这样就不会影响终端窗口上其余的操做了。