Ubuntu utilizes the systemd temporary files management framework (systemd-tmpfiles-clean.service) triggered by systemd-tmpfiles-clean.timer to periodically purge stale files, sockets, and lockfiles from volatile storage directories such as /tmp and /var/tmp. However, reviewing system logs with systemctl is-system-running can report a degraded state accompanied by the error: Failed to start Daily Cleanup of Temporary Directories.
Why Does systemd-tmpfiles-clean.service Fail?
The cleanup service parses declarative configuration files in /usr/lib/tmpfiles.d/, /run/tmpfiles.d/, and /etc/tmpfiles.d/ before scanning temporary storage paths. Primary causes of service failures include:
- Permission and Sticky Bit Violations on /tmp: The standard sticky bit permission (
1777) missing from/tmpor/var/tmp, causing security traversal checks to abort. - Corrupted or Unreadable Sockets in /tmp: Dead daemon sockets or deeply nested directory trees holding unprivileged file handles that
systemd-tmpfilescannot unlink. - Syntax Errors in Custom tmpfiles.d Configurations: Third-party packages or administrators adding malformed line entries inside
/etc/tmpfiles.d/*.conf. - Read-Only Root Filesystem: Disk volume faults forcing Ubuntu to remount the root partition in read-only mode, blocking cleanup transactions.
- Extended File Attributes (Immutable Flags): Files inside temporary directories flagged with the immutable attribute (
chattr +i), preventing standard deletion.
Method 1: Check Journal Logs and Identify Syntax Errors
Running the cleanup binary with diagnostic parameters reveals the exact configuration file or path triggering the failure.
- Open a root shell or connect via SSH.
- Check the service status:
sudo systemctl status systemd-tmpfiles-clean.service - Query the systemd journal for the service:
sudo journalctl -u systemd-tmpfiles-clean.service -b --no-pager - Look for log entries indicating Failed to parse line or Operation not permitted.
Method 2: Restore Standard Sticky Bit Permissions on /tmp and /var/tmp
The Linux filesystem standard requires temporary directories to have the sticky bit enabled (permissions 1777) so users can only delete their own files.
- Check current permissions on temporary directories:
ls -ld /tmp /var/tmp - If permissions do not display as
drwxrwxrwt, restore the standard sticky bit and full permissions:sudo chmod 1777 /tmp sudo chmod 1777 /var/tmp - Ensure that both directories are owned by root:
sudo chown root:root /tmp /var/tmp - Attempt to run the cleanup service manually:
sudo systemd-tmpfiles --clean
Method 3: Remove Immutable Attributes from Stale Files
If automated deployment scripts inadvertently marked files inside /tmp as immutable, the cleanup process cannot remove them.
- Scan
/tmpfor files with the immutable (i) attribute:sudo lsattr -R /tmp 2>/dev/null | grep -E '^....i' - If immutable files are identified, remove the attribute:
sudo chattr -i /tmp/problematic-file - Test running the service again:
sudo systemctl restart systemd-tmpfiles-clean.service
Method 4: Validate Custom tmpfiles.d Configuration Files
Syntax errors in configuration files under /etc/tmpfiles.d/ cause the parser to exit prematurely.
- List custom configuration files:
ls -la /etc/tmpfiles.d/ - Execute a dry-run test of all tmpfiles definitions:
sudo systemd-tmpfiles --dry-run --clean - Review the output for syntax warnings or line errors. Correct or remove any obsolete third-party configuration files.
- Verify that the service transitions to an active (exited) status with code 0:
sudo systemctl status systemd-tmpfiles-clean.service