TokoroTech

勉強した技術的なこと等まとめていってます!

【構築手順】RHEL9のrsyslogのログにプライオリティ(facility.severity)を明示的に付与

▼目次

▼目標(ゴール)

RHEL9のrsyslogのログにプライオリティ(facility.severity)を明示的に付与するようにrsyslogを設定します。 rsyslogが設定通りにログ出力しているかまで確認します。

▼用語

※※ 執筆中。。※※

▼システム要件

構成

サーバ構成

  • ①自宅デスクトップPC
  • ②vSphere ESXi 8サーバ
  • ③RHEL9(Red Hat Enterprise Linux 9)サーバ

構成図

※※ 執筆中。。※※

導入ソフトウェア

①自宅デスクトップPC

ソフトウェア バージョン 備考
Windows11 Windows 11 Home 24H2 -
VMware(R) Workstation 17 Pro 17.6.2 build-24409262 -

②vSphere ESXi 8サーバ

ソフトウェア バージョン 備考
vSphere ESXi 8 ESXi-8.0U2b-23305546-standard -

③RHEL9サーバ

ソフトウェア バージョン 備考
RHEL9 Red Hat Enterprise Linux release 9.5 (Plow) -

必要スペック(最小)

※あくまでも起動するために必要最低限のスペックであり、必要スペックは場合によって変わります。(自宅デスクトップPCは使用しているPCのスペックを記載しています。)

①自宅デスクトップPC

コンポーネント CPU RAM Root Disk Data Disk
自宅デスクトップPC AMD Ryzen 7 3700X 8-Core Processor (3.60 GHz) 64.0GB 500GB 4TB (SSD)

②vSphere ESXi 8サーバ

コンポーネント CPU RAM Root Disk Data Disk
vSphere ESXi 8 2コア 8GB以上 32GB以上 ※任意

③RHEL9サーバ

コンポーネント CPU RAM Root Disk Data Disk
RHEL9 ※確認できず1コアとします 1.5 GiB 10GB ※任意

スペック参考資料

各種パラメータ設定

※ (★)はデフォルト値になります。

①自宅デスクトップPC

ホスト名 IPv4/プレフィックス長 デフォルトGW DNS NTP プロキシ
(★)
-
192.168.1.50/24 192.168.1.30 8.8.8.8 (★)
time.windows.com
(★)
-

②vSphere ESXi 8サーバ

ホスト名 IPv4/プレフィックス長 デフォルトGW DNS NTP プロキシ
esxi01 192.168.1.100/24 192.168.1.30 8.8.8.8 ntp.nict.jp
ntp.jst.mfeed.ad.jp
(★)
-

③RHEL9サーバ

ホスト名 IPv4/プレフィックス長 デフォルトGW DNS NTP プロキシ
rhel9 192.168.1.110/24 192.168.1.30 8.8.8.8 (★)
※以下参照
(★)
-
[tokoro10nu@rhel9 ~]$ chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* mail1.marinecat.net           2   7   377    10   -446us[ -514us] +/- 4922us
^- x.ns.gin.ntt.net              2   6   377    11  +2462us[+2394us] +/-   73ms
^+ pred-374.chl.la               3   7   377    13    -71us[ -139us] +/- 5128us
^- time.cloudflare.com           3   7   377    15  -2131us[-2199us] +/-   60ms

ファイアウォール設定

変更なし

▼構築手順

RHEL9の構築

  1. 以下過去記事を参照し、RHEL9の構築をします。 tokoro10.net

RHEL9のログフォーマット変更

rsyslog.confのデフォルト設定の確認

  1. 以下コマンドを実行し、rsyslog.confのデフォルト設定を確認します。
    [root@rhel9 rsyslog.d]# less /etc/rsyslog.conf
    
    ~~(中略)~~
    #### RULES ####
    
    # Log all kernel messages to the console.
    # Logging much else clutters up the screen.
    #kern.*                                                 /dev/console
    
    # Log anything (except mail) of level info or higher.
    # Don't log private authentication messages!
    *.info;mail.none;authpriv.none;cron.none                /var/log/messages
    
    # The authpriv file has restricted access.
    authpriv.*                                              /var/log/secure
    
    # Log all the mail messages in one place.
    mail.*                                                  -/var/log/maillog
    
    
    # Log cron stuff
    cron.*                                                  /var/log/cron
    
    # Everybody gets emergency messages
    *.emerg                                                 :omusrmsg:*
    
    # Save news errors of level crit and higher in a special file.
    uucp,news.crit                                          /var/log/spooler
    
    # Save boot messages also to boot.log
    local7.*                                                /var/log/boot.log
    ~~(中略)~~
    

デフォルト設定のログ出力の確認

rsyslog.confのデフォルト設定されているログファイルがどのような出力になっているのか確認します。

  1. 以下コマンドを実行し、/var/log/messagesのデフォルト出力を確認します。
    [root@rhel9 rsyslog.d]# less /var/log/messages
    
    ~~(中略)~~
    Oct 25 12:23:28 localhost dnf[3437]: Red Hat Enterprise Linux 9 for x86_64 - AppStre  10 MB/s |  71 MB     00:06
    Oct 25 12:23:35 localhost su[3480]: (to root) tokoro10nu on pts/1
    Oct 25 12:23:40 localhost dnf[3437]: メタデータの期限切れの最終確認: 0:00:08 前の 2025年10月25日 12時23分24秒 に実施しました。
    Oct 25 12:23:41 localhost dnf[3437]: メタデータキャッシュを作成しました。
    Oct 25 12:23:42 localhost systemd[1]: dnf-makecache.service: Deactivated successfully.
    Oct 25 12:23:42 localhost systemd[1]: Finished dnf makecache.
    Oct 25 12:23:42 localhost systemd[1]: dnf-makecache.service: Consumed 26.504s CPU time.
    Oct 25 12:23:47 localhost systemd[1]: systemd-hostnamed.service: Deactivated successfully.
    Oct 25 12:27:35 localhost systemd[1]: Starting Cleanup of Temporary Directories...
    Oct 25 12:27:35 localhost systemd[1]: systemd-tmpfiles-clean.service: Deactivated successfully.
    Oct 25 12:27:35 localhost systemd[1]: Finished Cleanup of Temporary Directories.
    Oct 25 12:27:35 localhost systemd[1]: run-credentials-systemd\x2dtmpfiles\x2dclean.service.mount: Deactivated successfully.
    Oct 25 12:50:22 localhost gsd-color[2571]: unable to get EDID for xrandr-Virtual-1: unable to get EDID for output
    
  2. 以下コマンドを実行し、/var/log/secureのデフォルト出力を確認します。
    [root@rhel9 rsyslog.d]# less /var/log/secure
    
    ~~(中略)~~
    Oct 25 12:22:57 localhost sshd[3434]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.1.50  user=root
    Oct 25 12:22:59 localhost sshd[3434]: Failed password for root from 192.168.1.50 port 60581 ssh2
    Oct 25 12:23:03 localhost sshd[3434]: Received disconnect from 192.168.1.50 port 60581:11: authentication cancelled [preauth]
    Oct 25 12:23:03 localhost sshd[3434]: Disconnected from authenticating user root 192.168.1.50 port 60581 [preauth]
    Oct 25 12:23:17 localhost sshd[3442]: Accepted password for tokoro10nu from 192.168.1.50 port 60582 ssh2
    Oct 25 12:23:17 localhost sshd[3442]: pam_unix(sshd:session): session opened for user tokoro10nu(uid=1000) by tokoro10nu(uid=0)
    Oct 25 12:23:26 localhost sudo[3474]: pam_unix(sudo:auth): conversation failed
    Oct 25 12:23:26 localhost sudo[3474]: pam_unix(sudo:auth): auth could not identify password for [tokoro10nu]
    Oct 25 12:23:35 localhost su[3480]: pam_unix(su:session): session opened for user root(uid=0) by tokoro10nu(uid=1000)
    
  3. 以下コマンドを実行し、/var/log/maillogのデフォルト出力を確認します。 ですが、デフォルトではmaillogファイルが存在しません。
    [root@rhel9 rsyslog.d]# less /var/log/maillog
    /var/log/maillog: そのようなファイルやディレクトリはありません
    
  4. 以下コマンドを実行し、/var/log/cronのデフォルト出力を確認します。
    [root@rhel9 rsyslog.d]# less /var/log/cron
    
    Oct 25 12:12:08 localhost crond[1071]: (CRON) STARTUP (1.5.7)
    Oct 25 12:12:08 localhost crond[1071]: (CRON) INFO (Syslog will be used instead of sendmail.)
    Oct 25 12:12:08 localhost crond[1071]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 90% if used.)
    Oct 25 12:12:08 localhost crond[1071]: (CRON) INFO (running with inotify support)
    

rsyslogの設定変更

  1. 以下コマンドを実行し、rsyslog.confの以下設定を確認します。/etc/rsyslog.d/*の設定ファイルを読み込むように設定されていることが分かります。
    [root@rhel9 rsyslog.d]# less /etc/rsyslog.conf
    
    ~~(中略)~~
    # Include all config files in /etc/rsyslog.d/
    include(file="/etc/rsyslog.d/*.conf" mode="optional")
    ~~(中略)~~
    
  2. テンプレート設定用のコンフィグファイルを作成します。 ※ファイル名の先頭の「10」は読み込み順序の意味合い
    [root@rhel9 rsyslog.d]# vi /etc/rsyslog.d/10-template.conf
    
    $template PrintWithPriority,"<%pri-text%>%timestamp% %HOSTNAME% %syslogtag%%msg%\n"
    
  3. 以下コマンドを実行し、rsyslog.confでログファイルとテンプレートを紐づける設定を入れます。
    [root@rhel9 rsyslog.d]# cp /etc/rsyslog.conf /etc/rsyslog.conf.bak
    [root@rhel9 rsyslog.d]# vi /etc/rsyslog.conf
    
    ~~(中略)~~
    #### RULES ####
    
    # Log all kernel messages to the console.
    # Logging much else clutters up the screen.
    #kern.*                                                 /dev/console
    
    # Log anything (except mail) of level info or higher.
    # Don't log private authentication messages!
    *.info;mail.none;authpriv.none;cron.none                /var/log/messages;PrintWithPriority
    
    # The authpriv file has restricted access.
    authpriv.*                                              /var/log/secure;PrintWithPriority
    
    # Log all the mail messages in one place.
    mail.*                                                  -/var/log/maillog;PrintWithPriority
    
    
    # Log cron stuff
    cron.*                                                  /var/log/cron;PrintWithPriority
    
    # Everybody gets emergency messages
    *.emerg                                                 :omusrmsg:*
    
    # Save news errors of level crit and higher in a special file.
    uucp,news.crit                                          /var/log/spooler
    
    # Save boot messages also to boot.log
    local7.*                                                /var/log/boot.log
    ~~(中略)~~
    
  4. テンプレートの設定完了後、以下コマンドを実行しrsyslogサービスを再起動します。
    [root@rhel9 rsyslog.d]# systemctl restart rsyslog.service
    

rsyslogの設定変更後の動作確認

  1. 以下コマンドを実行し、ログ(ここではmessagesファイル)を改めて確認してみます。
    [root@rhel9 rsyslog.d]# tail /var/log/messages
    
    ~~(中略)~~
    Oct 25 13:01:05 localhost rsyslogd[3704]:[origin software="rsyslogd" swVersion="8.2310.0-4.el9" x-pid="3704" x-info="https://www.rsyslog.com"] start
    Oct 25 13:01:05 localhost rsyslogd[3704]:imjournal: journal files changed, reloading...  [v8.2310.0-4.el9 try https://www.rsyslog.com/e/0 ]
    Oct 25 13:01:23 localhost systemd[1]:Stopping System Logging Service...
    Oct 25 13:01:23 localhost rsyslogd[3704]:[origin software="rsyslogd" swVersion="8.2310.0-4.el9" x-pid="3704" x-info="https://www.rsyslog.com"] exiting on signal 15.
    Oct 25 13:01:23 localhost systemd[1]:rsyslog.service: Deactivated successfully.
    Oct 25 13:01:23 localhost systemd[1]:Stopped System Logging Service.
    Oct 25 13:01:23 localhost systemd[1]:Starting System Logging Service...
    Oct 25 13:01:23 localhost rsyslogd[3711]:[origin software="rsyslogd" swVersion="8.2310.0-4.el9" x-pid="3711" x-info="https://www.rsyslog.com"] start
    Oct 25 13:01:23 localhost systemd[1]:Started System Logging Service.
    Oct 25 13:01:23 localhost rsyslogd[3711]:imjournal: journal files changed, reloading...  [v8.2310.0-4.el9 try https://www.rsyslog.com/e/0 ]
    
  2. loggerコマンドを使って/var/log/secureファイルに対してログを出力してみます。
    [root@rhel9 ~]# logger -p authpriv.info "info log"
    [root@rhel9 ~]# logger -p authpriv.debug "debug log"
    [root@rhel9 ~]# logger -p authpriv.notice "notice log"
    [root@rhel9 ~]# logger -p authpriv.warning "warning log"
    [root@rhel9 ~]# logger -p authpriv.err "err log"
    [tokoro10nu@rhel9 ~]$ logger -p authpriv.crit "crit log"
    [tokoro10nu@rhel9 ~]$ logger -p authpriv.alert "alert log"
    [tokoro10nu@rhel9 ~]$ logger -p authpriv.emerg "emerg log"
    
  3. loggerコマンドで出力したログを確認してみます。ログの行頭にプライオリティが表示されていれば設定できていることが確認できます。
    [root@rhel9 ~]# tail /var/log/secure
    
    ~~(中略)~~
    Oct 25 13:09:02 localhost root[3730]:info log
    Oct 25 13:09:29 localhost root[3732]:debug log
    Oct 25 13:09:41 localhost root[3733]:notice log
    Oct 25 13:09:53 localhost root[3734]:warning log
    Oct 25 13:10:03 localhost root[3735]:err log
    Oct 25 13:12:29 localhost tokoro10nu[3828]:crit log
    Oct 25 13:12:42 localhost tokoro10nu[3829]:alert log
    Oct 25 13:12:54 localhost tokoro10nu[3830]:emerg log
    

備考

rsyslog.confでは以下の設定がデフォルトで設定されています。

*.emerg    :omusrmsg:*

これは「すべてのファシリティの emerg メッセージを、ログイン中の全ユーザにブロードキャストする」設定です。 この設定がデフォルトで入っているので、loggerコマンドでemergのログを出力した瞬間、以下出力が表示されます。

[tokoro10nu@rhel9 ~]$ logger -p authpriv.crit "crit log"
[tokoro10nu@rhel9 ~]$ logger -p authpriv.alert "alert log"
[tokoro10nu@rhel9 ~]$ logger -p authpriv.emerg "emerg log"
[tokoro10nu@rhel9 ~]$
Broadcast message from systemd-journald@localhost.localdomain (Sat 2025-10-25 13:12:54 JST):

tokoro10nu[3830]: emerg log


Message from syslogd@localhost at Oct 25 13:12:54 ...
 tokoro10nu[3830]:emerg log

[tokoro10nu@rhel9 ~]$

▼参考

tech-lab.sios.jp qiita.com tex2e.github.io

END