10.34 linux系統日誌

linux系統日誌

  • /var/log/messages //是linux系統一個總的日誌——>除非某些服務,有定義單獨的日誌
  • /etc/logrotate.conf 日誌切割配置文件
  • 參考日誌文件文章
  • dmesg命令
  • /var/log/dmesg 日誌
  • last命令,調用的文件/var/log/wtmp
  • lastb命令查看登陸失敗的用戶,對應的文件時/var/log/btmp
  • /var/log/secure

系統日誌

  • /var/log/messages //是linux系統一個總的日誌——>除非某些服務,有定義單獨的日誌
    • 系統中存有一個日誌切割機制,日誌的滾動,在增加到必定級別了,就會自動切割
[root@hf-01 ~]# 
[root@hf-01 ~]# ls /var/log/messages
/var/log/messages
[root@hf-01 ~]# less !$
less /var/log/messages
[root@hf-01 ~]# du -sh !$
du -sh /var/log/messages
388K	/var/log/messages
  • 在查看日誌的時候,會發現日誌自動切割了
    • linux系統中有一個logrotate服務,會自動切割日誌,防止無限制的增長
[root@hf-01 ~]# ls /var/log/messages*
/var/log/messages           /var/log/messages-20171120  /var/log/messages-20171203
/var/log/messages-20171112  /var/log/messages-20171127
[root@hf-01 ~]#
  • 查看日誌文件cat /etc/logrotate.conf
    • 它會告訴你
[root@hf-01 ~]# cat /etc/logrotate.conf
# see "man logrotate" for details        //會告訴你查看logrotate的幫助文檔
# 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        //是否須要壓縮,壓縮成 .tar.gz 

# 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.
[root@hf-01 ~]#
  • 查看 /etc/logrotate.d/syslog 文件
    • 它會爲cron,maillog,messages,secure,spooler這幾個日誌進行切割
      • messages日誌是由 syslogd 服務決定的,因此 kill -HUP 就會從新加載這個日誌
    • 還有一個腳本,shell命令行,在把日誌切割後(挪走),更名字生成新的日誌
  • Linux系統有一個特色,一個服務寫一個文件的時候,並非按照文件名去寫的,而是根據inode來寫的
[root@hf-01 ~]# ls /etc/logrotate.d
ppp  samba  syslog  wpa_supplicant  yum
[root@hf-01 ~]# cat /etc/logrotate.d/syslog
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
    sharedscripts
    postrotate
	/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript
}
[root@hf-01 ~]#

參考文件日誌文章

dmesg命令

  • dmesg命令,會把系統硬件相關的日誌列出來
    • 這個日誌是保存在內存中的,並非一個文件
  • 假如你的網卡有問題了,硬盤損壞了,都會記錄在這個日誌中
  • dmesg -c //清空當前日誌
    • 可是一重啓這個系統,又會生成這些日誌

/var/log/dmesg日誌文件

  • /var/log/dmesg //這是一個日誌文件
    • 這個日誌文件和 dmesg命令 沒有任何關聯
    • 它是系統啓動的一個日誌,記錄的信息

last命令

  • last命令,查看你正確的登陸歷史
    • 調用的文件/var/log/wtmp
    • 記錄是是誰,在哪裏,來源IP,時間,登陸的時長都會有記錄
  • /var/log/wtmp日誌是一個二進制文件,不能直接cat查看的,只能用last命令去查看
[root@hf-01 ~]# last
root     pts/0        192.168.74.1     Thu Dec  7 01:41   still logged in   
reboot   system boot  3.10.0-123.el7.x Thu Dec  7 01:36 - 05:38  (04:01)    
root     pts/1        192.168.74.1     Wed Dec  6 05:11 - 05:13  (00:01)    
root     pts/0        192.168.74.1     Wed Dec  6 01:27 - crash (1+00:09)   
reboot   system boot  3.10.0-123.el7.x Wed Dec  6 01:26 - 05:38 (1+04:11)   
root     pts/0        192.168.74.1     Tue Dec  5 01:38 - crash  (23:48)    
reboot   system boot  3.10.0-123.el7.x Tue Dec  5 01:37 - 05:38 (2+04:00)   
root     pts/0        192.168.74.1     Mon Dec  4 03:23 - down   (02:57)    
root     pts/0        192.168.74.1     Sun Dec  3 17:42 - 03:23  (09:40)    
等等等,只截取了一部分

[root@hf-01 ~]# ls /var/log/wtmp
/var/log/wtmp

lastb命令

  • lastb命令,查看登陸失敗的用戶
    • 對應的文件時/var/log/btmp 日誌
  • /var/log/btmp也是二進制文件,不能直接cat的
[root@hf-01 ~]# lastb

btmp begins Sat Dec  2 04:25:01 2017
[root@hf-01 ~]# ls /var/log/btmp
/var/log/btmp
[root@hf-01 ~]#

安全日誌

  • /var/log/secure
    • 好比登陸操做系統,驗證成功會在這裏記錄一個日誌,失敗也會去記錄
[root@hf-01 ~]# ls /var/log/secure
/var/log/secure
[root@hf-01 ~]#
相關文章
相關標籤/搜索