RHEL 8の/tmpのファイル削除の設定を確認する!

RHEL 8の/tmpの削除の設定を確認します。

systemdへの登録有無を確認する!

systemdに/tmp削除のサービスが登録されていることを確認します。

# systemctl list-unit-files | grep systemd-tmpfiles | sort
systemd-tmpfiles-clean.service             static   
systemd-tmpfiles-clean.timer               static   
systemd-tmpfiles-setup-dev.service         static   
systemd-tmpfiles-setup.service             static   

サービスの起動状態を確認する!

/tmpの削除に関しては、「systemd-tmpfiles-clean.service」と「systemd-tmpfiles-clean.timer」が担っています。

# systemctl status systemd-tmpfiles-clean.service
● systemd-tmpfiles-clean.service - Cleanup of Temporary Directories
   Loaded: loaded (/usr/lib/systemd/system/systemd-tmpfiles-clean.service; static; vendor preset: disabled)
   Active: inactive (dead) since Thu 2022-04-07 08:15:23 JST; 6h ago
...
# systemctl status systemd-tmpfiles-clean.timer
 systemctl status systemd-tmpfiles-clean.timer
● systemd-tmpfiles-clean.timer - Daily Cleanup of Temporary Directories
   Loaded: loaded (/usr/lib/systemd/system/systemd-tmpfiles-clean.timer; static; vendor preset: disabled)
   Active: active (waiting) since Wed 2022-04-06 07:59:19 JST; 1 day 6h ago
...
Warning: journal has been rotated since unit was started, output may be incomplete.
# systemctl status systemd-tmpfiles-setup-dev.service
● systemd-tmpfiles-setup-dev.service - Create Static Device Nodes in /dev
   Loaded: loaded (/usr/lib/systemd/system/systemd-tmpfiles-setup-dev.service; static; vendor preset: disabled)
   Active: active (exited) since Wed 2022-04-06 07:59:17 JST; 1 day 6h ago
...
# systemctl status systemd-tmpfiles-setup.service
● systemd-tmpfiles-setup.service - Create Volatile Files and Directories
   Loaded: loaded (/usr/lib/systemd/system/systemd-tmpfiles-setup.service; static; vendor preset: disabled)
   Active: active (exited) since Wed 2022-04-06 07:59:19 JST; 1 day 6h ago
...

serviceへの登録内容を確認する!

「systemd-tmpfiles --clean」を実行していることが分かります。

# cat /usr/lib/systemd/system/systemd-tmpfiles-clean.service
#  SPDX-License-Identifier: LGPL-2.1+
#
#  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.

[Unit]
Description=Cleanup of Temporary Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)
DefaultDependencies=no
Conflicts=shutdown.target
After=local-fs.target time-sync.target
Before=shutdown.target

[Service]
Type=oneshot
ExecStart=/usr/bin/systemd-tmpfiles --clean
SuccessExitStatus=65
IOSchedulingClass=idle

起動タイミングを確認する!

起動タイミングは、ブート後15分とその後1時間おきになります。

# cat /usr/lib/systemd/system/systemd-tmpfiles-clean.timer
#  SPDX-License-Identifier: LGPL-2.1+
#
#  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.

[Unit]
Description=Daily Cleanup of Temporary Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)

[Timer]
OnBootSec=15min
OnUnitActiveSec=1d

削除の設定を確認する!

# cat /usr/lib/tmpfiles.d/tmp.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.

# See tmpfiles.d(5) for details

# Clear tmp directories separately, to make them easier to override
q /tmp 1777 root root 10d
q /var/tmp 1777 root root 30d

# Exclude namespace mountpoints created with PrivateTmp=yes
x /tmp/systemd-private-%b-*
X /tmp/systemd-private-%b-*/tmp
x /var/tmp/systemd-private-%b-*
X /var/tmp/systemd-private-%b-*/tmp

# Remove top-level private temporary directories on each boot
R! /tmp/systemd-private-*
R! /var/tmp/systemd-private-*

/tmpの状態を確認する!

/tmpを確認すると、ファイルが削除されて存在していないことが分かります。

# ls -l /tmp
total 0
drwx------. 3 root     root     17 Apr  6 07:59 systemd-private-82dc82d420a4462487ba5755961dbfad-colord.service-HYf1Se
drwx------. 3 root     root     17 Apr  6 07:59 systemd-private-82dc82d420a4462487ba5755961dbfad-ModemManager.service-hmibQh
drwx------. 3 root     root     17 Apr  6 07:59 systemd-private-82dc82d420a4462487ba5755961dbfad-rtkit-daemon.service-g0SOCg
drwx------. 2 usradmin usradmin  6 Mar 24 15:36 tracker-extract-files.1000

tmp.confの設定方法を確認する!

manコマンドで、「tmp.conf」の設定方法を確認できます。様々なタイプの一時ファイルの作成と削除を、「tmp.conf」の設定で行うことができます。

# man tmpfiles.d

おわりに

/tmpの配下のファイルは、systemdで管理されたサービスにより自動で削除されていきます。

参考情報

関連記事

関連書籍(Amazon)

N/A