tmpfs Cheatsheet: RAM-Based Filesystem Operations
tmpfs is a temporary filesystem that stores files in virtual memory (RAM and swap) instead of persistent storage. This makes it extremely fast for read/write operations but means data is lost when the system reboots. This cheatsheet covers essential tmpfs operations for Linux system administrators and developers.
Understanding tmpfs
tmpfs creates a virtual filesystem that appears as a regular directory but stores all files in memory. It’s particularly useful for:
- Temporary file storage requiring high performance
- Caching frequently accessed data
- Creating secure temporary workspaces
- Reducing disk I/O for temporary operations
Essential tmpfs Operations
Mounting tmpfs to Directory
Create and mount a tmpfs filesystem to a directory:
sudo mount -t tmpfs -o size=SIZE tmpfs /path/to/mountpoint
Examples:
# Mount 1GB tmpfs to /tmp/fast
sudo mount -t tmpfs -o size=1G tmpfs /tmp/fast
# Mount 512MB tmpfs to /tmp/cache
sudo mount -t tmpfs -o size=512M tmpfs /tmp/cache
# Mount tmpfs with specific permissions
sudo mount -t tmpfs -o size=2G,mode=1777 tmpfs /tmp/shared
Listing tmpfs Mounts
View all current tmpfs mounts:
df -h | grep tmpfs
For more detailed information:
mount | grep tmpfs
View tmpfs usage and available space:
df -h /path/to/tmpfs/mount
Advanced tmpfs Configuration
Setting Size Limits
# Percentage of total RAM
sudo mount -t tmpfs -o size=50% tmpfs /tmp/half-ram
# Specific size in bytes, KB, MB, or GB
sudo mount -t tmpfs -o size=1073741824 tmpfs /tmp/1gb # 1GB in bytes
sudo mount -t tmpfs -o size=500M tmpfs /tmp/500mb # 500MB
sudo mount -t tmpfs -o size=2G tmpfs /tmp/2gb # 2GB
Permission and Ownership Options
# Set specific permissions (octal notation)
sudo mount -t tmpfs -o size=1G,mode=0755 tmpfs /tmp/restricted
# Set user and group ownership
sudo mount -t tmpfs -o size=1G,uid=1000,gid=1000 tmpfs /tmp/userspace
# Combine multiple options
sudo mount -t tmpfs -o size=2G,mode=1777,uid=root,gid=root tmpfs /tmp/public
Number of Files and Inodes
# Limit number of files (inodes)
sudo mount -t tmpfs -o size=1G,nr_inodes=1000 tmpfs /tmp/limited-files
# Unlimited inodes (default)
sudo mount -t tmpfs -o size=1G,nr_inodes=0 tmpfs /tmp/unlimited
Unmounting tmpfs
# Unmount tmpfs filesystem
sudo umount /path/to/tmpfs/mount
# Force unmount if busy
sudo umount -f /path/to/tmpfs/mount
# Lazy unmount (unmount when no longer busy)
sudo umount -l /path/to/tmpfs/mount
Persistent tmpfs Configuration
Adding to /etc/fstab
For tmpfs mounts that should persist across reboots:
# Edit /etc/fstab
sudo nano /etc/fstab
# Add tmpfs entry
tmpfs /tmp/persistent tmpfs defaults,size=1G,mode=1777 0 0
Common fstab tmpfs entries:
# General purpose tmpfs
tmpfs /tmp/cache tmpfs defaults,size=512M,mode=1777 0 0
# User-specific tmpfs
tmpfs /home/user/tmpfs tmpfs defaults,size=1G,uid=1000,gid=1000,mode=0755 0 0
# System tmpfs with restricted access
tmpfs /var/tmp/system tmpfs defaults,size=2G,mode=0755 0 0
Monitoring tmpfs Usage
Real-time Usage Monitoring
# Watch tmpfs usage in real-time
watch -n 1 'df -h | grep tmpfs'
# Monitor specific tmpfs mount
watch -n 1 'df -h /tmp/fast'
# Detailed filesystem information
stat -f /path/to/tmpfs/mount
System Memory and tmpfs
# Check system memory usage
free -h
# View memory usage including tmpfs
cat /proc/meminfo | grep -E 'MemTotal|MemFree|Shmem'
# Check tmpfs usage from /proc/mounts
cat /proc/mounts | grep tmpfs
Performance Optimization
Optimal Size Configuration
- Small files: Use smaller tmpfs (100-500MB) for temporary builds
- Large operations: Allocate up to 50% of available RAM
- System stability: Never exceed 80% of total RAM for all tmpfs combined
Use Cases and Best Practices
Temporary Build Directories
# Create tmpfs for compilation
sudo mount -t tmpfs -o size=2G tmpfs /tmp/build
cd /tmp/build
# Perform build operations
sudo umount /tmp/build
Cache Directories
# Browser cache in tmpfs
sudo mount -t tmpfs -o size=500M tmpfs /home/user/.cache/browser
# Application cache
sudo mount -t tmpfs -o size=1G tmpfs /var/cache/app
Troubleshooting Common Issues
tmpfs Full Error
# Check current usage
df -h /path/to/tmpfs
# Increase tmpfs size
sudo mount -o remount,size=2G /path/to/tmpfs
# Clean up files if needed
sudo rm -rf /path/to/tmpfs/old-files/*
Permission Denied
# Check current permissions
ls -la /path/to/tmpfs
# Remount with correct permissions
sudo umount /path/to/tmpfs
sudo mount -t tmpfs -o size=1G,mode=1777 tmpfs /path/to/tmpfs
Mount Point Busy
# Check what's using the mount point
lsof /path/to/tmpfs
fuser -v /path/to/tmpfs
# Kill processes using the mount point
sudo fuser -k /path/to/tmpfs
# Force unmount
sudo umount -f /path/to/tmpfs
Security Considerations
Secure tmpfs Configuration
# Restricted access tmpfs
sudo mount -t tmpfs -o size=1G,mode=0700,uid=root,gid=root tmpfs /tmp/secure
# No execution permissions
sudo mount -t tmpfs -o size=1G,noexec,nosuid,nodev tmpfs /tmp/noexec
Data Protection
Remember that tmpfs data is:
- Volatile: Lost on reboot or unmount
- Swappable: May be written to swap space
- Memory-resident: Visible in memory dumps
For sensitive data, consider:
- Using encrypted swap
- Implementing proper cleanup procedures
- Setting appropriate file permissions
Advanced tmpfs Features
Swap Behavior
# Mount with swap usage control
sudo mount -t tmpfs -o size=1G,nr_inodes=1k tmpfs /tmp/controlled
# Check swap usage
swapon -s
cat /proc/swaps
Integration with systemd
Create a systemd service for tmpfs management:
[Unit]
Description=Custom tmpfs mount
Before=local-fs.target
[Mount]
What=tmpfs
Where=/tmp/service
Type=tmpfs
Options=size=1G,mode=1777
[Install]
WantedBy=multi-user.target
References and Resources
- Linux tmpfs Documentation - Official kernel documentation
- mount(8) Manual Page - Complete mount command reference
- fstab(5) Manual Page - Filesystem table configuration
- systemd.mount(5) - systemd mount unit configuration
Questions Answered in This Document
Q: How do I create a tmpfs filesystem in Linux? A: Use sudo mount -t tmpfs -o size=SIZE tmpfs /path/to/mountpoint
to create and mount a tmpfs filesystem of specified size to a directory.
Q: What is the difference between tmpfs and regular filesystems? A: tmpfs stores files in RAM/swap instead of disk storage, making it much faster but volatile (data is lost on reboot).
Q: How do I check tmpfs usage and available space? A: Use df -h | grep tmpfs
to see all tmpfs mounts and their usage, or df -h /path/to/tmpfs
for a specific mount.
Q: Can I make tmpfs mounts persistent across reboots? A: Yes, add tmpfs entries to /etc/fstab
with format: tmpfs /mount/point tmpfs defaults,size=1G,mode=1777 0 0
.
Q: How do I set size limits for tmpfs? A: Use the size option: size=1G
for 1GB, size=50%
for 50% of RAM, or size=512M
for 512MB.
Q: What happens when tmpfs runs out of space? A: Applications will receive “No space left on device” errors. Increase size with mount -o remount,size=NEWSIZE
or clean up files.
Q: How do I unmount a tmpfs filesystem? A: Use sudo umount /path/to/tmpfs
for normal unmount, or sudo umount -f /path/to/tmpfs
for forced unmount.
Q: Can I set permissions and ownership for tmpfs? A: Yes, use options like mode=0755
for permissions, uid=1000
for user ownership, and gid=1000
for group ownership.
Q: Is tmpfs data secure? A: tmpfs data can be swapped to disk and is visible in memory dumps. For sensitive data, use encrypted swap and proper cleanup procedures.
Q: What are the best use cases for tmpfs? A: Temporary builds, caching, frequently accessed small files, and any scenario requiring high-speed temporary storage.