LPIC あずき本v5.0 必須システムサービス 10.1-10.2

LPICLinux勉強メモ

出典: 

システムクロックの設定

  • システムの内蔵時計の調節の重要性

    • ログの時刻
    • メールの時刻
    • 【補】プロトコルがちゃんと動かなかったりする

システムクロックとハードウェアクロック

  • ハードウェアクロック

    • ハードウェアなやつ
    • 電源がオフでも動く(電池駆動)
  • システムクロック

    • Linuxカーネル内に存在
    • Linux起動時にハードウェアクロックと同期
    • 以降、別々に動き続ける
    • ので、時間経過につれズレていく

dateコマンド

表示

  • システムクロックを参照して現在日時表示
date
2019年  4月 13日 土曜日 19:12:05 JST
  • フォーマット指定して表示
date "+%Y/%m/%d (%a)"
2019/04/13 (土)
書式 説明
%Y
%m 月(01-12)
%d 日(01-31)
%H 時(00-23)
%M 分(00-59)
%a 曜日
%b 月名
  • 【補】%mと%Mに注意(設定と逆)

設定

  • 時刻の変更も可能(root)
date --help
使用法: date [OPTION]... [+FORMAT]
または: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
  • これ
date [MMDDhhmm[[CC]YY][.ss]]
  • DD

  • CC

    • 西暦上2桁
  • YY

    • 西暦下2桁
  • ss

hwclockコマンド

  • ハードウェアクロックの参照
  • システムクロックをハードウェアクロックにセット
hwclock
hwclock: 既知のいずれの方法を使用しても、ハードウェアの時計にアクセスすることができません。
hwclock: --debug オプションを使用すると、アクセス方法の検索処理を詳しく確認することができます。
hwclock --debug
hwclock from util-linux 2.31.1
System Time: 1555151116.037458
Trying to open: /dev/rtc0
利用可能なクロックインターフェイスが見つかりません。
hwclock: 既知のいずれの方法を使用しても、ハードウェアの時計にアクセスすることができません。
  • RTC: Real Time Clock
  • 管理者でないと開けないっぽい
sudo hwclock --debug
hwclock from util-linux 2.31.1
System Time: 1555151215.492431
Trying to open: /dev/rtc0
Using the rtc interface to the clock.
ハードウェアの時刻が UTC に設定されているものと仮定します。
クロックティックを待っています...
...クロックティックを取得しました
ハードウェアの時計から読み込んだ時刻: 2019/04/13 10:26:56
ハードウェアの時刻値 : 2019/04/13 10:26:56 = 1969 年から 1555151216 秒経過
Time since last adjustment is 1555151216 seconds
Calculated Hardware Clock drift is 0.000000 seconds
2019-04-13 19:26:55.467952+0900

-r, --show

 -r, --show           display the RTC time
  • ハードウェアクロックを表示する
sudo hwclock --show
2019-04-13 19:27:45.827436+0900

-w, --systohc

 -w, --systohc        set the RTC from the system time
  • システムクロックの時刻をハードウェアクロックに設定する

    • system to hardware clock

-s, --hctosys

 -s, --hctosys        set the system time from the RTC
  • ハードウェアクロックの時刻をシステムクロックに設定する

    • hardware clock to system

timedatectlコマンド

  • systemd採用ディストリで利用可能
  • 日付、時刻、タイムゾーンを管理
timedatectl --help
timedatectl [OPTIONS...] COMMAND ...

Query or change system time and date settings.

...

Commands:
  status                   Show current time settings
  set-time TIME            Set system time
  set-timezone ZONE        Set system time zone
  list-timezones           Show known time zones
  set-local-rtc BOOL       Control whether RTC is in local time
  set-ntp BOOL             Enable or disable network time synchronization
  • サブコマンドなし: statusサブコマンドが実行される

status

  status                   Show current time settings
  • 現在の状態を表示する
timedatectl status
                      Local time: 土 2019-04-13 19:33:16 JST
                  Universal time: 土 2019-04-13 10:33:16 UTC
                        RTC time: 土 2019-04-13 10:33:16
                       Time zone: Asia/Tokyo (JST, +0900)
       System clock synchronized: yes
systemd-timesyncd.service active: yes
                 RTC in local TZ: no
  • systemd-timesyncd.service activeがNTPを使うかどうか

set-ntp <"yes"|"no">

  set-ntp BOOL             Enable or disable network time synchronization
  • NTPを使うかどうか
sudo timedatectl set-ntp no
         System clock synchronized: yes
- systemd-timesyncd.service active: yes
+ systemd-timesyncd.service active: no
                   RTC in local TZ: no
sudo timedatectl set-ntp yes
         System clock synchronized: yes
- systemd-timesyncd.service active: no
+ systemd-timesyncd.service active: yes
                   RTC in local TZ: no

set-time <時刻|日付|日付 時刻>

  set-time TIME            Set system time
  • 時刻、日付、または日付と時刻を設定する
  • システムクロックとハードウェアクロックの両方を同時に設定
  • NTPサービスが有効だとエラー
sudo timedatectl set-time 2019-01-01
Failed to set time: Automatic time synchronization is enabled
  • NTPサービス無効化してからためすと時刻かわる
sudo timedatectl set-ntp no
         System clock synchronized: yes
- systemd-timesyncd.service active: yes
+ systemd-timesyncd.service active: no
                   RTC in local TZ: no
sudo timedatectl set-time 2019-01-01
date
2019年  1月  1日 火曜日 00:00:02 JST
  • もどす
sudo timedatectl set-ntp yes
date
timedatectl
2019年  4月 13日 土曜日 19:42:45 JST
         System clock synchronized: yes
- systemd-timesyncd.service active: no
+ systemd-timesyncd.service active: yes
                   RTC in local TZ: no

list-timezones

  list-timezones           Show known time zones
  • タイムゾーンを一覧表示する
timedatectl list-timezones
  • いっぱいある
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
...

set-timezone タイムゾーン

  set-timezone ZONE        Set system time zone
  • タイムゾーンを設定する
sudo timedatectl set-timezone UTC
timedatectl
                      Local time: 土 2019-04-13 10:48:43 UTC
                  Universal time: 土 2019-04-13 10:48:43 UTC
                        RTC time: 月 2018-12-31 15:06:24
                       Time zone: UTC (UTC, +0000)
       System clock synchronized: yes
systemd-timesyncd.service active: yes
                 RTC in local TZ: no
  • /etc/localtimeのシンボリックリンクがすげ変わっている
  • /etc/timezoneが書き換わっている
ls -l /etc/localtime
cat /etc/timezone
lrwxrwxrwx 1 root root 25  4月 13 10:48 /etc/localtime -> ../usr/share/zoneinfo/UTC

UTC
  • もどす
sudo timedatectl set-timezone Asia/Tokyo
timedatectl
                      Local time: 土 2019-04-13 19:50:33 JST
                  Universal time: 土 2019-04-13 10:50:33 UTC
                        RTC time: 月 2018-12-31 15:08:13
                       Time zone: Asia/Tokyo (JST, +0900)
       System clock synchronized: yes
systemd-timesyncd.service active: yes
                 RTC in local TZ: no
ls -l /etc/localtime
cat /etc/timezone
lrwxrwxrwx 1 root root 32  4月 13 19:50 /etc/localtime -> ../usr/share/zoneinfo/Asia/Tokyo

Asia/Tokyo

NTPによる時刻設定

  • ハードウェアクロックもシステムクロックもあまり正確ではない
  • 正確なものに合わせる必要がある
  • NTP: Network Time Protocol

    • ネットワーク経由でNTPサーバから正確な時刻を取得・クロックを同期
  • NTPネットワークは階層構造

    • 最上位

      • 極めて正確

        • 原子時計
        • GPS
    • Stratum 1
    • Stratum 2
    • NTPクライアント
  • ntpdateコマンド

    • NTPサーバから正確な時刻取得
    • 手元のUbuntuには入っていなかった
sudo apt install -y ntpdate

ntpdateコマンド

man ntpdate
ntpdate(8)       System Manager's Manual       ntpdate(8)

NAME
       ntpdate - set the date and time via NTP

SYNOPSIS
       ntpdate  [-bBdoqsuv]  [-a  key] [-e authdelay] [-k
       keyfile] [-o version] [-p  samples]  [-t  timeout]
       server [...]

DESCRIPTION
...
  • manのsection 8: システム管理コマンドとデーモン
  • NTPサーバtime.server.lpic.jpから現在取得する例
ntpdate time.server.lpic.jp

NTPサーバの運用

  • ntpd
  • NTPサーバを自前で運用

    • 組織内にNTPクライアントが多いなら用意したほうが良い
    • 特定のNTPサーバに負荷が集中しないようにしよう

      • DNSに問い合わせるといくつかのNTPサーバから1つのIPアドレスがランダムに返るようにするとか
  • サーバ起動

    • SysVinit

      • sudo /etc/init.d/ntpd start
    • Systemd

      • sudo systemctl start ntpd.service
  • ntpq

    • NTPサーバの状態を照会
  • /etc/ntp.conf

    • NTPサーバの設定

      • server

        • NTPサーバの名前
      • driftfile

        • 補正情報ファイルの場所
        • /etc/ntp.driftとか
      • logfile

        • ログファイルの場所
        • /var/log/ntp.logとか
  • /etc/ntp.drift

    • 補正情報の保存先

      • クロックの誤差を予測した数値
    • ディストリによって/var/lib/ntp.driftとか/var/lib/ntp/ntp.driftとか
  • 雑にdockerのcentosコンテナで入れたやつ
cat /etc/ntp.conf
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile /var/lib/ntp/drift

# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery

# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1 
restrict ::1

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst

#broadcast 192.168.1.255 autokey	# broadcast server
#broadcastclient			# broadcast client
#broadcast 224.0.1.1 autokey		# multicast server
#multicastclient 224.0.1.1		# multicast client
#manycastserver 239.255.254.254		# manycast server
#manycastclient 239.255.254.254 autokey # manycast client

# Enable public key cryptography.
#crypto

includefile /etc/ntp/crypto/pw

# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography. 
keys /etc/ntp/keys

# Specify the key identifiers which are trusted.
#trustedkey 4 8 42

# Specify the key identifier to use with the ntpdc utility.
#requestkey 8

# Specify the key identifier to use with the ntpq utility.
#controlkey 8

# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats

# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor

Chrony

  • ntpd/ntpdateの代替

    • ntpパッケージと同時には使えない
  • chronydデーモン
  • chronycクライアント
  • 設定ファイル/etc/chrony.conf

    • server

      • NTPサーバの名前
    • driftfile

      • 補正情報ファイルの場所
      • /var/lib/chrony/driftとか
    • rtcsync

      • ハードウェアクロックと同期させる
    • logdir

      • ログファイルを書き出すディレクトリの場所
      • /var/log/chronyとか
  • 雑にdockerのcentosコンテナで入れたやつ
cat /etc/chrony.conf
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst

# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *

# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2

# Allow NTP client access from local network.
#allow 192.168.0.0/16

# Serve time even if not synchronized to a time source.
#local stratum 10

# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys

# Specify directory for log files.
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking

chronycコマンド

  • chronydの管理を行う
chronyc --help
Usage: chronyc [-h HOST] [-p PORT] [-n] [-c] [-d] [-4|-6] [-m] [COMMAND]
chrony help
  • 主なサブコマンド

    System clock:               
    tracking                    Display system time information
    ...

Time sources:
sources [-v] Display information about current sources sourcestats [-v] Display statistics about collected measurements …

NTP sources:
activity Check how many NTP sources are online/offline …

Client commands:
exit|quit Leave the program help Generate this help

- 対話的操作
    - サブコマンドを入力できる

```sh
chronyc
chrony version 3.2
Copyright (C) 1997-2003, 2007, 2009-2017 Richard P. Curnow and others
chrony comes with ABSOLUTELY NO WARRANTY.  This is free software, and
you are welcome to redistribute it under certain conditions.  See the
GNU General Public License version 2 for details.

chronyc>

システムログの設定

  • ログ

    • コンピュータの動作状況の記録
  • rsyslog, syslog, syslog-ng

    • さまざまなイベントを…

      • ログファイルに記録
      • コンソールに表示

rsyslogの設定

/etc/rsyslog.conf

#  /etc/rsyslog.conf	Configuration file for rsyslog.
#
#			For more information see
#			/usr/share/doc/rsyslog-doc/html/rsyslog_conf.html
#
#  Default logging rules can be found in /etc/rsyslog.d/50-default.conf


#################
#### MODULES ####
#################

module(load="imuxsock") # provides support for local system logging
#module(load="immark")  # provides --MARK-- message capability

# provides UDP syslog reception
#module(load="imudp")
#input(type="imudp" port="514")

# provides TCP syslog reception
#module(load="imtcp")
#input(type="imtcp" port="514")

# provides kernel logging support and enable non-kernel klog messages
module(load="imklog" permitnonkernelfacility="on")

###########################
#### GLOBAL DIRECTIVES ####
###########################

#
# Use traditional timestamp format.
# To enable high precision timestamps, comment out the following line.
#
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat

# Filter duplicated messages
$RepeatedMsgReduction on

#
# Set the default permissions for all log files.
#
$FileOwner syslog
$FileGroup adm
$FileCreateMode 0640
$DirCreateMode 0755
$Umask 0022
$PrivDropToUser syslog
$PrivDropToGroup syslog

#
# Where to place spool and state files
#
$WorkDirectory /var/spool/rsyslog

#
# Include all config files in /etc/rsyslog.d/
#
$IncludeConfig /etc/rsyslog.d/*.conf
  • モジュール読み込んでるところ
module(load="imuxsock") # provides support for local system logging
#module(load="immark")  # provides --MARK-- message capability

# provides UDP syslog reception
#module(load="imudp")
#input(type="imudp" port="514")

# provides TCP syslog reception
#module(load="imtcp")
#input(type="imtcp" port="514")

# provides kernel logging support and enable non-kernel klog messages
module(load="imklog" permitnonkernelfacility="on")
  • デフォルトではimuxsockimklogだけ有効
  • rsyslogの主なプラグインモジュール
プラグインモジュール 説明
imuxsock UNIXソケットによるローカルロギングサポート(loggerコマンド等)
imjournal systemdのジャーナルサポート
imklog カーネルログのサポート
immark マークを出力(—MARK—)
imudp UDPでメッセージを受信
imtcp TCPでメッセージを受信
  • ファイル分割

    • /etc/rsyslog.d/*.confファイル群を読み込む
#
# Include all config files in /etc/rsyslog.d/
#
$IncludeConfig /etc/rsyslog.d/*.conf
  • ルール設定部分
  • /etc/rsyslog.d/50-default.conf
#  Default rules for rsyslog.
#
#			For more information see rsyslog.conf(5) and /etc/rsyslog.conf

#
# First some standard log files.  Log by facility.
#
auth,authpriv.*			/var/log/auth.log
*.*;auth,authpriv.none		-/var/log/syslog
#cron.*				/var/log/cron.log
#daemon.*			-/var/log/daemon.log
kern.*				-/var/log/kern.log
#lpr.*				-/var/log/lpr.log
mail.*				-/var/log/mail.log
#user.*				-/var/log/user.log
  • カーネルログは全プライオリティ/var/log/kern.logに出力する、等
  • <ファシリティ><"."|"="><プライオリティ> <出力先(アクションフィールド)>

ファシリティ

  • メッセージの生成元

    • カーネルや実行中のプロセス
ファシリティ 説明
auth, authpriv 認証システム(loginなど)
cron cron
daemon 各種デーモン
kern カーネル
lpr 印刷システム
mail メールサービス
user ユーザーアプリケーション
local0-local7 ローカルシステム

プライオリティ

  • メッセージの重要度
重要度(降順) 説明
emerg 緊急事態
alert 早急に対処が必要
crit 深刻(システムの処理は継続できる)
err 一般的なエラー
warning 一般的な警告
notice 一般的な通知
info 一般的な情報
debug 一般的な情報
none ログを記録しない

出力先(アクションフィールド)

アクションフィールド 説明
/var/log/messages 指定のログファイルに出力
-/var/log/messages メッセージをファイルに書き込むごとに同期しない
/dev/tty1 指定のデバイスに出力。この例はコンソール
@sv.example.com 指定のホストにUDPで出力
@@sv.example.com 指定のホストにTCPで出力
violet 指定のユーザーの端末に出力
* ログイン中の全てのユーザーの端末に出力

/etc/rsyslog.confの設定例

  • <ファシリティ>.<プライオリティ> <出力先(アクションフィールド)>

    • 指定のプライオリティ以上のものが出力される
  • <ファシリティ>=<プライオリティ> <出力先(アクションフィールド)>

    • 指定のプライオリティのものが出力される
auth,authpriv.*			/var/log/auth.log
*.*;auth,authpriv.none		-/var/log/syslog
  • ファシリティauth,authprivのメッセージをプライオリティによらずすべて/var/log/auth.logに出力
  • auth,authpriv以外全ファシリティのメッセージをプライオリティによらずすべて/var/log/asyslogに出力
  • 設定反映
sudo systemctl restart rsyslog.service

loggerコマンド

  • ログメッセージ生成
  • シスログの設定の確認に有用
logger -p syslog.info Test "logger test message"
  • ルール*.*;auth,authpriv.none -/var/log/syslogに従って、
    /var/log/syslogに追記される

/var/log/syslog

...
+ Apr 13 21:53:06 wand-ThinkPad-X240s Test: logger test message

systemd-catコマンド

  • コマンドの実行結果をジャーナルに書き込む
systemd-cat echo 'hoge'

/var/log/syslog

...
+ Apr 13 21:53:52 wand-ThinkPad-X240s [2686]: hoge

ログの調査

  • 書籍的には主要なログファイルは/var/log/messagesということになっている
  • 手元のUbuntuでは/var/log/syslogが主要っぽい

    • Debianもそうみたい
  • tail -fによる監視
  • grepによる絞り込み
  • /var/log/secure

    • 認証などセキュリティ関連のログの保存先
    • 手元のUbuntuでは/var/log/auth.log
  • ログインユーザ関連
コマンド 対応するファイル(バイナリ) 説明
who /var/run/utmp ログイン中のユーザ名、ログイン端末名、ログイン日時、ログイン元IP
w /var/run/utmp 上記 + システム情報も
last /var/log/wtmp 最近ログインしたユーザの一覧
lastlog /var/log/lastlog ユーザごとに最近のログイン一覧
who
wand     :0           2019-04-12 16:53 (:0)
w
 22:04:27 up 1 day,  5:10,  1 user,  load average: 2.14, 1.85, 1.89
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
wand     :0       :0               金16   ?xdm?   2:58m  0.00s /usr/lib/gdm
last
wand     :0           :0               Fri Apr 12 16:53   still logged in
reboot   system boot  4.15.0-47-generi Fri Apr 12 16:53   still running
wand     :0           :0               Fri Apr 12 16:45 - down   (00:07)
reboot   system boot  4.15.0-47-generi Fri Apr 12 16:45 - 16:53  (00:08)
wand     :0           :0               Mon Apr  1 19:19 - 16:44 (10+21:25)
reboot   system boot  4.15.0-46-generi Mon Apr  1 19:19 - 16:44 (10+21:25)
wand     :0           :0               Mon Apr  1 12:59 - down   (05:24)
reboot   system boot  4.15.0-46-generi Mon Apr  1 12:58 - 18:23  (05:24)
wand     :0           :0               Mon Apr  1 12:53 - down   (00:03)
reboot   system boot  4.15.0-46-generi Mon Apr  1 12:52 - 12:56  (00:04)

wtmp begins Mon Apr  1 12:51:09 2019
lastlog
ユーザ名         ポート   場所             最近のログイン
root                                       **一度もログインしていません**
daemon                                     **一度もログインしていません**
...
wand             tty2                      日  2月 24 02:59:20 +0900 2019
...

journalctlコマンド

  • systemd採用システムにおいて、systemdのログ(ジャーナル)を閲覧

    • systemd-journald.service
systemctl is-enabled systemd-journald.service
  • disableできないやつ
static
  • ユニット絞り込み
journalctl -u atd
-- Logs begin at Tue 2019-01-01 00:00:00 JST, end at Sat 2019-04-13 22:09:14 JST. --
 4月 12 20:34:57 wand-ThinkPad-X240s systemd[1]: Started Deferred execution scheduler.
  • ジャーナルは/var/log/journal/var/run/log/journalのバイナリに保存されている
  • オプション
オプション 説明
-f, —follow 末尾を表示し続ける(tail -fとかとおなじ)
-r, —reverse 新しい順(デフォルトは古い順。その逆)
-e, —pager-end 末尾を表示する(ページャでさかのぼれる)
-x, —catalog 説明文付き
-k, —dmesg カーネルメッセージのみ
-b, —boot ブート時メッセージ
-p, —priority=プライオリティ 指定したプライオリティ以上
-u, —unit=ユニット名 ユニット指定
—full エスケープ文字を除いてプレーンテキストで出力
—no-pager 1ページごとに表示せず、すべてのログを出力する
  • 設定ファイル/etc/systemd/journald.conf
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See journald.conf(5) for details.

[Journal]
#Storage=auto
#Compress=yes
#Seal=yes
#SplitMode=uid
#SyncIntervalSec=5m
#RateLimitIntervalSec=30s
#RateLimitBurst=1000
#SystemMaxUse=
#SystemKeepFree=
#SystemMaxFileSize=
#SystemMaxFiles=100
#RuntimeMaxUse=
#RuntimeKeepFree=
#RuntimeMaxFileSize=
#RuntimeMaxFiles=100
#MaxRetentionSec=
#MaxFileSec=1month
#ForwardToSyslog=yes
#ForwardToKMsg=no
#ForwardToConsole=no
#ForwardToWall=yes
#TTYPath=/dev/console
#MaxLevelStore=debug
#MaxLevelSyslog=debug
#MaxLevelKMsg=notice
#MaxLevelConsole=info
#MaxLevelWall=emerg
#LineMax=48K
  • ジャーナルを永続化するかどうか(例: Storage=persistent)
  • ログファイルの最大ファイルサイズ(例: SystemMaxUse=10G)
  • 設定の反映
sudo systemctl restart systemd-journald

ログファイルのローテーション

  • 放置すると肥大化する一方
  • ローテーション機能

    • 古くなったログを切り分ける
    • ところてん式にリネーム

      • messages.1 -> messages.2
      • messages -> messages.1
      • messages作成
    • 一番古いの消す
  • logrotateユーティリティ

    • cronで定期実行される
  • /etc/logrotate.conf設定ファイル
# see "man logrotate" for details
# rotate log files weekly
weekly

# use the syslog group by default, since this is the owning group
# of /var/log/syslog.
su root syslog

# keep 4 weeks worth of backlogs
rotate 4

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

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

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

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

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

# system-specific logs may be configured here
  • /etc/logrotate.d/rsyslogがインクルードされている
/var/log/syslog
{
	rotate 7
	daily
	missingok
	notifempty
	delaycompress
	compress
	postrotate
		/usr/lib/rsyslog/rsyslog-rotate
	endscript
}

/var/log/mail.info
/var/log/mail.warn
/var/log/mail.err
/var/log/mail.log
/var/log/daemon.log
/var/log/kern.log
/var/log/auth.log
/var/log/user.log
/var/log/lpr.log
/var/log/cron.log
/var/log/debug
/var/log/messages
{
	rotate 4
	weekly
	missingok
	notifempty
	compress
	delaycompress
	sharedscripts
	postrotate
		/usr/lib/rsyslog/rsyslog-rotate
	endscript
}
  • /var/log/syslogrotate 7compress
ls -l /var/log/syslog*
-rw-r-----   1 syslog         adm       2229945 2019-04-13 22:28 /var/log/syslog
-rw-r-----   1 syslog         adm      18567139 2019-04-13 02:06 /var/log/syslog.1
-rw-r-----   1 syslog         adm         12840 2019-04-12 00:40 /var/log/syslog.2.gz
-rw-r-----   1 syslog         adm        361565 2019-04-11 15:09 /var/log/syslog.3.gz
-rw-r-----   1 syslog         adm        189717 2019-04-09 02:18 /var/log/syslog.4.gz
-rw-r-----   1 syslog         adm        540315 2019-04-08 01:09 /var/log/syslog.5.gz
-rw-r-----   1 syslog         adm         57534 2019-04-06 18:09 /var/log/syslog.6.gz
-rw-r-----   1 syslog         adm       1963535 2019-04-04 17:05 /var/log/syslog.7.gz
  • 確かに7まで保存され、gzip圧縮されている