Post

systemd-tmpfiles and /tmp Management

systemd-tmpfiles and /tmp Management

/tmp is the directory everyone writes to and nobody cleans up. On systemd hosts, systemd-tmpfiles is the tool that creates, cleans, and ages temporary files and directories, and it is the correct way to manage /tmp, /run, and application-specific scratch space.

What systemd-tmpfiles Does

systemd-tmpfiles reads configuration files from /usr/lib/tmpfiles.d/, /run/tmpfiles.d/, and /etc/tmpfiles.d/ and applies them. Each line describes a file or directory to create, clean, or age.

1
2
3
4
5
6
7
8
9
10
11
# Show the current configuration
systemd-tmpfiles --cat-config

# Apply all rules now
sudo systemd-tmpfiles --create

# Clean up expired files
sudo systemd-tmpfiles --clean

# Dry run
sudo systemd-tmpfiles --clean --dry-run

Tip: --clean is what runs on a timer. It removes files older than the age specified in the configuration, which is how /tmp stays under control.

The Configuration Format

1
2
3
4
# Type  Path              Mode  User  Group  Age  Argument
d       /run/myapp        0755  myapp myapp  -    -
f       /run/myapp/pid    0644  myapp myapp  -    -
r       /run/myapp/stale  -     -     -      -    -
  • d — create a directory.
  • f — create a file.
  • r — remove a path.
  • e — clean a directory, keeping the directory itself.
  • v — create a subvolume (btrfs).

Tip: e is the type you want for /tmp. It cleans the contents but keeps the directory, which is exactly the behaviour you expect.

Managing /tmp

1
2
3
4
5
6
7
# /etc/tmpfiles.d/tmp.conf

# Clean /tmp files older than 10 days
e /tmp 1777 root root 10d

# Clean /var/tmp files older than 30 days
e /var/tmp 1777 root root 30d

Tip: The default on most distributions is 10 days for /tmp and 30 days for /var/tmp. Shortening it is a common response to a full disk.

Creating Application Directories

1
2
3
4
5
6
7
8
9
10
# /etc/tmpfiles.d/myapp.conf

# Create the runtime directory
d /run/myapp 0755 myapp myapp -

# Create the state directory
d /var/lib/myapp 0750 myapp myapp -

# Create a lock file
f /run/myapp/lock 0644 myapp myapp -

Tip: RuntimeDirectory= and StateDirectory= in a systemd unit do the same thing more cleanly. Use tmpfiles.d when you need the directory to exist before the service starts.

The Cleanup Timer

1
2
3
4
5
6
7
8
# Is the timer active?
systemctl status systemd-tmpfiles-clean.timer

# When does it run?
systemctl list-timers systemd-tmpfiles-clean.timer

# Run it manually
sudo systemctl start systemd-tmpfiles-clean.service

Tip: The timer runs daily by default. If /tmp fills faster than that, shorten the age in tmpfiles.d rather than running the cleanup more often.

PrivateTmp for Services

1
2
3
# /etc/systemd/system/myapp.service
[Service]
PrivateTmp=true

Tip: PrivateTmp=true gives the service its own /tmp and /var/tmp, invisible to other processes. It is the cleanest way to stop a service from interfering with system temporary files.

The Full Type List

tmpfiles.d supports more types than the common d, f, r, and e.

TypeMeaning
fCreate a file if it does not exist
f+Create or truncate a file
wWrite to a file if it exists
w+Write to a file, creating it if needed
dCreate a directory
DCreate or empty a directory
eClean a directory, keeping the directory itself
vCreate a btrfs subvolume
qCreate a btrfs subvolume or directory
QCreate or empty a btrfs subvolume or directory
pCreate a FIFO
p+Create or truncate a FIFO
LCreate a symlink
L+Create or replace a symlink
cCreate a character device
c+Create or replace a character device
bCreate a block device
b+Create or replace a block device
CRecursively copy a file or directory
C+Recursively copy or replace
xRemove a path and its contents
XRemove a path and its contents, ignoring errors
rRemove a file or empty directory
RRecursively remove a path
zAdjust the SELinux context
ZRecursively adjust the SELinux context
tSet extended attributes
TRecursively set extended attributes
hSet file attributes
HRecursively set file attributes
aSet POSIX ACLs
ARecursively set POSIX ACLs
mSet the mtime of a file or directory

Tip: C+ and X are the two types most people miss. C+ is the cleanest way to seed a directory from a template, and X is the cleanest way to remove a directory tree.

systemd-tmpfiles –purge and –remove

1
2
3
4
5
6
7
8
# Remove all files and directories created by tmpfiles.d
sudo systemd-tmpfiles --remove

# Remove all files and directories, including those not created by tmpfiles.d
sudo systemd-tmpfiles --purge

# Dry run
sudo systemd-tmpfiles --remove --dry-run

Tip: --remove is the correct way to clean up after a package is uninstalled. It removes only the paths that tmpfiles.d created, which is far safer than rm -rf.

Quick Reference

1
2
3
4
5
6
7
8
9
10
11
# Show the configuration
systemd-tmpfiles --cat-config

# Apply rules
sudo systemd-tmpfiles --create

# Clean expired files
sudo systemd-tmpfiles --clean

# Run the cleanup timer now
sudo systemctl start systemd-tmpfiles-clean.service

Tip: Work in order: check the configuration, check the timer, then shorten the age if /tmp fills too fast. Most /tmp incidents are a missing cleanup rule or a service writing gigabytes of scratch files.

This post is licensed under CC BY 4.0 by the author.