1.查看日誌常用命令
tail:
-n 是顯示行號;相當於nl命令;例子如下:
tail -100f test.log 實時監控100行日誌
tail -n 10 test.log 查詢日誌尾部最後10行的日誌;
tail -n +10 test.log 查詢10行之後的所有日誌;
head:
跟tail是相反的,tail是看後多少行日誌;例子如下:
head -n 10 test.log 查詢日誌文件中的頭10行日誌;
head -n -10 test.log 查詢日誌文件除了最後10行的其他所有日誌;
cat:
tac是倒序查看,是cat單詞反寫;例子如下:
cat -n test.log |grep "debug" 查詢關鍵字的日誌
2. 應用場景一:按行號查看---過濾出關鍵字附近的日誌
1)cat -n test.log |grep "debug" 得到關鍵日誌的行號
2)cat -n test.log |tail -n +92|head -n 20 選擇關鍵字所在的中間一行. 然後查看這個關鍵字前10行和後10行的日誌:
tail -n +92表示查詢92行之後的日誌
head -n 20 則表示在前面的查詢結果裏再查前20條記錄
3. 應用場景二:根據日期查詢日誌
sed -n '/2014-12-17 16:17:20/,/2014-12-17 16:17:36/p' test.log
特別說明:上面的兩個日期必須是日誌中打印出來的日誌,否則無效;
先 grep '2014-12-17 16:17:20' test.log 來確定日誌中是否有該 時間點
4.應用場景三:日誌內容特別多,打印在屏幕上不方便查看
(1)使用more和less命令,
如: cat -n test.log |grep "debug" |more 這樣就分頁打印了,通過點擊空格鍵翻頁
(2)使用 >xxx.txt 將其保存到文件中,到時可以拉下這個文件分析
如:cat -n test.log |grep "debug" >debug.txt
5.應用場景四:開啓、關閉防火牆端口
centos7防火牆由firewalle管理,不是iptables
停止firewalld服務 systemctl stop firewalld
禁用firewalld服務 systemctl mask firewalld
開啓 systemctl unmask firewalld
iptables操作:
安裝iptables-services:yum install iptables-services
開啓、關閉、重啓防火牆: service iptables [stop|start|restart]
或者:systemctl [stop|start|restart] iptables
永久關閉防火牆:chkconfig iptables off
永久關閉後重啓:chkconfig iptables on
設置開機啓動:systemctl enable iptables
保存:service iptables save 或者 /usr/libexec/iptables/iptables.init save
開放某個端口:
查看防火牆開放的端口:more /etc/sysconfig/iptables
放開某個端口(如:8080)
(1)通過vim /etc/sysconfig/iptables 進入編輯增添一條 -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
(2)執行 /etc/init.d/iptables restart 命令將iptables服務重啓