loading...

24.04.2024

Optimizing Disk Space Usage on Linux Servers: A Practical Guide

Introduction

Running out of disk space is a frequent issue for Linux servers, leading to service disruptions, application crashes, or failed deployments. As a DevOps engineer, optimizing disk space is a critical skill to ensure system reliability. This article provides a step-by-step guide to identifying and resolving disk space issues on Linux servers using built-in tools and automation, helping you maintain healthy servers and impress potential employers.

Why Disk Space Optimization Matters

Full disks can cause:

  • Application failures due to inability to write logs or data
  • Slow performance from excessive I/O
  • Failed CI/CD pipelines or backups
    Proactively managing disk space demonstrates your ability to maintain production systems and prevent outages.

Prerequisites

  • Root or sudo access to a Linux server (e.g., Ubuntu, CentOS)
  • Basic familiarity with Linux commands
  • Tools like df, du, find, and ncdu (install if needed)

Step-by-Step Guide to Optimizing Disk Space

1. Check Current Disk Usage

Start by identifying how much disk space is used and where:

df -h

This displays disk usage in a human-readable format. Focus on filesystems nearing 100% usage (e.g., / or /var).

Example output:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       100G   85G   15G  85% /

2. Identify Large Files and Directories

Use du to find space-hogging directories:

du -h / | sort -rh | head -n 10

This lists the top 10 largest directories. Focus on common culprits like /var/log, /tmp, or /home.

For a more interactive tool, install and use ncdu:

# Ubuntu
sudo apt install ncdu
# CentOS
sudo yum install ncdu
ncdu /

Navigate with arrow keys to identify large files or directories.

3. Clean Up Old Logs

Log files in /var/log often consume significant space. Check their sizes:

ls -lh /var/log

To clean up:

  • Compress Old Logs: Use gzip to compress logs older than a week:
    find /var/log -type f -name "*.log" -mtime +7 -exec gzip {} ;
  • Clear Large Logs: Truncate active logs safely (e.g., for syslog):
    sudo truncate -s 0 /var/log/syslog
  • Use logrotate: Configure /etc/logrotate.conf to automate log rotation. Example for /var/log/myapp.log:
    /var/log/myapp.log {
      daily
      rotate 7
      compress
      missingok
      notifempty
    }

    Test the configuration:

    sudo logrotate -f /etc/logrotate.conf

4. Remove Unused Packages

Unnecessary packages can accumulate, especially on long-running servers. On Debian/Ubuntu:

sudo apt autoremove
sudo apt autoclean

On CentOS/RHEL:

sudo yum autoremove
sudo yum clean all

These commands remove unused dependencies and clean cached package data.

5. Delete Temporary Files

Temporary files in /tmp or /var/tmp can pile up. Safely remove old files:

find /tmp -type f -mtime +7 -delete
find /var/tmp -type f -mtime +7 -delete

Verify no critical processes are using these files before deletion:

lsof /tmp

6. Automate Cleanup with a Script

Create a script to automate disk cleanup. Save as cleanup_disk.sh:

#!/bin/bash

# Configuration
LOG_DIR="/var/log"
TMP_DIR="/tmp"
DAYS_TO_DELETE=7
LOG_FILE="/var/log/disk_cleanup.log"

# Ensure log file exists
touch "$LOG_FILE"
if [ $? -ne 0 ]; then
  echo "ERROR: Cannot write to $LOG_FILE" >&2
  exit 1
fi

# Log function
log() {
  echo "$(date '+%Y-%m-%d %H:%M:%S') $1" >> "$LOG_FILE"
}

# Clean temporary files
find "$TMP_DIR" -type f -mtime +$DAYS_TO_DELETE -delete
log "Deleted temporary files older than $DAYS_TO_DELETE days in $TMP_DIR"

# Compress logs
find "$LOG_DIR" -type f -name "*.log" -mtime +$DAYS_TO_DELETE -exec gzip {} ;
log "Compressed logs older than $DAYS_TO_DELETE days in $LOG_DIR"

# Remove old compressed logs
find "$LOG_DIR" -type f -name "*.gz" -mtime +30 -delete
log "Deleted compressed logs older than 30 days in $LOG_DIR"

# Clean package cache (Ubuntu)
if command -v apt >/dev/null 2>&1; then
  apt autoremove -y && apt autoclean
  log "Cleaned apt package cache"
fi

# Clean package cache (CentOS)
if command -v yum >/dev/null 2>&1; then
  yum autoremove -y && yum clean all
  log "Cleaned yum package cache"
fi

# Report disk usage
DISK_USAGE=$(df -h / | tail -1 | awk '{print $5}')
log "Disk usage after cleanup: $DISK_USAGE"

Make it executable:

chmod +x cleanup_disk.sh

7. Schedule the Script with Cron

Run the cleanup daily at 3 AM. Edit the crontab:

crontab -e

Add:

0 3 * * * /path/to/cleanup_disk.sh

8. Test the Script

Run manually to verify:

./cleanup_disk.sh

Check /var/log/disk_cleanup.log for output:

2025-07-16 15:00:00 Deleted temporary files older than 7 days in /tmp
2025-07-16 15:00:01 Compressed logs older than 7 days in /var/log
2025-07-16 15:00:02 Disk usage after cleanup: 60%

Common Issues and Fixes

  • Permission Errors: Run the script as root or with sudo to access system directories.
  • Accidental Deletion: Double-check find commands with -print before -delete (e.g., find /tmp -type f -mtime +7 -print).
  • Logrotate Failures: Verify /etc/logrotate.conf syntax with logrotate -d.
  • Package Manager Errors: Ensure apt or yum commands are run with sufficient privileges.

Best Practices

  • Monitor Disk Usage: Use tools like Prometheus or Nagios to alert before disks fill up.
  • Backup Critical Data: Archive important logs to a remote location before deletion.
  • Test Incrementally: Test cleanup commands on a non-production server first.
  • Log Actions: Always log cleanup activities for auditing and debugging.

Conclusion

Optimizing disk space on Linux servers is a vital DevOps skill that prevents outages and ensures system reliability. This guide and script provide a reusable solution for identifying and resolving disk space issues, showcasing your expertise in system administration. Save this guide for your next server maintenance task, and keep your systems running smoothly!

Posted in LinuxTaggs: