Understanding /tmp Directory Cleanup: Systemd-tmpfiles Rules for Ubuntu & Fedora


4 views

Most modern Linux distributions including Ubuntu and Fedora use systemd-tmpfiles for managing temporary directories. This provides a unified approach to handling /tmp cleanup.

To see how your system handles /tmp cleanup:

# For systemd-based systems
systemctl status systemd-tmpfiles-clean.timer

# Check the configuration files
cat /usr/lib/tmpfiles.d/tmp.conf
cat /etc/tmpfiles.d/*.conf

Both distributions typically configure /tmp cleaning through:

# Ubuntu's default (since 18.04)
D /tmp 1777 root root 10d

# Fedora's default
D /tmp 1777 root root 10d

The 10d parameter means files older than 10 days will be deleted during regular cleanup.

To modify the cleanup behavior, create or edit /etc/tmpfiles.d/mytmp.conf:

# Keep files for 60 days, clean daily at 3AM
D /tmp 1777 root root 60d

Then apply changes:

systemd-tmpfiles --clean

The cleanup runs via:

  • A daily systemd timer (systemd-tmpfiles-clean.timer)
  • On boot (unless PrivateTmp=true is set for the service)

Some systems mount /tmp as tmpfs (in-memory filesystem), which clears on reboot. Check with:

mount | grep /tmp

To make persistent, edit /etc/fstab:

# Replace tmpfs with regular filesystem
# /tmp /tmp none bind 0 0

The /tmp directory follows different cleanup rules across Linux distributions. On modern systems, there are typically two approaches:

  • Systemd's tmpfiles.d (used by Ubuntu and Fedora)
  • Traditional cron jobs (older systems)

Ubuntu uses systemd-tmpfiles with these default settings:

# Check /usr/lib/tmpfiles.d/tmp.conf
D /tmp 1777 root root 10d

This configuration means:

  • Files older than 10 days are removed
  • Permissions are reset to 1777 on boot
  • Files aren't automatically cleared on reboot

Fedora also uses systemd but with different defaults:

# /usr/lib/tmpfiles.d/tmp.conf
D /tmp 1777 root root 30d

Key differences from Ubuntu:

  • 30-day retention period instead of 10
  • Same permission handling

To implement your desired "daily at 3AM, delete files >60 days old" policy:

# Create custom config in /etc/tmpfiles.d/mytmp.conf
D /tmp 1777 root root 60d

# Then create a systemd timer:
# /etc/systemd/system/tmp-cleanup.timer
[Unit]
Description=Daily tmp cleanup

[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true

[Install]
WantedBy=timers.target

For systems without systemd:

# Add to crontab -e
0 3 * * * find /tmp -type f -atime +60 -delete

Check active tmpfiles rules with:

systemd-analyze cat-config tmpfiles.d/tmp.conf
  • Some applications assume /tmp persistence across reboots
  • Consider using XDG_RUNTIME_DIR for user-specific temp files
  • For security, set proper permissions (1777 for /tmp)