What is systemd-tmpfiles and how does it manage temporary files?
systemd-tmpfiles creates, cleans, and sets permissions on temporary files/dirs from declarative .conf rules — so volatile paths exist on boot and old junk gets aged out automatically.
The point is declarative management: instead of scripting "if this dir is missing, make it," you write one line describing the desired state and systemd enforces it at boot and periodically. This solves a real problem — directories under /tmp and /run are wiped on reboot, so services that expect them would break; a tmpfiles rule recreates them every boot with the right owner and mode. The Age column lets it also delete files older than, say, 10 days, preventing temp dirs from slowly filling the disk.
Purpose:
- Create temporary directories on boot
- Clean up old temporary files
- Set permissions on directories
Configuration files:
/usr/lib/tmpfiles.d/*.conf # System defaults
/etc/tmpfiles.d/*.conf # Admin customizations
/run/tmpfiles.d/*.conf # Runtime
Service: systemd-tmpfiles-setup.service runs at boot
Commands:
# Apply all configurations
systemd-tmpfiles --create --remove
# Preview what would be done
systemd-tmpfiles --create --dry-run
Example configuration:
# Type Path Mode User Group Age
d /tmp/myapp 0755 root root 10d
Why use it?
- Ensures directories exist after reboot
- Automatically cleans old files
- Prevents disk fill-up from temporary files