Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

systemd-tmpfiles

Create, clean and remove temporary and volatile files and folders, following the rules in tmpfiles.d files.

Level
RHCSA exam
Group
Scheduling
Needs root
Yes
Package
systemd

What it does

systemd-tmpfiles reads rule files from /usr/lib/tmpfiles.d/ (packages), /run/tmpfiles.d/ and /etc/tmpfiles.d/ (yours; a file with the same name here replaces the package file). Each line is one rule: type, path, mode, user, group, age, argument.

At boot, systemd-tmpfiles-setup.service runs --create (make folders and files, for example in /run). Every day, systemd-tmpfiles-clean.timer runs --clean: files older than the age of their rule are deleted. RHEL's own rules clean /tmp after 10 days and /var/tmp after 30 days.

How to write it

systemd-tmpfiles [OPTION]... [CONFIG-FILE]...

Main parts

Line types (tmpfiles.d)

d PATH MODE USER GROUP AGE
Create a folder; with AGE, clean old files in it.
D PATH …
Like d, but empty the folder at every boot (--remove).
e PATH …
Clean an existing folder by age; do not create it.
v / q / Q PATH …
Create a folder or btrfs subvolume (with quota).
f PATH MODE USER GROUP - TEXT
Create a file if missing (with TEXT as content).
F PATH …
Create or empty a file.
w PATH - - - - TEXT
Write TEXT into an existing file (for example in /sys or /proc).
L PATH - - - - TARGET
Create a symbolic link.
C PATH - - - - SOURCE
Copy a file or folder if the target is missing.
x PATH
Do not clean this path (exception).
X PATH
Do not clean this path, but clean what is inside.
r / R PATH
Remove the path (R = with content) at --remove.
z / Z PATH MODE USER GROUP
Fix rights, owner and SELinux label (Z = recursive).
t / T PATH - - - - ATTR
Set extended attributes.
h / H PATH - - - - ATTR
Set file attributes (like chattr).
a / A PATH - - - - ACL
Set POSIX ACLs.

Age

10d, 12h, 30m, 1w
Age units: days, hours, minutes, weeks.
-
No cleaning.

Options 17

Most used

Actions

--create
Create the files and folders of the rules; write, set rights.
--clean
Delete files older than the age of their rule.
--remove
Remove the paths of r/R/D rules.
--purge
Remove everything the rules created.
--cat-config
Show all rule files, as they are used.
--tldr
Show the rules without comments and empty lines.

Scope

--user
Use the user rules (~/.config/user-tmpfiles.d/).
--boot
Also run rules marked with ! (only safe at boot).
--prefix=PATH
Only rules for paths under PATH.
--exclude-prefix=PATH
Skip rules for paths under PATH.
-E
Short for excluding /dev, /proc, /run, /sys.
--root=DIR / --image=FILE / --image-policy=POLICY
Work on another root folder or disk image.
--replace=PATH
Treat the given file as a replacement for PATH.
--graceful
Ignore rules with unknown users or groups.

Other

--no-pager
Do not use a pager.
-h, --help
Show help.
--version
Show the version.

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1See all rules in use

    systemd-tmpfiles --cat-config | less
    systemd-tmpfiles --tldr | grep -E '^[dDqQ] /(var/)?tmp'
    q /tmp 1777 root root 10d
    q /var/tmp 1777 root root 30d
  2. 2A folder for an app that must exist after every boot

    echo 'd /run/myapp 0755 myapp myapp -' > /etc/tmpfiles.d/myapp.conf
    systemd-tmpfiles --create /etc/tmpfiles.d/myapp.conf
    ls -ld /run/myapp
    drwxr-xr-x. 2 myapp myapp 40 Oct 27 13:10 /run/myapp

    /run is empty after every reboot; the rule makes the folder again at boot.

  3. 3Delete old files automatically

    echo 'd /var/tmp/cache 0750 root root 7d' > /etc/tmpfiles.d/cache.conf
    systemd-tmpfiles --clean /etc/tmpfiles.d/cache.conf

    Files in /var/tmp/cache older than 7 days are removed now and every day by the timer.

  4. 4Keep /tmp files longer than 10 days

    cp /usr/lib/tmpfiles.d/tmp.conf /etc/tmpfiles.d/tmp.conf
    sed -i 's|^q /tmp 1777 root root 10d|q /tmp 1777 root root 30d|' /etc/tmpfiles.d/tmp.conf

    The file in /etc with the same name replaces the package file.

  5. 5Protect one folder from cleaning

    echo 'x /tmp/build-keep' > /etc/tmpfiles.d/keep.conf
  6. 6Empty a folder at every boot

    echo 'D /var/lib/myapp/sessions 0700 myapp myapp -' > /etc/tmpfiles.d/sessions.conf
  7. 7Make a link at boot

    echo 'L /run/myapp.sock - - - - /var/lib/myapp/app.sock' >> /etc/tmpfiles.d/myapp.conf
  8. 8Fix rights and SELinux labels at boot (DevSecOps)

    echo 'Z /srv/app 0750 app app -' > /etc/tmpfiles.d/app-perms.conf
    systemd-tmpfiles --create /etc/tmpfiles.d/app-perms.conf
  9. 9Check the daily cleaning timer

    systemctl list-timers systemd-tmpfiles-clean.timer
    systemctl status systemd-tmpfiles-clean.service
  10. 10Write a kernel setting at boot

    echo 'w /sys/kernel/mm/transparent_hugepage/enabled - - - - never' > /etc/tmpfiles.d/thp.conf
  11. 11User rules without root

    mkdir -p ~/.config/user-tmpfiles.d
    echo 'd %h/.cache/build 0700 - - 3d' > ~/.config/user-tmpfiles.d/build.conf
    systemd-tmpfiles --user --clean

Watch out

  • The age counts from the newest of access, change and modify time — a file that is read often is not cleaned.
  • --remove deletes for real. Test new rules with --clean on a test folder first.
  • Rule paths must be absolute. %h (home) and %U (user ID) work only in user rules.

Files

/etc/tmpfiles.d/*.conf
Your rules (win over package rules with the same name).
/usr/lib/tmpfiles.d/*.conf
Package rules.
/run/tmpfiles.d/*.conf
Runtime rules.
~/.config/user-tmpfiles.d/
User rules.

Tips to remember

  • One line d PATH MODE USER GROUP AGE covers most tasks: "the folder must exist" and "old files must go".
  • systemd-tmpfiles --cat-config | grep PATH shows which rule touches a path.

Exit codes

0Success.
65A rule has a syntax error (with --create etc. the rest still runs).
73A file or folder could not be created.
OtherAnother error.

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man 8 systemd-tmpfiles