Turbolinux、Red Hat Linux、Vine Linuxなどでは、syslogをはじめとする各種logファイルの1週間分のログを1ファイルとして、過去4週間分保存している。
$ ls -l /var/log/messages* -rw------- 1 root root 1234 feb 6 15:33 /var/log/messages -rw------- 1 root root 4567 feb 3 22:16 /var/log/messages.1 -rw------- 1 root root 9012 jun 27 04:02 /var/log/messages.2 -rw------- 1 root root 3456 jun 20 04:02 /var/log/messages.3 -rw------- 1 root root 7890 jun 13 04:02 /var/log/messages.4 |
この作業は、logrotateというツールで自動的に行われるため(cronで1日1回logrotateコマンドが実行される。設定は/etc/cron.daily/logrotate)、ユーザーが何かのアクションを行う必要はない。
logrotate は、「cron.daily」に入っている。
4週間分のログでは足りない場合や、ディスクスペースの関係で4週間もログを保存しておけない場合は、保存期間などを変更することができる。
logrotateの設定ファイルは、/etc/logrotate.conf
/etc/logrotate.dディレクトリ内に、サービスごとの設定ファイルがある。
/etc/logrotate.conf 1例
# see "man logrotate" for details # rotate log files weekly weekly ←ローテートは週ごと
# keep 4 weeks worth of backlogs rotate 4 ←4回分のログを保存
# create new (empty) log files after rotating old ones create
# 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 -- we'll rotate them here /var/log/wtmp { monthly create 0664 root utmp rotate 1 }
# system-specific logs may be also be configured here. |
| /etc/logrotate.conf |
もし、1年分(53週)のログを保存するのであれば、/etc/logrotate.confの「rotate」の行を変更する。
# keep 4 weeks worth of backlogs rotate 53 |
「compress」オプションを指定すると(コメントを外すと)、過去のログがgzipで圧縮される。
また、特定のログだけは1年分保存して、ほかのログはデフォルトのまま4週間分保存するといった設定を行うこともできる。それには、/etc/logrotate.dディレクトリ内にある、サービスごとの設定ファイルを変更する。例えば、syslogだけを53週分保存する場合は、/etc/logrotate.confはそのままにしておいて、/etc/logrotate.d/syslogを変更する。
/var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron { rotate 53 ←この行を追加 sharedscripts postrotate /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || t rue endscript } |
/etc/logrotate.conf
参考例 ------------------------------------------------------------------------------- |
# 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
# 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
--------------------------------------------------------------------