Stopping /var/log from Dooming Your Server with a Simple tmpfs Trick
When a server runs out of space on the root partition, services stop, backups fail, and the machine can become unresponsive. On busy production systems the culprit is almost always the log tree under /var/log. Even with log rotation, a mis‑configured service or a sudden spike in verbosity can fill the disk in minutes. The classic fix is to move logs to a separate partition or to a remote syslog server, but that adds complexity and a new attack surface.
A lightweight, low‑maintenance alternative is to keep the entire /var/log tree in RAM using tmpfs. The logs are still written to disk on reboot, but the risk of a full‑disk crash is eliminated while keeping the same file layout and permissions. Below is a step‑by‑step guide that shows how to set it up on a typical RHEL‑style system, the trade‑offs, and how to keep the logs useful for compliance and forensic purposes.
Why tmpfs for /var/log makes sense
- Speed – Writing to RAM is orders of magnitude faster than spinning disks or SSDs. This reduces I/O contention for services that log heavily (e.g., web servers, databases, VPNs).
- Isolation – If the disk becomes full, the kernel will not attempt to write logs to disk, so the log files stay consistent. The system continues to run.
- Simplicity – No need to create a dedicated partition, adjust
/etc/fstab, or manage remote syslog daemons. The configuration lives in a single systemd unit file. - Security – Logs are kept in memory, reducing the attack surface for an adversary who might try to tamper with log files on disk. However, this also means logs are lost on reboot, so you must decide if that trade‑off is acceptable.
Prerequisites
- A system running systemd (most modern distributions).
- Root or sudo privileges.
- A backup strategy that captures logs before reboot, if you need them for compliance.
Step 1 – Create a systemd unit to mount /var/log as tmpfs
Create /etc/systemd/system/tmpfs-var-log.mount with the following contents:
[Unit]
Description=Mount /var/log as tmpfs
Before=local-fs.target
ConditionPathExists=/var/log
[Mount]
What=tmpfs
Where=/var/log
Type=tmpfs
Options=mode=0755,uid=0,gid=0,size=512M
[Install]
WantedBy=local-fs.target
size=512Mlimits the memory usage. Adjust based on your RAM and log volume.- The
mode,uid, andgidoptions preserve the default ownership and permissions of/var/log. - The
Before=local-fs.targetline ensures the mount happens before other services that might write to/var/log.
Reload systemd and enable the unit:
sudo systemctl daemon-reload
sudo systemctl enable --now tmpfs-var-log.mount
Verify the mount:
mount | grep /var/log
# tmpfs on /var/log type tmpfs (rw,mode=0755,uid=0,gid=0,size=512M)
Step 2 – Preserve logs across reboots
Since the logs are volatile, you’ll want to persist them before each reboot. One approach is to use logrotate to archive the logs to a separate directory on disk, then truncate the in‑memory files.
Create a cron job that runs at reboot:
sudo crontab -e
# Add the following line
@reboot /usr/local/bin/persist-logs.sh
Create /usr/local/bin/persist-logs.sh:
#!/bin/bash
set -euo pipefail
LOGDIR="/var/log"
BACKUPDIR="/var/log/backup"
TIMESTAMP=$(date +%Y%m%d%H%M%S)
mkdir -p "$BACKUPDIR"
rsync -a --delete "$LOGDIR/" "$BACKUPDIR/$TIMESTAMP/"
# Truncate all log files to free memory
find "$LOGDIR" -type f -exec truncate -s 0 {} +
# Optional: rotate the backup directory to keep only the last 7 days
find "$BACKUPDIR" -maxdepth 1 -type d -mtime +7 -exec rm -rf {} +
Make it executable:
sudo chmod +x /usr/local/bin/persist-logs.sh
Now every reboot will archive the current logs to /var/log/backup/<timestamp> and clear the in‑memory files. The backup directory can be mounted on a separate partition or sent to a remote syslog server if you need long‑term retention.
Step 3 – Adjust logrotate for in‑memory logs
logrotate expects to rotate files on disk. When the files live in tmpfs, the rotation must happen before the next reboot. Add a new logrotate configuration:
sudo tee /etc/logrotate.d/tmpfs-var-log <<'EOF'
/var/log/*.log {
daily
missingok
rotate 7
compress
delaycompress
notifempty
create 0640 root adm
postrotate
systemctl restart rsyslog
endscript
}
EOF
Because the logs are in memory, the postrotate script restarts rsyslog to reopen the rotated files. Adjust the rotate count and compression settings to match your disk space budget.
Trade‑offs and caveats
| Aspect | Benefit | Drawback |
|---|---|---|
| Memory usage | Fast I/O, no disk wear | Consumes RAM; may affect other services if RAM is limited |
| Log persistence | Logs survive until reboot | Lost on crash or power loss unless archived |
| Security | Logs not exposed on disk | If an attacker gains root, they can read logs in memory, but they can also read them on disk if you archive them |
| Compliance | Keeps logs for audit | Some regulations require logs to be stored on persistent media; you must archive them to a compliant store |
| Maintenance | Single unit file | Requires manual backup script; not automatic on every crash |
When to use this trick
- High‑traffic web or database servers where log I/O is a bottleneck.
- Docker or container hosts where
/var/logis shared across many containers and can fill quickly. - Development or staging environments where uptime is more important than long‑term log retention.
When to avoid it
- Regulated environments (PCI‑DSS, HIPAA) that mandate long‑term log storage on immutable media.
- Systems with very low RAM (e.g., 1 GB) where a 512 MB tmpfs would starve other processes.
- Critical services that rely on log timestamps across reboots; you’ll need to ensure the backup process preserves timestamps.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Services fail to write logs after boot | /var/log not mounted | Check systemctl status tmpfs-var-log.mount |
rsyslog or systemd-journald reports “cannot open log file” | Permissions mismatch | Verify mode, uid, gid in the unit file |
| Log rotation fails with “file not found” | Log files were truncated before rotation | Ensure logrotate runs before the backup script or adjust timing |
| Memory pressure spikes | Tmpfs size too large | Reduce size= in the unit file or add noexec,nosuid options to limit usage |
Alternatives and complementary measures
Use
systemd-journaldwithStorage=volatile– This keeps the journal in RAM and writes a snapshot to disk on shutdown. It’s similar to tmpfs but integrated with systemd.
Link: https://www.freedesktop.org/software/systemd/man/systemd-journald.service.htmlMove only high‑volume logs to tmpfs – For example, keep
/var/log/nginx/access.login tmpfs while other logs stay on disk.Configure logrotate to compress aggressively – Even on disk, compression can keep usage low.
Final thoughts
Mounting /var/log as a tmpfs is a pragmatic way to shield your server from disk‑full disasters caused by runaway logs. It trades off persistence for resilience, which is often a worthwhile swap in production environments that can afford to lose logs on reboot. By coupling the mount with a simple backup script and a tuned logrotate configuration, you keep the benefits of fast I/O and low maintenance while still satisfying audit requirements through archived snapshots.
See also
- Application-Level Firewalls
- Debugging flaky DNS on a Raspberry Pi bridge setup with nftables rules
- Nftables log‑filter tricks to find hidden routing loops on a bridged LAN
- When systemd‑resolved stalls: Diagnosing and fixing the 1‑second lookup delay
- Resolve DNS Cache Poisoning on Raspberry Pi with resolvectl – One‑Step Fix
- Fixing a Crashed Btrfs Subvolume After Unexpected Reboot