How to Check and Free Up Disk Space on a Linux Server

linux, disk space, disk usage, server maintenance, troubleshooting, df, du, log cleanup, system administration 0 Помог ли вам данный ответ?

Problem Description

Running out of disk space on a Linux server is a common issue that can lead to application failures, database crashes, email delivery problems, and even prevent you from logging in via SSH. Symptoms of a full or nearly full disk include:

  • Services or websites suddenly going offline
  • Error messages such as No space left on device
  • Inability to write log files or upload files
  • MySQL or MariaDB failing to start
  • Emails bouncing back to senders

This article walks you through identifying disk usage and safely reclaiming space on your Linux server.

Solution Steps

Step 1: Check Overall Disk Usage

Connect to your server via SSH and run the following command to see a summary of all mounted filesystems:

df -h

The -h flag displays sizes in a human-readable format (e.g., GB, MB). Look at the Use% column to identify partitions that are full or nearly full. Any partition at or above 90% should be addressed promptly.

Step 2: Identify Which Directories Consume the Most Space

Navigate to the root directory and run the following to see the largest top-level directories:

du -sh /* 2>/dev/null | sort -rh | head -20

This command summarizes disk usage for each top-level directory, sorts the results from largest to smallest, and displays the top 20 entries. You can drill down further into any suspicious directory. For example, if /var is large:

du -sh /var/* 2>/dev/null | sort -rh | head -20

Step 3: Find Large Files on the System

To locate individual files that may be consuming excessive space, run:

find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null | sort -k5 -rh

This searches for files larger than 100 MB anywhere on the system and sorts them by size. Adjust the +100M value to search for smaller or larger files as needed.

Step 4: Clean Up Common Space Consumers

Below are the most frequent sources of wasted disk space and how to address them:

Old Log Files

Log files in /var/log/ can grow very large over time. Check their sizes with:

du -sh /var/log/* | sort -rh | head -10

You can safely clear a log file without deleting it by running:

cat /dev/null > /var/log/large-logfile.log

Alternatively, remove old rotated log archives:

rm -f /var/log/*.gz /var/log/*.1 /var/log/*.old

Package Manager Cache

Cached package files can accumulate over time. Clean them with the appropriate command for your distribution:

  • CentOS / AlmaLinux / Rocky Linux: yum clean all or dnf clean all
  • Ubuntu / Debian: apt-get clean and apt-get autoremove

Old Kernel Versions

Previous kernel versions can take up significant space in the /boot partition. On Ubuntu/Debian, remove old kernels with:

apt-get autoremove --purge

On CentOS/AlmaLinux/Rocky Linux, you can remove old kernels manually:

package-cleanup --oldkernels --count=2

Always keep at least one known working kernel as a fallback.

Temporary Files

Clean up the /tmp directory by removing files that are not currently in use:

find /tmp -type f -atime +7 -delete

This deletes files in /tmp that have not been accessed in the last 7 days. Adjust the +7 value as needed.

User Trash and Home Directories

If multiple users have accounts on the server, check their home directories and trash folders:

du -sh /home/* | sort -rh

rm -rf /home/*/.local/share/Trash/*

Step 5: Verify the Results

After performing the cleanup steps above, run df -h again to confirm that disk space has been reclaimed. Aim to keep usage below 85% for optimal system performance and stability.

Additional Tips

  • Set up monitoring: Use monitoring tools or simple cron-based scripts to alert you when disk usage exceeds a threshold (e.g., 80%). A basic example cron entry: 0 */6 * * * df -h / | awk 'NR==2 {gsub(/%/,""); if ($5 > 80) print "Disk usage alert: " $5 "%"}' | mail -s "Disk Alert" [email protected]
  • Implement log rotation: Ensure logrotate is properly configured to rotate and compress logs automatically. Check the configuration at /etc/logrotate.conf and /etc/logrotate.d/.
  • Archive or offload old data: Consider moving old backups, archives, or media files to external storage or a remote backup server rather than keeping them on the primary disk.
  • Be cautious with deletion: Always verify what a file or directory contains before deleting it. When in doubt, back up the data first. Avoid deleting files you do not recognize, especially those outside of /var, /tmp, and /home.
  • Consider disk expansion: If you consistently run low on space despite regular cleanup, it may be time to upgrade your storage. Contact our support team to discuss available options for expanding your server's disk capacity.
  • Check for deleted but open files: Sometimes a file is deleted while a process still has it open, meaning the disk space is not freed. Run lsof +L1 to find such files. Restarting the associated process will release the space.

Помог ли вам данный ответ?

Отправить тикет

Если вам нужна помощь, отправьте тикет, и наша команда поддержки поможет вам.

Отправить тикет