Use systemd's tmpfiles.d fragments to create, protect and clean transient files reliably across boots. 16.11.2025 | reading time: 3 min Temporary files can become a maintenance headache. Use systemd's tmpfiles.d configuration snippets to declare what should exist in /run, /tmp or other volatile paths, set modes and owners, and let the init system create and clean them predictably at boot and on a schedule. Hands-on example: create a runtime directory Create a simple tmpfiles.d fragment, apply it and inspect the result to prove it works right away. ```bash sudo tee /etc/tmpfiles.d/myapp.conf > /dev/null <<'EOF' d /run/myapp 0755 root root - EOF sudo systemd-tmpfiles --create /etc/tmpfiles.d/myapp.conf ls -ld /run/myapp ``` Output: ```text drwxr-xr-x 2 root root 4096 /run/myapp ``` Practical knobs and behaviors to watch Fragments use a compact line format with a type letter, a path, mode, uid, gid and an age field; type letters include d for directories, f for files, L for symlinks and r to remove entries, and age values like 10d mean ten days; fragments in /etc override vendor files in /usr/lib, and systemd runs tmpfiles setup at boot and a periodic cleanup service later, so test changes with the `systemd-tmpfiles --create` and `--clean` or `--remove` options before relying on them. When tmpfiles.d shines Use tmpfiles.d to: ensure runtime sockets and pid directories exist with correct permissions, trim caches or temporary stores on a schedule, or create ephemeral state in tmpfs; it centralizes policies and avoids ad-hoc init scripts that differ between boots or distributions. Safety and best practices Put local overrides in /etc/tmpfiles.d, review vendor fragments in /usr/lib/tmpfiles.d, prefer exact paths rather than wildcards when removing files, and test removal rules carefully to prevent accidental data loss; treat tmpfiles entries as part of configuration management and document intent. Next tools to know For cleanup or legacy setups consider specialized tools or daemons and learn how they differ from tmpfiles.d so you can choose the right approach for each system. Wrap-up and next steps tmpfiles.d gives you declarative control over volatile filesystem state — simple to write, powerful in practice; keep experimenting and include tmpfiles fragments in your automation, and if you want to level up your Linux abilities consider certification paths like CompTIA Linux+ or LPIC-1 with intensive exam prep at bitsandbytes.academy. Join Bits & Bytes Academy First class LINUX exam preparation. filesystem boot-process utilities scripting security