一、前言
日志文件記錄了系統每天發生的各種各樣的事情,比如監測系統狀況、排查問題等。作為系統運維人員可以通過日志來檢查錯誤發生的原因,或者受到攻擊時攻擊者留下的痕跡。日志的主要功能是審計和監測,還可以實時地監測系統狀態、監測和追蹤侵入者等等。
二、系統日志文件
Linux日志文件絕大多數存放在 /var/log 目錄,其中一些日志文件由應用程序創建,其他的則通過 syslog 來創建。
一般情況下,只需要關注核心的系統和應用程序日志。
例如:
全局系統活動信息等
基於 Debian 的系統,如 Ubuntu 在 /var/log/syslog 中存儲。
基於 RedHat 的系統,如 RHEL 或 CentOS 在 /var/log/messages 中存儲。
驗證和授權信息等
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.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 系統實現中,都是用 2、3、4、5 幾個級別。在 CentOS 中,2表示無NFS支持的多用戶模式,3表示完全多用戶模式(最常用),4保留給用戶自定義,5表示圖形化登錄方式。
開啟或關閉某個服務的命令格式是:
chkconfig --level 級別 服務名 off/on
例如:
# chkconfig --level 345 crond off
--level 后面可以指定多個級別,默認是針對級別2、3、4和5操作的。
chkconfig 還可以把某個服務加入到系統服務或者刪除,即可以使用 "chkconfig --add 服務名" 或者 "chkconfig --del 服務名"來操作,如下:
# chkconfig --add crond
# chkconfig --list |grep crond 或者 # chkconfig --list crond
# chkconfig --del crond
四、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 \;
五、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退出會話,這樣就不會影響終端窗口上其他的操作了。
