The purpose of /tmp directory on GNU/Linux has been to provide programs space for temporary files. A system reboot automatically deletes the file in /tmp directory. So, anything in the directory /tmp won’t exist after system reboot. In this article, we cover /tmp on RAM.
Most of GNU/Linux based distributions can mount /tmp as RAM based tmpfs (temporary file system). It not only removes disk I/O but also increases performance. tmpfs resides in the memory rather than on the disk.
Benefits of /tmp on RAM
A. Performance
Read and Write will be significantly faster as /tmp mounts on RAM as tmpfs. Besides, RAM’s read and write speeds are faster as compared to that of a Hard Drive or Solid State Drive. Hence, applications can access data on /tmp much faster.
B. Disk Optimization
Temporary files on tmpfs, reduces disk usage. This would free up resources for other disk I/O operations. Especially fewer Read and Write operations to Flash based Storage reduces the requirement to TRIM NAND based Disk. Apart from that, it also results in fewer Disk wake-ups.
C. Data Security
As temporary data is not written to disk, the chances of data leak from Hard Disk/SSD are next to none.
Limitations of /tmp on RAM disk
A. Pseudo Data Security
Data can however still leak. While our system is live and a program’s data resides in tmpfs or other side channels depending upon security measures taken by the program.
But data will not leak after device gets powered down, with exception for cold boot method for data recovery.
B. RAM Limit
tmpfs size is limited to amount of RAM available in the system. Therefore, size of /tmp is limited. If a program hogs on /tmp directory for huge amounts of data then system will resort back to swap space or slow down the entire system, with hardly any RAM for other processes.
Configure /tmp on tmpfs
On RHEL/CentOS/Fedora/AlmaLinux:
To Check
systemctl is-enabled tmp.mount
Enable –
systemctl enable tmp.mount
Disable –
systemctl disable tmp.mount
On Debian/Ubuntu:
Check –
systemctl is-enabled tmp.mount
Enable –
cp /usr/share/systemd/tmp.mount /etc/systemd/system/ systemctl enable tmp.mount
Disable –
systemctl disable tmp.mount
Manually On GNU/Linux:
Add to /etc/fstab
tmpfs /tmp tmpfs rw,nosuid,noatime,nodev,size=4G,mode=1777 0 0
*size=4G which depends on RAM allocation.
Manually resize on fly :
mount -o remount,size=4G,noatime /tmp