Ubuntu Desktop 20.04の/tmpのファイル削除の設定を確認する!

Ubuntu Desktop 20.04の/tmpの削除の設定を確認します。

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

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

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

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

/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 (/lib/systemd/system/systemd-tmpfiles-clean.service; static; vendor preset: enabled)
     Active: inactive (dead) since Fri 2022-12-09 20:27:23 JST; 10h ago
TriggeredBy: ● systemd-tmpfiles-clean.timer
       Docs: man:tmpfiles.d(5)
             man:systemd-tmpfiles(8)
    Process: 17408 ExecStart=/usr/bin/systemd-tmpfiles --clean (code=exited, status=0/SUCCESS)
   Main PID: 17408 (code=exited, status=0/SUCCESS)

Warning: journal has been rotated since unit was started, output may be incomplete.
# systemctl status systemd-tmpfiles-clean.timer
● systemd-tmpfiles-clean.timer - Daily Cleanup of Temporary Directories
     Loaded: loaded (/lib/systemd/system/systemd-tmpfiles-clean.timer; static; vendor preset: enabled)
     Active: active (waiting) since Fri 2022-12-09 20:12:20 JST; 11h ago
    Trigger: Sat 2022-12-10 20:27:23 JST; 13h left
   Triggers: ● systemd-tmpfiles-clean.service
       Docs: man:tmpfiles.d(5)
             man:systemd-tmpfiles(8)

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 (/lib/systemd/system/systemd-tmpfiles-setup-dev.service; static; vendor preset: enabled)
     Active: active (exited) since Fri 2022-12-09 20:12:18 JST; 11h ago
       Docs: man:tmpfiles.d(5)
             man:systemd-tmpfiles(8)
    Process: 450 ExecStart=/usr/bin/systemd-tmpfiles --prefix=/dev --create --boot (code=exited, status=0/SUCCESS)
   Main PID: 450 (code=exited, status=0/SUCCESS)

Warning: journal has been rotated since unit was started, output may be incomplete.
# systemctl status systemd-tmpfiles-setup.service
● systemd-tmpfiles-setup.service - Create Volatile Files and Directories
     Loaded: loaded (/lib/systemd/system/systemd-tmpfiles-setup.service; static; vendor preset: enabled)
     Active: active (exited) since Fri 2022-12-09 20:12:20 JST; 11h ago
       Docs: man:tmpfiles.d(5)
             man:systemd-tmpfiles(8)
    Process: 994 ExecStart=/usr/bin/systemd-tmpfiles --create --remove --boot --exclude-prefix=/dev (code=exited, statu>
   Main PID: 994 (code=exited, status=0/SUCCESS)

Warning: journal has been rotated since unit was started, output may be incomplete.

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-set.target
Before=shutdown.target

[Service]
Type=oneshot
ExecStart=systemd-tmpfiles --clean
SuccessExitStatus=DATAERR
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
D /tmp 1777 root root -
#q /var/tmp 1777 root root 30d

/tmpの状態を確認する!

/tmpを確認すると、1日以内に作成されたファイルしか存在していないことが分かります。

# date
2022年 12月 10日 土曜日 08:02:39 JST
# ls -l /tmp
total 52
-rw------- 1 usradmin usradmin    0 12月  9 20:12 config-err-6SJBjM
drwx------ 5 root     root     4096 12月  9 20:12 snap-private-tmp
drwx------ 2 usradmin usradmin 4096 12月  9 20:12 ssh-tH6hDX60WvzF
drwx------ 3 root     root     4096 12月  9 20:12 systemd-private-d88328f367714c3f88b004ea22daa7ba-colord.service-FwatIg
drwx------ 3 root     root     4096 12月 10 07:49 systemd-private-d88328f367714c3f88b004ea22daa7ba-fwupd.service-b8RuNf
drwx------ 3 root     root     4096 12月  9 20:12 systemd-private-d88328f367714c3f88b004ea22daa7ba-ModemManager.service-tbWGSf
drwx------ 3 root     root     4096 12月  9 20:12 systemd-private-d88328f367714c3f88b004ea22daa7ba-openvpn-client@client.service-aqCqUg
drwx------ 3 root     root     4096 12月  9 20:12 systemd-private-d88328f367714c3f88b004ea22daa7ba-switcheroo-control.service-YE97Fi
drwx------ 3 root     root     4096 12月  9 20:12 systemd-private-d88328f367714c3f88b004ea22daa7ba-systemd-logind.service-t2vVAi
drwx------ 3 root     root     4096 12月  9 20:12 systemd-private-d88328f367714c3f88b004ea22daa7ba-systemd-resolved.service-09SGKf
drwx------ 3 root     root     4096 12月  9 20:12 systemd-private-d88328f367714c3f88b004ea22daa7ba-systemd-timesyncd.service-avTj0e
drwx------ 3 root     root     4096 12月  9 20:12 systemd-private-d88328f367714c3f88b004ea22daa7ba-upower.service-KX3pzi
drwx------ 2 usradmin usradmin 4096 12月  9 20:35 tracker-extract-files.1000
drwx------ 2 root     root     4096 12月  9 20:12 vmware-root

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

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

# man tmpfiles.d

おわりに

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

参考情報

関連記事

関連書籍(Amazon)

N/A