linux下logrotate 配置和理解

 對於Linux 的系統安全來講,日誌文件是極其重要的工具。系統管理員可使用logrotate 程序用來管理系統中的最新的事件,對於Linux 的系統安全來講,日誌文html

件是極其重要的工具。系統管理員可使用logrotate 程序用來管理系統中的最新的事件。logrotate 還能夠用來備份日誌文件,本篇將經過如下幾部分來介紹node

 


日誌文件的管理:linux

    一、logrotate 配置程序員

    二、缺省配置 logrotateapache

    三、使用include 選項讀取其餘配置文件安全

    四、使用include 選項覆蓋缺省配置ide

    五、爲指定的文件配置轉儲參數工具


1、logrotate 配置post


logrotate 程序是一個日誌文件管理工具。用來把舊的日誌文件刪除,並建立新的日誌文件,咱們把它叫作「轉儲」。咱們能夠根據日誌文件的大小,也能夠根據其this

天數來轉儲,這個過程通常經過 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).


2、缺省配置 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 {

        monthly

        rotate 1

    }

    

    # system-specific logs may be configured here



缺省的配置通常放在logrotate.conf 文件的最開始處,影響整個系統。在本例中就是前面12行。


第三行weekly 指定全部的日誌文件每週轉儲一次。

第五行 rotate 4 指定轉儲文件的保留 4份。

第七行 errors root 指定錯誤信息發送給root。

第九行create 指定 logrotate 自動創建新的日誌文件,新的日誌文件具備和

原來的文件同樣的權限。

第11行 #compress 指定不壓縮轉儲文件,若是須要壓縮,去掉註釋就能夠了。


3、使用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 文件就能夠了。


4、使用include 選項覆蓋缺省配置


當 /etc/logrotate.conf 讀入文件時,include 指定的文件中的轉儲參數將覆蓋缺省的參數,以下例:


# linuxconf 的參數

    /var/log/htmlaccess.log

    { 

        errors jim

        notifempty

        nocompress

        weekly

        prerotate

        /usr/bin/chattr -a /var/log/htmlaccess.log

        endscript

        postrotate

        /usr/bin/chattr +a /var/log/htmlaccess.log

        endscript

    }

    /var/log/netconf.log

    { 

        nocompress

        monthly

    }


在這個例子中,當 /etc/logrotate.d/linuxconf 文件被讀入時,下面的參數將覆蓋/etc/logrotate.conf中缺省的參數。


Notifempty

errors jim


5、爲指定的文件配置轉儲參數

常常須要爲指定文件配置參數,一個常見的例子就是每個月轉儲/var/log/wtmp。爲特定文件而使用的參數格式是:


# 註釋

/full/path/to/file

{

    option(s)

}


下面的例子就是每個月轉儲 /var/log/wtmp 一次:

    #Use logrotate to rotate wtmp

    /var/log/wtmp

    {

        monthly

        rotate 1

    }


 


6、其餘須要注意的問題


一、儘管花括號的開頭能夠和其餘文本放在同一行上,可是結尾的花括號必須單獨成行。


二、使用 prerotate 和 postrotate 選項

下面的例子是典型的腳本 /etc/logrotate.d/syslog,這個腳本只是對

/var/log/messages 有效。


    /var/log/messages

    { 

        prerotate

        /usr/bin/chattr -a /var/log/messages

        endscript

        postrotate

        /usr/bin/kill -HUP syslogd

        /usr/bin/chattr +a /var/log/messages

        endscript

    }


第一行指定腳本對 /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 部分的腳本


三、logrotate 的運行分爲三步:


判斷系統的日誌文件,創建轉儲計劃以及參數,經過cron daemon 運行下面的代碼是 Red Hat Linux 缺省的crontab 來天天運行logrotate。


    #/etc/cron.daily/logrotate

    #! /bin/sh

    

    /usr/sbin/logrotate /etc/logrotate.conf


四、/var/log/messages 不能產生的緣由:

這種狀況不多見,可是若是你把/etc/services 中的 514/UDP 端口關掉的話,這個文件就不能產生了。


小結:本文經過對Red Hat 系統上典型的logrotate 配置例子的介紹,詳細說明了logrotate 程序的應用方法。但願對全部Linux 系統管理員有所幫助。管理好,分析好日誌文件是系統安全的第一步

 

參考文章:http://blog.csdn.net/cjwid/article/details/1690101

相關文章
相關標籤/搜索