DEV Community

Ubuntu Fundamentals: /tmp

The Unsung Hero: Mastering /tmp in Production Ubuntu Systems

A recent production incident involving a runaway process filling /tmp on a critical application server highlighted a pervasive issue: inadequate understanding and management of /tmp. This wasn’t a simple disk space issue; it exposed a cascade of problems – application instability, logging failures, and ultimately, service disruption. In modern Ubuntu-based systems, particularly within cloud VMs (AWS, Azure, GCP) and containerized environments (Docker, Kubernetes), /tmp is often treated as an afterthought. This is a critical mistake. Properly configuring and monitoring /tmp is fundamental to system reliability, security, and performance, especially in long-term support (LTS) production deployments.

What is "/tmp" in Ubuntu/Linux Context?

/tmp is a directory designated for storing temporary files. Crucially, it’s not a persistent storage location. On Ubuntu (and Debian-based systems), /tmp is typically mounted as a tmpfs filesystem by default. This means it resides in RAM and is cleared on reboot. This behavior is defined in /etc/fstab:

tmpfs   /tmp   tmpfs   defaults,noatime,mode=1777   0   0
Enter fullscreen mode Exit fullscreen mode

The mode=1777 setting grants sticky bit permissions, meaning users can only delete their own files within /tmp. Older systems or custom configurations might use a disk-backed /tmp, which introduces different considerations (discussed later). Key system tools interacting with /tmp include systemd (for service temporary files), APT (for package extraction), various compilers, and applications requiring temporary storage during operation. systemd-tmpfiles manages the cleanup of files in /tmp based on configurations in /etc/tmpfiles.d/.

Use Cases and Scenarios

  1. Application Temporary Files: Many applications (e.g., image processing, video encoding) create temporary files in /tmp during operation. Failure to clean these up can lead to disk space exhaustion, even with tmpfs.
  2. Package Management: APT extracts package archives into /tmp before installation. A corrupted or incomplete extraction can leave behind large files.
  3. Database Operations: Database servers (PostgreSQL, MySQL) utilize /tmp for temporary tables, sorting operations, and backups. Large queries or backups can quickly fill the space.
  4. Containerized Environments: Docker and Kubernetes often leverage /tmp for build processes, layer caching, and application runtime data. Improperly managed /tmp within containers can impact host system performance.
  5. Secure Session Management: Some applications use /tmp to store temporary session data, requiring careful permission management to prevent unauthorized access.

Command-Line Deep Dive

  • Checking /tmp Usage: df -h /tmp provides current disk space usage. du -hsx /tmp/* | sort -rh | head -10 identifies the largest files/directories within /tmp.
  • Monitoring tmpfs Growth: watch -n 1 'df -h /tmp' provides a real-time view of /tmp usage.
  • Cleaning Up Old Files: find /tmp -type f -atime +7 -delete removes files older than 7 days. Caution: Test thoroughly before deploying.
  • Inspecting tmpfiles Configuration: cat /etc/tmpfiles.d/* shows the rules for automatic cleanup.
  • Systemd Service Temporary Files: systemctl status <service_name> --properties reveals the TmpFiles setting, indicating where the service stores temporary files. Example: systemctl status apache2 --properties | grep TmpFiles
  • Checking Sticky Bit: ls -ld /tmp confirms the sticky bit is set (drwxrwxrwt).

System Architecture

graph LR
    A[Application] --> B(/tmp);
    C[APT] --> B;
    D[Database] --> B;
    E[Docker/Kubernetes] --> B;
    F[systemd] --> B;
    G[Kernel (tmpfs)] --> B;
    H[systemd-tmpfiles] --> B;
    B --> G;
    H --> B;
    style B fill:#f9f,stroke:#333,stroke-width:2px
Enter fullscreen mode Exit fullscreen mode

/tmp sits as a central point of interaction for numerous system components. systemd leverages /tmp for service-specific temporary files, managed by systemd-tmpfiles. APT uses it during package installation. The kernel’s tmpfs implementation provides the underlying filesystem. Applications directly write to /tmp. The interaction between these components must be understood to effectively manage /tmp. journald can also indirectly impact /tmp if applications log excessively to files within /tmp.

Performance Considerations

Using tmpfs for /tmp offers significant performance benefits due to its in-memory nature. However, excessive usage can lead to memory pressure, impacting overall system performance. htop and free -m are essential for monitoring memory usage. iotop can identify processes heavily utilizing /tmp I/O.

To tune /tmp performance:

  • vm.vfs_cache_pressure: Adjust this sysctl parameter (default 100) to control how aggressively the kernel reclaims memory used for filesystem caches. Lower values favor caching. sysctl -w vm.vfs_cache_pressure=50
  • tmpfs Size: Increase the size of the tmpfs mount in /etc/fstab if necessary. However, avoid allocating excessive memory. tmpfs /tmp tmpfs defaults,noatime,mode=1777,size=2G 0 0
  • Application Optimization: Encourage applications to use more efficient temporary file management practices (e.g., using mkstemp to create unique temporary files).

Security and Hardening

/tmp is a common target for exploits. Security measures include:

  • Sticky Bit: Ensure the sticky bit (mode=1777) is set in /etc/fstab.
  • AppArmor/SELinux: Implement AppArmor or SELinux profiles to restrict application access to /tmp.
  • noexec Mount Option: Mount /tmp with the noexec option to prevent execution of files directly from /tmp. Add noexec to the /etc/fstab entry.
  • Regular Auditing: Use auditd to monitor file access and modification within /tmp. auditctl -w /tmp -p wa -k tmp_access
  • UFW/iptables: While not directly related to /tmp, securing network access to the system is crucial to prevent remote exploitation.

Automation & Scripting

An Ansible playbook snippet to ensure /tmp is mounted as tmpfs with the correct permissions:

---
- name: Ensure /tmp is a tmpfs mount
  mount:
    path: /tmp
    src: tmpfs
    fstype: tmpfs
    opts: defaults,noatime,mode=1777
    state: mounted
Enter fullscreen mode Exit fullscreen mode

Cloud-init can be used to configure /tmp during instance initialization. A cloud-init snippet:

#cloud-config
mounts:
  - path: /tmp
    source: tmpfs
    type: tmpfs
    options: defaults,noatime,mode=1777
Enter fullscreen mode Exit fullscreen mode

Logs, Debugging, and Monitoring

  • journalctl: Monitor system logs for errors related to /tmp access or cleanup. journalctl -xe | grep tmp
  • dmesg: Check kernel messages for filesystem errors. dmesg | grep tmpfs
  • lsof: Identify processes holding open files in /tmp. lsof /tmp
  • strace: Trace system calls made by a process to understand its interaction with /tmp. strace -p <pid>
  • System Health Checks: Implement monitoring to alert on high /tmp usage (e.g., using Prometheus and Grafana).

Common Mistakes & Anti-Patterns

  1. Ignoring /tmp: Treating /tmp as a limitless storage space. Correct: Implement monitoring and cleanup policies.
  2. Using Disk-Backed /tmp Without Consideration: Using a disk-backed /tmp without understanding the performance implications. Correct: Prefer tmpfs unless specific application requirements dictate otherwise.
  3. Insufficient Cleanup: Failing to implement automatic cleanup of temporary files. Correct: Utilize systemd-tmpfiles or scheduled find commands.
  4. Incorrect Permissions: Not setting the sticky bit, allowing users to delete each other's files. Correct: Ensure mode=1777 in /etc/fstab.
  5. Hardcoding Paths: Applications hardcoding absolute paths within /tmp instead of using mkstemp. Correct: Use mkstemp to create unique, secure temporary files.

Best Practices Summary

  1. Always use tmpfs for /tmp unless absolutely necessary to use a disk-backed filesystem.
  2. Enforce the sticky bit (mode=1777) in /etc/fstab.
  3. Implement automatic cleanup using systemd-tmpfiles or scheduled find commands.
  4. Monitor /tmp usage with system health checks.
  5. Restrict application access to /tmp using AppArmor or SELinux.
  6. Consider the noexec mount option for enhanced security.
  7. Encourage applications to use mkstemp for creating temporary files.
  8. Regularly audit /tmp for unexpected files or permissions.
  9. Document /tmp configuration and cleanup policies.
  10. Understand the interaction between /tmp and services like systemd, APT, and databases.

Conclusion

Mastering /tmp is not merely a system administration task; it’s a critical component of building reliable, secure, and performant Ubuntu-based systems. Ignoring its nuances can lead to significant production incidents. Take the time to audit your existing systems, build automated configuration and cleanup scripts, implement robust monitoring, and document your standards. A well-managed /tmp is a silent guardian of system stability.

Top comments (0)