77.Linux系統日誌,screen工具介紹

Linux系統日誌

日誌重要嗎?必須的,沒有日誌我們怎麼知道系統狀況?沒有日誌如何排查一個trouble?日誌記錄了系統每天發生的各種各樣的事情,你可以通過他來檢查錯誤發生的原因,或者受到***時***者留下的痕跡。日誌主要的功能有:審計和監測,還可以實時的監測系統狀態,監測和追蹤侵入者等等。
常查看的日誌文件爲/var/log/message, 它是核心系統日誌文件,包含了系統啓動時的引導消息,以及系統運行時的其他狀態消息。IO錯誤、網絡錯誤和其他系統錯誤都會記錄到這個文件中。另外其他信息,比如某個人的身份切換爲root以及用戶自定義安裝的軟件(apache)的日誌也會在這裏列出。通常,/var/log/messages是在做故障診斷時首先要查看的文件。系統有一個日誌輪詢的機制,每星期切換一個日誌,變成message.xxxxxxxx, message.xxxxxxxx, ... messages.xxxxxxxx 連同messages一共有5個這樣的日誌文件。這裏的xxxxxxxx就是按照日期的格式生成的文件,這是通過logrotate工具的控制來實現的,它的配置文件是/etc/logrotate.conf如果沒有特殊需求請不要修改這個配置文件。

[root@localhost ~]#cat /etc/logrotate.conf 
# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
    minsize 1M
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

# system-specific logs may be also be configured here.
/var/log/messages是由syslogd這個守護進程產生的,如果停掉這個服務則系統不會產生/var/log/messages,所以這個服務不要停。Syslogd服務的配置文件爲/etc/syslog.conf這個文件定義了日誌的級別,有需求使用man syslog.conf獲得更多關於它的信息。
除了關注/var/log/messages外,你還應該多關注一下dmesg這個命令,它可以顯示系統的啓動信息,如果你的某個硬件有問題(比如說網卡)用這個命令也是可以看到的。
[root@localhost ~]# 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-514.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) ) #1 SMP Tue Nov 22 16:42:41 UTC 2016
[    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.10.0-514.el7.x86_64 root=UUID=1cec20ae-09f9-4d50-a2eb-5c1b1a4ab741 ro crashkernel=auto rhgb quiet LANG=zh_CN.UTF-8
[    0.000000] Disabled fast string operations
[    0.000000] e820: BIOS-provided physical RAM map:
[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009ebff] usable
[    0.000000] BIOS-e820: [mem 0x000000000009ec00-0x000000000009ffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000000dc000-0x00000000000fffff] reserved

關於安全方面的日誌,簡單介紹幾個命令或者日誌。

last命令:

[root@localhost ~]#last |head
root pts/0 192.168.204.1 Wed Jan 31 16:45 still logged in
root tty1 Wed Jan 24 22:57 still logged in
root pts/0 192.168.204.1 Wed Jan 24 20:09 - 22:39 (02:30)
reboot system boot 3.10.0-514.el7.x Wed Jan 24 20:08 - 18:09 (6+22:01)
root pts/0 192.168.204.1 Wed Jan 24 19:57 - down (00:11)
reboot system boot 3.10.0-514.el7.x Wed Jan 24 19:57 - 20:08 (00:11)
root pts/0 192.168.204.1 Wed Jan 24 18:20 - 19:56 (01:36)
root pts/0 192.168.204.1 Wed Jan 24 16:58 - 18:20 (01:21)
root pts/1 192.168.204.1 Tue Jan 23 17:30 - 18:19 (00:48)
root pts/0 192.168.204.1 Tue Jan 23 16:48 - 18:19 (01:30)
last命令用來查看登錄Linux歷史信息,從左至右依次爲賬戶名稱、登錄終端、登錄客戶端ip、登錄日期及時長。last命令輸出的信息實際上是讀取了二進制日誌文件/var/log/wtmp, 只是這個文件不能直接使用cat, vim, head, tail等工具查看。
另外一個和登陸信息有關的日誌文件爲/var/log/secure, 該日誌文件記錄驗證和授權等方面的信息,比如ssh登陸系統成功或者失敗,都會把相關信息記錄在這個日誌裏。











screen工具介紹

在工作中,我們也許會有這樣的需求,要執行一個命令或者腳本,但是需要幾個小時甚至幾天。這就要考慮一個問題,就是中途斷網或出現其他意外情況,執行的任務中斷了怎麼辦?你可以把命令或者腳本丟到後臺運行,不過也不保險。下面就介紹兩種方法來避免這樣的問題發生。

1、使用nohup

[root@zlinux ~]# vim /usr/local/sbin/sleep.sh
[root@zlinux ~]# cat !$
cat /usr/local/sbin/sleep.sh
#! /bin/bash
sleep 1000
[root@zlinux ~]# nohup sh /usr/local/sbin/sleep.sh &
[1] 3703
直接加一個 ‘&’ 雖然丟到後臺了,但是當退出該終端時很有可能這個腳本也會退出的,而在前面加上 nohup 就沒有問題了,nohup的作用就是不掛斷地運行命令。






2、screen工具的使用

簡單來說,screen是一個可以在多個進程之間多路複用一個物理終端的窗口管理器。screen中有會話的概念,用戶可以在一個screen會話中創建多個screen窗口,在每一個screen窗口中就像操作一個真實的SSH連接窗口那樣。
1、打開一個會話,直接輸入screen命令然後回車,進入screen會話窗口。如果你沒有screen命令,就安裝下:
[root@zlinux ~]# screen
-bash: screen: 未找到命令
[root@zlinux ~]# yum install -y screen
然後輸入screen就進入到screen會話窗口,在會話窗口查看已經打開的會話:
[root@zlinux ~]# screen -ls
There is a screen on:
3754.pts-0.zlinux (Attached)
1 Socket in /var/run/screen/S-root.
Ctrl +a 再按d退出該screen會話,只是退出,並沒有結束。結束的話輸入Ctrl +d 或者輸入exit。
退出後還想再次登錄某個screen會話,使用sreen -r [screen 編號],這個編號就是上例中那個3754。當只有一個screen會話時,後面的編號是可以省略的。當你有某個需要長時間運行的命令或者腳本時就打開一個screen會話,然後運行該任務。按ctrl +a 再按d退出會話,不影響終端窗口上的任何操作。










發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章