How to Increase /tmp Directory Size on EC2 Ubuntu Instances for High-Temp Storage Needs


2 views

When running Ubuntu on AWS EC2 with EBS volumes, the default /tmp directory often inherits storage limitations from the root partition (typically 8-10GB). For applications requiring extensive temporary storage - like video processing, big data jobs, or compilation tasks - this quickly becomes problematic. The error No space left on device surfaces when /tmp fills up, despite having ample space in other mounted volumes.

While creating /mnt/tmp and symlinking might seem tempting:


sudo mkdir /mnt/tmp
sudo chmod 1777 /mnt/tmp
sudo rm -rf /tmp
sudo ln -s /mnt/tmp /tmp

This approach has drawbacks:

  • Potential compatibility issues with legacy applications
  • Security implications from changing sticky bit locations
  • Performance overhead for I/O-heavy operations

A more robust method uses mount binding to redirect /tmp without symlinks:


# Create new temp directory on larger volume
sudo mkdir /mnt/temp
sudo chmod 1777 /mnt/temp

# Backup existing /tmp contents
sudo cp -a /tmp/. /mnt/temp/

# Mount the new location
sudo mount --bind /mnt/temp /tmp

# Verify with:
df -h /tmp

Add to /etc/fstab for reboot persistence:


/mnt/temp /tmp none bind 0 0

Or use systemd for modern Ubuntu:


# Create /etc/systemd/system/tmp.mount
[Unit]
Description=Mount /tmp to larger volume

[Mount]
What=/mnt/temp
Where=/tmp
Type=none
Options=bind

[Install]
WantedBy=multi-user.target

For memory-intensive workloads, consider a dedicated tmpfs:


# Add to /etc/fstab
tmpfs /tmp tmpfs defaults,size=16G 0 0

Note: This consumes RAM, so size appropriately based on instance memory.

After implementation:


# Check mount points
mount | grep /tmp

# Monitor space
watch -n 5 "df -h /tmp; du -h --max-depth=1 /tmp | sort -h"

Containers often need explicit /tmp configuration:


# In docker-compose.yml
services:
  app:
    volumes:
      - /mnt/temp:/tmp
    tmpfs:
      - /run:size=128m

When running disk-intensive applications on AWS EC2 instances with Ubuntu, you might encounter "No space left on device" errors in /tmp. This happens because:

  • EC2's root volume (typically 8-10GB) houses /tmp by default
  • Additional EBS volumes mount under /mnt or /data
  • Many applications (especially Java-based ones) heavily use /tmp

Instead of symbolic links (which can cause compatibility issues), use bind mounts:


# Create new temp directory on larger volume
sudo mkdir /mnt/tmp
sudo chmod 1777 /mnt/tmp

# Add to /etc/fstab
echo "/mnt/tmp /tmp none bind 0 0" | sudo tee -a /etc/fstab

# Apply changes without reboot
sudo mount --bind /mnt/tmp /tmp

For memory-rich instances, consider RAM-based temp storage:


# Add to /etc/fstab
tmpfs /tmp tmpfs defaults,size=16G 0 0

# Apply immediately
sudo mount -a

For Ubuntu 16.04+ with systemd:


# Create override directory
sudo mkdir -p /etc/systemd/system/tmp.mount.d/

# Create config file
echo "[Mount]
What=/dev/xvdf
Where=/tmp
Type=ext4
Options=defaults" | sudo tee /etc/systemd/system/tmp.mount.d/override.conf

# Reload and restart
sudo systemctl daemon-reload
sudo systemctl restart tmp.mount

After implementation:


# Check mount points
df -h /tmp

# Verify permissions
ls -ld /tmp

# Monitor usage
sudo apt install iotop
iotop -o
  • Avoid symlinks for /tmp - some applications (like MySQL) may break
  • For EBS-backed /tmp, enable monitoring on the volume
  • tmpfs solutions vanish on reboot - ensure your app handles this
  • Always test changes in staging first