LoginSignup
3
8

More than 5 years have passed since last update.

CentOS7 ログ管理(systemd-journald.service)の設定

Last updated at Posted at 2016-05-29

概要

OS標準のjournaldの設定を変更し、ログを最適に管理する。

ログの永続化

標準だとjournaldのログは、/run/log/journal/machine-id/配下に保管される。
「/run」ディレクトリは「tmpfs」にマウントされているため、OSを再起動するとデータがクリアされてログファイルが無くなってしまうので、永続的に保管されるように設定を変更する。

なお設定変更は「/var/log/journal」というディレクトリを作成して、サービスを再起動すればそこにログが保管されるようになる。

出力先を変更した場合にログが一般ユーザーで参照できなくなるので、権限を変更する。

/var/log/journalディレクトリの作成
sudo mkdir -p /var/log/journal
ls -ld /var/log/journal

<出力結果>
drwxr-xr-x 2 root root 6 10月  9 10:13 /var/log/journal
journaldの再起動と動作確認
sudo systemctl restart systemd-journald
systemctl status systemd-journald

<出力結果>
● systemd-journald.service - Journal Service
   Loaded: loaded (/usr/lib/systemd/system/systemd-journald.service; static; vendor preset: disabled)
   Active: active (running) since 月 2017-10-09 10:14:48 JST; 14s ago
     Docs: man:systemd-journald.service(8)
           man:journald.conf(5)
 Main PID: 14104 (systemd-journal)
   Status: "Processing requests..."
   CGroup: /system.slice/systemd-journald.service
           mq14104 /usr/lib/systemd/systemd-journald
ログの出力を確認
ls -l /var/log/journal/`cat /etc/machine-id`/system.journal

<出力結果>
-rw-r----- 1 root root 8388608 10月  9 10:14 /var/log/journal/9bbe3e59b2ed4c62884f1c3e33415d71/system.journal
ログの権限変更
sudo chmod 644 /var/log/journal/`cat /etc/machine-id`/system.journal
ls -l /var/log/journal/`cat /etc/machine-id`/system.journal

<出力結果>
-rw-r--r-- 1 root root 8388608 10月  9 10:18 /var/log/journal/9bbe3e59b2ed4c62884f1c3e33415d71/system.journal

ログファイルの最大サイズ設定

以下の設定ファイルを修正して、ログファイルの最大サイズを設定する。
初期設定(設定ファイル上では空欄)ではログを保存するディレクトリがマウントされているファイルシステムの10%までログを保存する設定となっている。

以下のコマンドで、事前に設定ファイルをバックアップする。

journald.confのバックアップ
sudo cp -piv /etc/systemd/journald.conf /etc/systemd/journald.conf.`date "+%Y%m%d"`

<出力結果>
`/etc/systemd/journald.conf' -> `/etc/systemd/journald.conf.YYYYMMDD'

以下の設定ファイルで、「SystemMaxUse」に保存するログのサイズを指定する。ここでは「2G」を指定する。

/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
#RateLimitInterval=30s
#RateLimitBurst=1000
### Setting begin
#SystemMaxUse=
SystemMaxUse=2G
### Setting end
#SystemKeepFree=
#SystemMaxFileSize=
#RuntimeMaxUse=
#RuntimeKeepFree=
#RuntimeMaxFileSize=
#MaxRetentionSec=
#MaxFileSec=1month
#ForwardToSyslog=yes
#ForwardToKMsg=no
#ForwardToConsole=no
#ForwardToWall=yes
#TTYPath=/dev/console
#MaxLevelStore=debug
#MaxLevelSyslog=debug
#MaxLevelKMsg=notice
#MaxLevelConsole=info
#MaxLevelWall=emerg

journaldを再起動し、設定を確認する。

journaldの再起動と動作確認
sudo systemctl restart systemd-journald
systemctl status systemd-journald

<出力結果>
● systemd-journald.service - Journal Service
   Loaded: loaded (/usr/lib/systemd/system/systemd-journald.service; static; vendor preset: disabled)
   Active: active (running) since 月 2017-10-09 10:38:52 JST; 11s ago
     Docs: man:systemd-journald.service(8)
           man:journald.conf(5)
 Main PID: 14177 (systemd-journal)
   Status: "Processing requests..."
   CGroup: /system.slice/systemd-journald.service
           mq14177 /usr/lib/systemd/systemd-journald
ログサイズの変更を確認
sudo journalctl -u systemd-journald -l

<出力結果>
(中略)
10月 09 10:38:52 hostname.domainname systemd-journal[14177]:Permanent journal is using 16.0M (max allowed 2.0G, trying to leave 4.0G free of 26.7G available → current limit 2.0G).
(中略)

次の手順

CentOS7 ネットワーク設定

以上

3
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
8