1

I have a server (Ubuntu 22.04) which has a tmpfs partition mounted at /tmp. The server only reboots for security updates, so I can't rely on reboots to clear out /tmp.

systemd-tmpfile is using default settings, which means that /usr/lib/tmpfiles.d/tmp.conf contains:

# Clear tmp directories separately, to make them easier to override
D /tmp 1777 root root -
#q /var/tmp 1777 root root 30d

However, this doesn't (ever) clear out /tmp. Should this be working, or should I replace the D with an r or similar? Or maybe I should just cron a daily cleanup job? If I have to cron a cleanup job, how should I over-ride the default tmp.conf to prevent it interfering? Thanks.

1 Answer 1

1

In short, this has nothing to do with tmpfs, and tmp.conf doesn't do what you want it to do. The - entry for /tmp means that no automatic cleanup will be done. See the man page for tmpfiles.d:

The age field only applies to lines starting with d, D, e, v, q, Q, C, x and X. If omitted or set to "-", no automatic clean-up is done.

Copy the default tmp.conf to /etc/tmpfiles.d/tmp.conf, and replace the - with 1d or similar. This file has a higher priority than the tmp.conf at /usr/lib/tmpfiles.d/.

The D means that the contents of the directory will be removed when systemd-tmpfiles --remove is run, and your distro will do that at boot time (it probably runs systemd-tmpfiles --remove --create --boot or something similar). So, the Ubuntu default is to clean up /tmp only at boot.

Note that this doesn't mean that /tmp will be automatically emptied out every 24 hours (or whatever). /usr/lib/tmpfiles.d/ contains other entries, such as

D! /tmp/.X11-unix 1777 root root 10d

The ! means that it's unsafe to remove these files at runtime, and it's only considered if the --boot option is given. In this case, the file also has to be 10 days or older (10d) in order to be deleted.

If you can't work out why a file isn't being deleted do a manual cleanup with debug enabled:

# SYSTEMD_LOG_LEVEL=debug systemd-tmpfiles --clean

and look for output lines like:

/usr/lib/tmpfiles.d/x11.conf:12: Ignoring entry D! "/tmp/.X11-unix" because --boot is not specified.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.