Why /tmp can become a silent disk‑drainer
On an Ubuntu desktop /tmp is just another folder on the root partition.
Every app that needs a scratch pad drops files there, and most of the time it’s fine—just a few megabytes.
But if a program starts spitting out large temporary data (think big builds, video editors, or a runaway installer) and never cleans up, that folder can gobble up the whole root filesystem.
A full root partition means apps crash, new files can’t be created, and worst of all you might find yourself locked out of the login screen.
The fix is simple: keep /tmp off the root or make sure it’s cleaned regularly.
Below I’ll walk through both options, the trade‑offs, and how to verify that the fix actually works.
1. Inspect the current situation
First, see how much space /tmp is using and whether it’s growing.
# Size of /tmp and the root filesystem
du -sh /tmp
df -h /
If /tmp is only a few MB you’re probably fine.
If it’s hundreds of MB or more, you’re at risk.
A quick way to spot a runaway temporary directory is to look for unusually large files:
sudo find /tmp -type f -size +100M -exec ls -lh {} \; | head
If you spot a file that looks like a temporary build artifact or a huge cache, that’s a candidate for cleanup.
2. Option A – Mount /tmp as a separate tmpfs
2.1 Why tmpfs?
tmpfs lives in RAM (and spills to swap if needed).
It never writes to disk, so it can’t fill your root partition.
It also wipes itself on reboot, keeping the space usage low, and it’s fast—great for lots of small files.
2.2 Create the tmpfs entry
Open /etc/fstab with your favourite editor:
sudo nano /etc/fstab
Add the following line at the end:
tmpfs /tmp tmpfs defaults,noatime,mode=1777,size=1G 0 0
| Option | Meaning |
|---|---|
noatime |
Skip updating file access times; saves writes. |
mode=1777 |
Sticky‑bit permissions for /tmp. |
size=1G |
Allocate up to 1 GB of RAM for /tmp. Adjust if you have more or less memory. |
Tip: If you want
/tmpto be larger than your RAM, setsize=to a value that will fit in RAM + swap. For example,size=2Gon a 4 GB system is safe.
2.3 Apply the change
sudo mount -a
If you see no errors, the new tmpfs is active. Verify:
df -h /tmp
You should see something like:
Filesystem Size Used Avail Use% Mounted on
tmpfs 1.0G 12M 988M 2% /tmp
2.4 Verify that old files are cleared
Create a test file:
echo "test" > /tmp/hello.txt
ls -l /tmp/hello.txt
Reboot the machine:
sudo reboot
After the reboot, check /tmp again:
ls -l /tmp
The file should be gone, confirming that the tmpfs is cleared on boot.
2.5 Caveats
| Issue | Mitigation |
|---|---|
| Swap usage | If /tmp grows beyond RAM, it will start using swap. On a desktop with limited swap, this can slow the system. Keep the size= parameter conservative. |
| Persistence | Files in /tmp are lost on reboot. If you need to keep a file across reboots, store it elsewhere (e.g., ~/Downloads). |
| Large temporary files | Some apps (video editors, big builds) create huge temporary files. If those exceed the tmpfs size, the app may fail. In that case, consider a dedicated partition instead. |
3. Option B – Use a separate partition for /tmp
If you prefer a disk‑based /tmp that persists across reboots, you can create a small dedicated partition or logical volume.
3.1 Create a new partition
-
Identify free space on your disk:
sudo fdisk -l -
Create a new partition (e.g.,
/dev/sda3) usingfdiskorgdisk. Pickext4or a similar filesystem. -
Format the partition:
sudo mkfs.ext4 -F /dev/sda3 -
Mount it temporarily to confirm it works:
sudo mkdir /mnt/tmp_test sudo mount /dev/sda3 /mnt/tmp_test sudo touch /mnt/tmp_test/ok sudo umount /mnt/tmp_test
3.2 Update /etc/fstab
sudo nano /etc/fstab
Add:
/dev/sda3 /tmp ext4 defaults,noatime,mode=1777 0 0
3.3 Apply and test
sudo mount -a
df -h /tmp
You should now see the new partition mounted at /tmp.
3.4 Pros and cons
| Benefit | Drawback |
|---|---|
| Persistence | Files survive reboots, useful for long‑running builds. |
| No RAM pressure | Uses disk space, not memory. |
| Potential fragmentation | If the partition is small and heavily used, fragmentation can slow access. |
4. Automatic cleanup with tmpreaper
Even with a dedicated /tmp, you might still want to keep it tidy.
tmpreaper is a lightweight utility that removes files older than a specified age.
4.1 Install
sudo apt update
sudo apt install tmpreaper
4.2 Configure
Edit /etc/tmpreaper.conf:
sudo nano /etc/tmpreaper.conf
Set the age threshold (e.g., 7 days):
# Remove files older than 7 days
TMPREAPER_MAX_AGE=7d
You can also set a minimum size to avoid deleting very small files that might still be useful.
4.3 Enable the systemd timer
sudo systemctl enable --now tmpreaper.timer
Check its status:
systemctl status tmpreaper.timer
You’ll see something like:
Active: active (waiting) since Mon 2026-11-27 12:34:56 UTC; 1h 23min ago
4.4 Verify cleanup
Create a test file older than 7 days:
sudo touch /tmp/oldfile
sudo touch -d "10 days ago" /tmp/oldfile
Run tmpreaper manually:
sudo tmpreaper -r /tmp
The file should be removed. Check the log:
journalctl -u tmpreaper
You’ll see entries confirming the deletion.
5. Combining approaches: tmpfs + tmpreaper
For most desktop users the simplest and safest solution is to mount /tmp as a tmpfs and let tmpreaper handle cleanup if you ever need to keep a small amount of data across reboots.
- Mount
/tmpas tmpfs (see 2.2). - Install and enable
tmpreaper(see 4.1–4.3).
Result:
- Zero risk of filling the root partition.
- Automatic cleanup of stale files.
- No manual intervention required.
6. Troubleshooting common issues
| Symptom | Likely cause | Fix |
|---|---|---|
mount: /tmp: cannot read superblock after editing /etc/fstab |
Typo in the fstab entry | Re‑edit /etc/fstab, correct the line, run sudo mount -a. |
/tmp is still on the root partition after reboot |
System didn’t remount due to a syntax error | Check /var/log/syslog for mount errors. |
Applications complain about missing /tmp after reboot |
Permissions wrong | Ensure mode=1777 is set; run sudo chmod 1777 /tmp. |
tmpreaper deletes files you need |
Threshold too low | Increase TMPREAPER_MAX_AGE or add exclusions in /etc/tmpreaper.conf. |
System slows down after creating a huge file in /tmp |
tmpfs size exceeded | Reduce the size= option or move to a disk‑based /tmp. |
7. Real‑world examples
7.1 Building a large kernel module
When compiling a kernel module, the build system creates a lot of temporary files.
On a 4 GB system, a single build can consume 500 MB of /tmp.
With a tmpfs of 1 GB, the build proceeds without risk.
If you prefer a persistent /tmp, a 2 GB partition will hold the build artifacts across reboots.
7.2 Using apt with a large cache
apt stores package files in /var/cache/apt/archives, not /tmp.
However, some third‑party installers (e.g., snapcraft) use /tmp for intermediate stages.
A tmpfs prevents the installer from filling the root partition.
7.3 Docker‑like containers on a desktop
If you run container runtimes (podman, Docker) locally, they often create temporary files in /tmp.
A dedicated tmpfs keeps the host’s root partition safe while still providing fast access for the containers.
8. Security considerations
Mounting /tmp as a tmpfs with mode=1777 is the default secure configuration.
It ensures:
- Only the owning process can delete its own files (
sticky bit). - No other user can modify or delete files they don’t own.
If you mount /tmp on a separate partition, keep the same permissions (mode=1777).
Avoid setting noexec unless you have a specific reason, as many applications need to execute temporary binaries.
9. Summary of steps
| Step | Command |
|---|---|
Mount /tmp as tmpfs |
`echo ’tmpfs /tmp tmpfs defaults,noatime,mode=1777,size=1G 0 0' |
| Apply mount | sudo mount -a |
| Install tmpreaper | sudo apt install tmpreaper |
| Configure tmpreaper | sudo nano /etc/tmpreaper.conf (set TMPREAPER_MAX_AGE=7d) |
| Enable timer | sudo systemctl enable --now tmpreaper.timer |
After these steps, your Ubuntu desktop will no longer risk running out of space due to /tmp over‑filling, and you’ll have a clean, fast temporary directory that’s easy to maintain.
See also
- How to Stop a Log File from Filling Your SSD in 5 Minutes
- When nftables silently drops your local web server: a step‑by‑step debug
- Running a FreeNAS Backup Server with rsync and Snapshots on a Raspberry Pi 4
- Rootless Podman: How I Got My Home Directory Synced to a Container Volume
- Setting up a Home NAS with Docker, NFS, and SMB – A Hands‑On Guide