Server Disk Space Administration Guide

Professional Server Maintenance and Storage Management

Critical Notice

Administering disk space on a server is a critical task. If your server runs out of storage space, it can crash, leading to service interruptions and potential data loss. Regular monitoring and maintenance are essential for stable server operations.

๐Ÿ“‹ Overview

Disk space administration involves several key tasks that ensure your server maintains optimal performance and availability. This guide covers the following essential aspects:

Monitor Disk Usage
โ†’
Remove Unused Packages
โ†’
Clean System Logs
โ†’
Analyze Directories

๐Ÿ” Step 1: Check Disk Space

1Run the disk free command to view current storage usage:

df -h

Command Explanation

The -h flag means human-readable format, which displays sizes in KB, MB, and GB rather than raw bytes.

Sample Output:

Filesystem Size Used Avail Use% Mounted on /dev/sda1 23G 7.4G 15G 33% / tmpfs 2.0G 0 2.0G 0% /dev/shm /dev/sda2 100G 45G 50G 48% /var

Important Line to Monitor

The line with Mounted on "/" is the most critical. In the example above:

  • Size: 23 GB (total capacity)
  • Used: 7.4 GB
  • Available: 15 GB
  • Use%: 33%

๐Ÿ“Š Disk Usage Visualization

Current Disk Usage: 33%

33% Used (7.4 GB)

Warning Threshold: 60%

60% Used - Time to Clean Up

Critical Threshold: 85%

85% Used - Immediate Action Required

๐Ÿ“… Recommended Monitoring Schedule

Best Practice: Run this command every 2-3 weeks and maintain a history of your server's storage usage.

  1. Take a screenshot of the df -h output every two weeks
  2. Store screenshots with dates to build a historical record
  3. Compare results over time to identify trends
  4. When usage reaches approximately 60%, begin cleanup procedures

๐Ÿงน Step 2: Clean Unused Packages

2Remove packages that are no longer needed and clear the local repository:

sudo apt autoremove

What This Command Does

autoremove removes packages that were automatically installed as dependencies for other software but are no longer needed by any installed programs.

sudo apt clean

What This Command Does

clean clears out the local repository of retrieved package files. These are cached .deb files that remain after package installation and can safely be removed.

Combined Command (Run Both at Once):

sudo apt autoremove && sudo apt clean

๐Ÿ“ˆ Expected Results

Before cleanup:

Used: 7.4 GB

After running both commands:

Used: 7.3 GB

โœ… Immediate Impact

You should see an immediate reduction in disk usage. In this example, 100 MB was freed. The actual amount freed depends on how many unused packages and cached files were present.

Verify the changes:

df -h

๐Ÿ“ Step 3: Clean System Log Files

3Remove old system log files to free up additional space:

sudo journalctl --vacuum-time=1days

Command Explanation

This command uses the journalctl utility to manage system logs:

  • journalctl: System logging utility for Linux servers
  • --vacuum-time=1days: Removes archived log files older than 1 day

You can adjust the time period based on your needs (e.g., 2days, 1week, 1month).

Sample Output:

Vacuuming done, freed 2.3 MB archived space.

๐Ÿ“Š Cumulative Results

Disk Space Recovery Progress

Stage Action Taken Disk Used Space Freed
Initial Starting point 7.4 GB -
After Package Cleanup apt autoremove & clean 7.3 GB ~100 MB
After Log Cleanup journalctl vacuum 7.0 GB ~400 MB Total

๐Ÿ“ Step 4: Analyze Directory Sizes

4Identify directories consuming the most storage space:

First, navigate to the root directory:

cd /

Then run the disk usage analysis command:

sudo du -ah --max-depth=1 | sort -h

Command Breakdown

  • du: Disk usage command - estimates file and directory space usage
  • -a: Include all files (including hidden files)
  • -h: Human-readable format (KB, MB, GB)
  • --max-depth=1: Limit analysis to current directory and immediate subdirectories
  • | sort -h: Pipe output to sort command, ordered by size in human-readable format

Sample Output (sorted by size, largest at bottom):

8.0K /tmp 12K /root 24M /opt 87M /var/www 450M /home 1.3G /var 2.1G /boot 3.5G /usr 15G /

๐Ÿ”Ž Deep Dive into Large Directories

Based on the output above, /usr (3.5 GB) and /var (1.3 GB) are the largest directories. Let's investigate further:

Change to the /var directory:

cd /var

Run the same du command to see subdirectories:

sudo du -ah --max-depth=1 | sort -h

Sample Output:

4.0K /var/mail 8.0K /var/opt 24M /var/cache 87M /var/www 150M /var/log 1.1G /var/lib 1.3G /var

Here we can see that /var/lib (1.1 GB) is the largest subdirectory, followed by /var/log (150 MB).

๐ŸŒ Analyzing Web Server Directories

Navigate to the web directory:

cd /var/www

Analyze web directory space usage:

sudo du -ah --max-depth=1 | sort -h

Sample Output:

8.0K /var/www/html 87M /var/www/example.com 87M /var/www

The website directory example.com is using 87 MB of storage.

โš ๏ธ Critical Safety Warning

DO NOT Delete Files in System Directories

Exercise extreme caution when exploring system directories. Deleting the wrong files can render your server inoperable.

Forbidden Directory: /lib

Let's examine why the /lib directory is critical:

cd /lib
sudo du -ah --max-depth=1 | sort -h

โ›” ABSOLUTE PROHIBITION

  • NEVER work inside the /lib directory
  • NEVER delete files or directories in /lib
  • NEVER modify anything in system directories unless you are absolutely certain of the consequences

The /lib directory contains essential system libraries. Deleting or modifying files here WILL break your server!

โœ… Safe Directories for Cleanup

Approved Areas for Manual Cleanup

You may safely check and clean the following directories:

1. Home Directory

cd ~
sudo du -ah --max-depth=1 | sort -h

Look for unnecessary downloaded files, old backups, or temporary files you no longer need.

2. /var Directory

cd /var
sudo du -ah --max-depth=1 | sort -h

Check for old log files that haven't been automatically cleaned, or unnecessary cached data.

3. /tmp Directory

cd /tmp
ls -lh

Temporary files can often be safely removed, though the system usually cleans this automatically.

Golden Rule

If you're unsure about a file or directory, DO NOT DELETE IT. Research first or consult with experienced administrators. When in doubt, leave it alone.

๐Ÿ”ง The du Command: A Powerful Troubleshooting Tool

The du command is invaluable for troubleshooting disk space issues. It allows you to:

  • Navigate systematically: Start from root (/) and drill down into specific directories
  • Identify storage hogs: Quickly locate directories consuming excessive space
  • Target cleanup efforts: Focus your attention on the largest consumers of storage
  • Track changes over time: Compare directory sizes before and after cleanup operations

Workflow Example:

Start at /
Identify largest dirs
โ†’
Navigate to /var
Drill deeper
โ†’
Check /var/log
Find old logs
โ†’
Safe Cleanup
Remove if appropriate

๐Ÿ“‹ Complete Maintenance Checklist

Weekly/Bi-weekly Tasks

  1. Monitor disk usage:
    df -h
  2. Take a screenshot for historical tracking
  3. If usage exceeds 60%, proceed with cleanup:

Cleanup Procedure (When Needed)

  1. Remove unused packages and clean cache:
    sudo apt autoremove && sudo apt clean
  2. Clean old system logs:
    sudo journalctl --vacuum-time=1days
  3. Verify results:
    df -h
  4. If more space is needed, analyze directories:
    cd /
    sudo du -ah --max-depth=1 | sort -h
  5. Navigate to large directories and investigate further
  6. Remove only files you're certain are safe to delete

๐Ÿ“Š Decision Matrix for Disk Space Management

Disk Usage Status Action Required Urgency
0-50% โœ“ Healthy Regular monitoring only Low
51-60% โš Normal Continue monitoring, prepare for cleanup Low
61-75% โš  Warning Perform cleanup procedures Medium
76-85% โš  High Usage Immediate cleanup + directory analysis High
86-95% ๐Ÿ”ด Critical Emergency cleanup + consider expansion Critical
96-100% ๐Ÿ”ด Emergency Immediate action - server crash imminent Emergency

๐Ÿ’ก Best Practices Summary

Key Recommendations

  • Regular monitoring: Check disk usage every 2-3 weeks
  • Historical tracking: Maintain screenshots to identify trends
  • Proactive cleanup: Begin cleanup when usage reaches 60%
  • Automated tasks: Set up cron jobs for automatic package cleanup
  • Log rotation: Ensure system logs are properly rotated and archived
  • Safety first: Never delete system files or directories you don't understand
  • Research first: When in doubt, investigate before taking action
  • Backup important data: Always maintain backups before major cleanups
  • Document changes: Keep records of what was cleaned and when

๐ŸŽฏ Conclusion

Proper disk space administration is essential for maintaining a healthy, stable server environment. By following this guide and implementing regular monitoring and cleanup procedures, you can prevent disk space issues before they become critical problems. The du command is your most valuable tool for identifying storage issues and targeting your cleanup efforts effectively.

Remember: Prevention through regular monitoring is far easier than recovery from a crashed server due to full disk space.