Table of Contents
- Understanding Temporary Storage in Linux
- Key Temporary Directories
- Tools for Managing Temporary Storage
- Best Practices for Temporary Storage
- Advanced Techniques
- Troubleshooting Common Issues
- Conclusion
- References
Understanding Temporary Storage in Linux
Temporary storage in Linux refers to files and directories designed for short-term use. Unlike permanent storage (e.g., /home or /var), temporary data is often:
- Volatile: Lost on system reboot (e.g., in-memory storage).
- Ephemeral: Deleted automatically after use or after a predefined period.
- High-I/O: Used for frequent read/write operations (e.g., application caches).
Volatile vs. Non-Volatile Temporary Storage
Linux uses two primary types of temporary storage:
1. Disk-Based Temporary Storage
Stored on physical disks (e.g., HDD/SSD) and persists across reboots (unless explicitly cleaned). Examples include /tmp (cleared on reboot by default on some systems) and /var/tmp (persists across reboots).
2. In-Memory Temporary Storage (tmpfs)
Stored in RAM or swap, offering faster read/write speeds than disk-based storage. Data is lost on reboot, making it ideal for temporary, high-performance workloads. Examples include /dev/shm (shared memory for inter-process communication) and custom tmpfs mounts.
Key Temporary Directories
Linux systems rely on standardized directories for temporary storage. Understanding their purposes, persistence, and behavior is foundational to effective management.
1. /tmp: System-Wide Temporary Files
-
Purpose: Shared temporary space for all users and processes. Used by system utilities, applications, and users for short-lived files (e.g., installation scripts, session data).
-
Persistence: Typically cleared on system reboot or shutdown (via tools like
systemd-tmpfilesortmpwatch). -
Size: Depends on the underlying filesystem (e.g., if
/tmpis on the root partition, it shares space with other directories). -
Permissions: World-writable (
drwxrwxrwt), with the sticky bit set (tat the end of permissions). The sticky bit ensures only the owner of a file (or root) can delete it, preventing accidental or malicious file deletion by other users.Example: Check
/tmppermissions and disk usage:ls -ld /tmp # Output: drwxrwxrwt 10 root root 4096 Oct 1 12:00 /tmp df -h /tmp # Check available space in /tmp
2. /var/tmp: Persistent Temporary Files
-
Purpose: Temporary files that need to survive reboots (e.g., large downloads, backup snapshots, or data needed across sessions).
-
Persistence: Not cleared on reboot by default (cleaned manually or via tools like
tmpwatchwith longer retention periods). -
Size: Like
/tmp, it uses space from the underlying filesystem (often the root partition).Example: List files in
/var/tmpolder than 30 days:find /var/tmp -type f -mtime +30
3. /dev/shm: Shared Memory for Inter-Process Communication (IPC)
-
Purpose: A
tmpfsmount (in-memory storage) for processes to share data quickly. Used by applications like databases, web servers, or desktop environments (e.g., GNOME uses/dev/shmfor session management). -
Persistence: Data is lost on reboot.
-
Size: By default, 50% of available RAM (configurable via mount options).
Example: Check
/dev/shmsize and usage:df -h /dev/shm # Output: tmpfs 4.0G 12K 4.0G 1% /dev/shm
Tools for Managing Temporary Storage
Linux provides a suite of tools to monitor, clean, and optimize temporary storage. Below are the most essential ones:
1. Basic Monitoring & Cleanup Commands
These tools help identify bloat and manually clean temporary files.
df and du: Check Disk Usage
df -h /tmp: Show free space in/tmp.du -sh /tmp/*: List size of top-level files/directories in/tmp.du -ah /var/tmp | sort -rh | head -10: Find the 10 largest files in/var/tmp.
find: Delete Old or Unneeded Files
Use find to target files by age, size, or type.
Example 1: Delete files in /tmp older than 7 days:
find /tmp -type f -mtime +7 -delete
Example 2: Delete empty directories in /var/tmp older than 30 days:
find /var/tmp -type d -empty -mtime +30 -delete
2. tmpwatch/tmpreaper: Automated Cleanup
tmpwatch (Red Hat-based systems) and tmpreaper (Debian/Ubuntu) automatically delete old temporary files. They are often run via cron jobs.
-
tmpwatchSyntax:tmpwatch [options] <age> <directory>-m: Measure age by modification time (default).-d: Delete empty directories.-f: Force deletion of read-only files.
Example: Clean
/tmpby deleting files older than 24 hours and empty directories older than 7 days:tmpwatch -m 24 -d 168 /tmp # -m 24 = 24 hours, -d 168 = 7 days (168 hours) -
tmpreaper(Debian/Ubuntu alternative):
Example: Delete/var/tmpfiles older than 30 days:tmpreaper 30d /var/tmp
3. systemd-tmpfiles: Modern Systemd-Based Cleanup
Most modern Linux distributions (e.g., Ubuntu 20.04+, Fedora, Debian 11+) use systemd-tmpfiles to manage temporary files. It reads configuration files in /usr/lib/tmpfiles.d/, /run/tmpfiles.d/, and /etc/tmpfiles.d/ to define rules for creating, deleting, or cleaning temporary files.
-
Key Config Files:
/usr/lib/tmpfiles.d/tmp.conf: Default rules for/tmpand/var/tmp(e.g., clean/tmpon boot)./etc/tmpfiles.d/custom.conf: User-defined rules (override system defaults here).
Example: Add a custom rule to clean
/mnt/mytmpfiles older than 1 day:
Create/etc/tmpfiles.d/mytmp.conf:d /mnt/mytmp 0755 root root 1d # d = directory, 0755 = permissions, 1d = clean after 1 dayApply the rule immediately:
systemd-tmpfiles --clean /etc/tmpfiles.d/mytmp.conf
4. mount: Configure tmpfs Storage
tmpfs (in-memory storage) can be mounted manually with custom size limits. Use mount or /etc/fstab for persistence across reboots.
-
Temporary
tmpfsMount (lost on reboot):mount -t tmpfs -o size=2G tmpfs /mnt/mytmp # Mount 2GB tmpfs at /mnt/mytmp -
Permanent
tmpfsMount (via/etc/fstab):
Add this line to/etc/fstabto mount a 4GBtmpfsat/mnt/fasttmpon boot:tmpfs /mnt/fasttmp tmpfs defaults,size=4G 0 0
Best Practices for Temporary Storage
1. Security
- Enforce the Sticky Bit: Ensure
/tmpand/var/tmphave the sticky bit set (chmod +t /tmp) to prevent unauthorized file deletion. - Avoid Sensitive Data: Never store passwords, encryption keys, or PII in temporary directories. Use
mktempto create secure, unique temporary files:secure_temp_file=$(mktemp) # Creates a file like /tmp/tmp.abc123 with 0600 permissions - Restrict Permissions: Limit access to application-specific temporary directories (e.g.,
chmod 700 /tmp/myapp).
2. Performance
- Use
tmpfsfor Speed: For I/O-heavy tasks (e.g., compiling code), mount atmpfsto reduce disk latency:mount -t tmpfs -o size=8G tmpfs /tmp/build # Use 8GB of RAM for compiling - Balance Size vs. Memory: Avoid overprovisioning
tmpfs(e.g., don’t allocate 90% of RAM to/dev/shm), as it can lead to out-of-memory (OOM) errors.
3. Automated Cleanup
- Leverage
systemd-tmpfiles: Define rules in/etc/tmpfiles.d/to automate cleanup (e.g., delete logs in/tmp/app-logsafter 24 hours). - Schedule
tmpwatch/tmpreaper: Use cron to run cleanup jobs during off-peak hours (e.g., daily at 3 AM):# Add to root's crontab (crontab -e) 0 3 * * * /usr/sbin/tmpwatch -m 24 /tmp # Clean /tmp daily at 3 AM
4. Compliance
For regulated environments (e.g., HIPAA, GDPR), ensure temporary files containing sensitive data are:
- Encrypted at rest (if stored on disk).
- Automatically purged after use (use
shredorwipefor disk-based temp files). - Logged (track creation/deletion times for audits).
Advanced Techniques
1. Custom tmpfs Mounts for Applications
Some applications (e.g., Elasticsearch, Redis) benefit from dedicated tmpfs mounts for scratch space. For example, mount a 10GB tmpfs for Elasticsearch’s temp directory:
# Add to /etc/fstab
tmpfs /var/lib/elasticsearch/tmp tmpfs defaults,size=10G,uid=elasticsearch,gid=elasticsearch 0 0
2. Persistent Temporary Storage with OverlayFS
Combine tmpfs (speed) and disk storage (persistence) using overlayfs. For example, use a tmpfs as the upper layer (fast writes) and /var/tmp as the lower layer (persistent storage):
mount -t overlay overlay -o lowerdir=/var/tmp,upperdir=/tmp/overlay,workdir=/tmp/work /mnt/hybrid-tmp
3. Managing Temporary Storage in Containers
In Docker/Kubernetes, use tmpfs mounts to isolate temporary data and avoid writing to container images:
-
Docker Example: Mount a 2GB
tmpfsin a container:docker run -it --tmpfs /tmp:size=2G alpine sh -
Kubernetes Example: Define a
tmpfsvolume in a pod:volumes: - name: tmp-storage emptyDir: medium: Memory sizeLimit: 2Gi
Troubleshooting Common Issues
1. /tmp or /var/tmp Is Full
- Identify Large Files: Use
du -sh /tmp/* | sort -rh | headto find space hogs. - Clean Old Files: Run
tmpwatch -m 24 /tmpor delete large, unused files manually. - Expand Storage: If
/tmpis on a separate partition, resize it withresize2fs(ext4) orxfs_growfs(XFS).
2. Permission Denied in /tmp
- Check Sticky Bit: Ensure
/tmphasdrwxrwxrwtpermissions (chmod +t /tmpif missing). - Verify Ownership: Ensure the user has read/write access to the file (use
ls -l /tmp/filename).
3. tmpfs Running Out of Space
- Adjust Size: Remount the
tmpfswith a larger size (e.g.,mount -o remount,size=16G /dev/shm). - Monitor Usage: Use
df -h /dev/shmto track usage and identify processes consuming excess memory (e.g.,lsof /dev/shm).
4. systemd-tmpfiles Not Cleaning Files
- Check Config Files: Verify rules in
/etc/tmpfiles.d/(e.g., ensured /tmp 1777 root root 1dis present to clean/tmpdaily). - View Logs: Check
journalctl -u systemd-tmpfiles-clean.servicefor errors.
Conclusion
Effective management of temporary storage is critical for maintaining Linux system performance, security, and reliability. By understanding key directories like /tmp, /var/tmp, and /dev/shm, leveraging tools like systemd-tmpfiles and tmpwatch, and following best practices for security and cleanup, you can ensure temporary storage works for you—without causing bloat or risk.
Whether you’re optimizing a personal workstation or managing enterprise servers, the techniques in this guide will help you keep temporary storage lean, fast, and secure.