Systemd-tmpfiles

From Leo's Notes
Last edited on 17 April 2023, at 21:06.

systemd-tmpfiles is a systemd subsystem that creates, deletes, and cleans up temporary files and directories on a system. Its main functions are:

  1. Create system directories such as /proc, /sys, /var, /tmp, etc.
  2. Cleanup operations on specific directories such as in /tmp and /var/tmp.

Directory cleanups are done using a systemd-tmpfiles-clean timer and service. On Red Hat based systems, the cleanup timer runs once 15 minutes after startup and also once a day.

Configuration[edit | edit source]

To see the current configs, run: systemd-tmpfiles --cat-config. Configuration files are located in the following directories:

  • Default system config files are located in /usr/lib/tmpfiles.d.
  • Alternatively: /run/tmpfiles.d/*.conf
  • Custom configs can be placed in /etc/tmpfiles.d. Overrides files with the same names in the other locations listed above.

Custom config[edit | edit source]

See the tmpfiles.d man page for a comprehensive overview. I will go over some important bits below.

To add your own configuration, place a file with a .conf file extention in /etc/tmpfiles.d/. Each line should contain a single directive for a single path. For example:

#Type Path        Mode UID  GID  Age Argument
d     /run/user   0755 root root 10d -
L     /tmp/foobar -    -    -    -   /dev/null

Types that you may frequently use are listed below:

Type Description
f Create a file if it doesn't exist
F Create a file or truncate it if it exists
d Create a directory
c Create a character device node
r Removes a file or directory if it exists. Does not follow symlinks. Use 'R' for recursive removes.
x Exclude a path during a clean operation
X Exclude a path (to a directory) during a clean operation
Q Creates a subvolume (or directory if the underlying filesystem doesn't support volumes)
L Creates a symlink
! Exclamation mark '!' denotes that the rule should only execute on system startup.

If an age is specified, automatic cleanup of files older than the specified age will be done. Files older than the specified age will be deleted.