對于Linux 的系統安全來說,日志檔案是極其重要的工具。系統管理者可以使用logrotate 程式用來管理系統中的最新的事件,對于Linux 的系統安全來說,日志檔案是極其重要的工具。系統管理者可以使用logrotate 程式用來管理系統中的最新的事件。logrotate 還可以用來備份日志檔案,本篇将通過以下幾部分來介紹
日志檔案的管理:
1、logrotate 配置
2、預設配置 logrotate
3、使用include 選項讀取其他配置檔案
4、使用include 選項覆寫預設配置
5、為指定的檔案配置轉儲參數
一、logrotate 配置
logrotate 程式是一個日志檔案管理工具。用來把舊的日志檔案删除,并建立新的日志檔案,我們把它叫做“轉儲”。我們可以根據日志檔案的大小,也可以根據其天數來轉儲,這個過程一般通過 cron 程式來執行。
logrotate 程式還可以用于壓縮日志檔案,以及發送日志到指定的E-mail 。
logrotate 的配置檔案是 /etc/logrotate.conf。主要參數如下表:
參數 功能
compress 通過gzip 壓縮轉儲以後的日志
nocompress 不需要壓縮時,用這個參數
copytruncate 用于還在打開中的日志檔案,把目前日志備份并截斷
nocopytruncate 備份日志檔案但是不截斷
create mode owner group 轉儲檔案,使用指定的檔案模式建立新的日志檔案
nocreate 不建立新的日志檔案
delaycompress 和 compress 一起使用時,轉儲的日志檔案到下一次轉儲時才壓縮
nodelaycompress 覆寫 delaycompress 選項,轉儲同時壓縮。
errors address 專儲時的錯誤資訊發送到指定的Email 位址
ifempty 即使是空檔案也轉儲,這個是 logrotate 的預設選項。
notifempty 如果是空檔案的話,不轉儲
mail address 把轉儲的日志檔案發送到指定的E-mail 位址
nomail 轉儲時不發送日志檔案
olddir directory 轉儲後的日志檔案放入指定的目錄,必須和目前日志檔案在同一個檔案系統
noolddir 轉儲後的日志檔案和目前日志檔案放在同一個目錄下
prerotate/endscript 在轉儲以前需要執行的指令可以放入這個對,這兩個關鍵字必須單獨成行
postrotate/endscript 在轉儲以後需要執行的指令可以放入這個對,這兩個關鍵字必須單獨成行
daily 指定轉儲周期為每天
weekly 指定轉儲周期為每周
monthly 指定轉儲周期為每月
rotate count 指定日志檔案删除之前轉儲的次數,0 指沒有備份,5 指保留5 個備份
tabootext [+] list 讓logrotate 不轉儲指定擴充名的檔案,預設的擴充名是:.rpm-orig, .rpmsave, v, 和 ~
size size 當日志檔案到達指定的大小時才轉儲,Size 可以指定 bytes (預設)以及KB (sizek)或者MB (sizem).
二、預設配置 logrotate
logrotate 預設的配置募?/etc/logrotate.conf。
Red Hat Linux 預設安裝的檔案内容是:
# see "man logrotate" for details
# rotate log files weekly
weekly
# keep 4 weeks worth of backlogs
rotate 4
# send errors to root
errors root
# create new (empty) log files after rotating old ones
create
# uncomment this if you want your log files compressed
#compress
1
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d
# no packages own lastlog or wtmp --we'll rotate them here
/var/log/wtmp {
monthly
create 0664 root utmp
rotate 1
}
/var/log/lastlog {
# system-specific logs may be configured here
預設的配置一般放在logrotate.conf 檔案的最開始處,影響整個系統。在本例中就是前面12行。
第三行weekly 指定所有的日志檔案每周轉儲一次。
第五行 rotate 4 指定轉儲檔案的保留 4份。
第七行 errors root 指定錯誤資訊發送給root。
第九行create 指定 logrotate 自動建立新的日志檔案,新的日志檔案具有和
原來的檔案一樣的權限。
第11行 #compress 指定不壓縮轉儲檔案,如果需要壓縮,去掉注釋就可以了。
三、使用include 選項讀取其他配置檔案
include 選項允許系統管理者把分散到幾個檔案的轉儲資訊,集中到一個
主要的配置檔案。當 logrotate 從logrotate.conf 讀到include 選項時,會從指定檔案讀入配置資訊,就好像他們已經在/etc/logrotate.conf 中一樣。
第13行 include /etc/logrotate.d 告訴 logrotate 讀入存放在/etc/logrotate.d 目錄中的日志轉儲參數,當系統中安裝了RPM 軟體包時,使用include 選項十分有用。RPM 軟體包的日志轉儲參數一般存放在/etc/logrotate.d 目錄。
include 選項十分重要,一些應用把日志轉儲參數存放在 /etc/logrotate.d 。
典型的應用有:apache, linuxconf, samba, cron 以及syslog。
這樣,系統管理者隻要管理一個 /etc/logrotate.conf 檔案就可以了。
四、使用include 選項覆寫預設配置
當 /etc/logrotate.conf 讀入檔案時,include 指定的檔案中的轉儲參數将覆寫預設的參數,如下例:
# linuxconf 的參數
/var/log/htmlaccess.log
{ errors jim
notifempty
nocompress
prerotate
/usr/bin/chattr -a /var/log/htmlaccess.log
endscript
postrotate
/usr/bin/chattr +a /var/log/htmlaccess.log
/var/log/netconf.log
{ nocompress
在這個例子中,當 /etc/logrotate.d/linuxconf 檔案被讀入時,下面的參數将覆寫/etc/logrotate.conf中預設的參數。
Notifempty
errors jim
五、為指定的檔案配置轉儲參數
經常需要為指定檔案配置參數,一個常見的例子就是每月轉儲/var/log/wtmp。為特定檔案而使用的參數格式是:
# 注釋
/full/path/to/file
{
option(s)
下面的例子就是每月轉儲 /var/log/wtmp 一次:
#Use logrotate to rotate wtmp
/var/log/wtmp
六、其他需要注意的問題
1、盡管花括号的開頭可以和其他文本放在同一行上,但是結尾的花括号必須單獨成行。
2、使用 prerotate 和 postrotate 選項
下面的例子是典型的腳本 /etc/logrotate.d/syslog,這個腳本隻是對
/var/log/messages 有效。
/var/log/messages
{
/usr/bin/chattr -a /var/log/messages
/usr/bin/kill -HUP syslogd
/usr/bin/chattr +a /var/log/messages
第一行指定腳本對 /var/log messages 有效
花括号外的/var/log messages
prerotate 指令指定轉儲以前的動作/usr/bin/chattr -a 去掉/var/log/messages檔案的“隻追加”屬性 endscript 結束 prerotate 部分的腳本postrotate 指定轉儲後的動作
/usr/bin/killall -HUP syslogd
用來重新初始化系統日志守護程式 syslogd
/usr/bin/chattr +a /var/log/messages
重新為 /var/log/messages 檔案指定“隻追加”屬性,這樣防治程式員或使用者覆寫此檔案。
最後的 endscript 用于結束 postrotate 部分的腳本
3、logrotate 的運作分為三步:
判斷系統的日志檔案,建立轉儲計劃以及參數,通過cron daemon 運作下面的代碼是 Red Hat Linux 預設的crontab 來每天運作logrotate。
#/etc/cron.daily/logrotate
#! /bin/sh
/usr/sbin/logrotate /etc/logrotate.conf
4、/var/log/messages 不能産生的原因:
這種情況很少見,但是如果你把/etc/services 中的 514/UDP 端口關掉的話,這個檔案就不能産生了。
小結:本文通過對Red Hat 系統上典型的logrotate 配置例子的介紹,詳細說明了logrotate 程式的應用方法。希望對所有Linux 系統管理者有所幫助。管理好,分析好日志檔案是系統安全的第一步,在以後的文章裡FreeLAMP還會介紹另外一個檢查日志的好東東 logcheck
日志檔案包含了關于系統中發生的事件的有用資訊,在排障過程中或者系統性能分析時經常被用到。對于忙碌的伺服器,日志檔案大小會增長極快,伺服器會很快消耗磁盤空間,這成了個問題。除此之外,處理一個單個的龐大日志檔案也常常是件十分棘手的事。

主流Linux發行版上都預設安裝有logrotate包,如果出于某種原因,logrotate沒有出現在裡頭,你可以使用apt-get或yum指令來安裝。
在Debian或Ubuntu上:
# apt-get install logrotate cron
在Fedora,CentOS或RHEL上:
# yum install logrotate crontabs
logrotate的配置檔案是/etc/logrotate.conf,通常不需要對它進行修改。日志檔案的輪循設定在獨立的配置檔案中,它(們)放在/etc/logrotate.d/目錄下。
樣例一
在第一個樣例中,我們将建立一個10MB的日志檔案/var/log/log-file。我們将展示怎樣使用logrotate來管理該日志檔案。
我們從建立一個日志檔案開始吧,然後在其中填入一個10MB的随機比特流資料。
# touch /var/log/log-file# head -c 10M < /dev/urandom > /var/log/log-file
由于現在日志檔案已經準備好,我們将配置logrotate來輪循該日志檔案。讓我們為該檔案建立一個配置檔案。
# vim /etc/logrotate.d/log-file
/var/log/log-file { monthly rotate 5 compress delaycompress missingok notifempty create 644 root root postrotate /usr/bin/killall -HUP rsyslogd endscript}
這裡:
- monthly: 日志檔案将按月輪循。其它可用值為‘daily’,‘weekly’或者‘yearly’。
- rotate 5: 一次将存儲5個歸檔日志。對于第六個歸檔,時間最久的歸檔将被删除。
- compress: 在輪循任務完成後,已輪循的歸檔将使用gzip進行壓縮。
- delaycompress: 總是與compress選項一起用,delaycompress選項訓示logrotate不要将最近的歸檔壓縮,壓縮将在下一次輪循周期進行。這在你或任何軟體仍然需要讀取最新歸檔時很有用。
- missingok: 在日志輪循期間,任何錯誤将被忽略,例如“檔案無法找到”之類的錯誤。
- notifempty: 如果日志檔案為空,輪循不會進行。
- create 644 root root: 以指定的權限建立全新的日志檔案,同時logrotate也會重命名原始日志檔案。
- postrotate/endscript: 在所有其它指令完成後,postrotate和endscript裡面指定的指令将被執行。在這種情況下,rsyslogd 程序将立即再次讀取其配置并繼續運作。
上面的模闆是通用的,而配置參數則根據你的需求進行調整,不是所有的參數都是必要的。
樣例二
在本例中,我們隻想要輪循一個日志檔案,然而日志檔案大小可以增長到50MB。
# vim /etc/logrotate.d/log-file
/var/log/log-file { size=50M rotate 5 create 644 root root postrotate /usr/bin/killall -HUP rsyslogd endscript}
樣例三
我們想要讓舊日志檔案以建立日期命名,這可以通過添加dateext常熟實作。
# vim /etc/logrotate.d/log-file
/var/log/log-file { monthly rotate 5 dateext create 644 root root postrotate /usr/bin/killall -HUP rsyslogd endscript}
這将讓歸檔檔案在它們的檔案名中包含日期資訊。
排障
這裡提供了一些logrotate設定的排障提示。
1. 手動運作logrotate
logrotate可以在任何時候從指令行手動調用。
要調用為/etc/lograte.d/下配置的所有日志調用logrotate:
# logrotate /etc/logrotate.conf
要為某個特定的配置調用logrotate:
# logrotate /etc/logrotate.d/log-file
2. 演練
排障過程中的最佳選擇是使用‘-d’選項以預演方式運作logrotate。要進行驗證,不用實際輪循任何日志檔案,可以模拟演練日志輪循并顯示其輸出。
# logrotate -d /etc/logrotate.d/log-file
正如我們從上面的輸出結果可以看到的,logrotate判斷該輪循是不必要的。如果檔案的時間小于一天,這就會發生了。
3. 強制輪循
即使輪循條件沒有滿足,我們也可以通過使用‘-f’選項來強制logrotate輪循日志檔案,‘-v’參數提供了詳細的輸出。
# logrotate -vf /etc/logrotate.d/log-file
reading config file /etc/logrotate.d/log-filereading config info for /var/log/log-file Handling 1 logs rotating pattern: /var/log/log-file forced from command line (5 rotations)empty log files are rotated, old logs are removedconsidering log /var/log/log-file log needs rotatingrotating log /var/log/log-file, log->rotateCount is 5dateext suffix '-20140916'glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'renaming /var/log/log-file.5.gz to /var/log/log-file.6.gz (rotatecount 5, logstart 1, i 5),old log /var/log/log-file.5.gz does not existrenaming /var/log/log-file.4.gz to /var/log/log-file.5.gz (rotatecount 5, logstart 1, i 4),old log /var/log/log-file.4.gz does not exist. . .renaming /var/log/log-file.0.gz to /var/log/log-file.1.gz (rotatecount 5, logstart 1, i 0),old log /var/log/log-file.0.gz does not existlog /var/log/log-file.6.gz doesn't exist -- won't try to dispose of itrenaming /var/log/log-file to /var/log/log-file.1creating new /var/log/log-file mode = 0644 uid = 0 gid = 0running postrotate scriptcompressing log with: /bin/gzip
4. Logrotate的記錄日志
logrotate自身的日志通常存放于/var/lib/logrotate/status目錄。如果處于排障目的,我們想要logrotate記錄到任何指定的檔案,我們可以指定像下面這樣從指令行指定。
# logrotate -vf –s /var/log/logrotate-status /etc/logrotate.d/log-file
5. Logrotate定時任務
logrotate需要的cron任務應該在安裝時就自動建立了,我把cron檔案的内容貼出來,以供大家參考。
# cat /etc/cron.daily/logrotate
#!/bin/sh # Clean non existent log file entries from status filecd /var/lib/logrotatetest -e status || touch statushead -1 status > status.cleansed 's/"//g' status | while read logfile datedo [ -e "$logfile" ] && echo "\"$logfile\" $date"done >> status.cleanmv status.clean status test -x /usr/sbin/logrotate || exit 0/usr/sbin/logrotate /etc/logrotate.conf
小結一下,logrotate工具對于防止因龐大的日志檔案而耗盡存儲空間是十分有用的。配置完畢後,程序是全自動的,可以長時間在不需要人為幹預下運作。本教程重點關注幾個使用logrotate的幾個基本樣例,你也可以定制它以滿足你的需求。