博客地址: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退出會話,這樣就不會影響終端窗口上其餘的操做了。