Running out of disk space? How to check what is using up server space in Linux

Rollins Orlu By Rollins Orlu on Apr 22, 2013 in Security

If there is one thing you will surely be confronted with as a server administrator, it's the issue of disk space being used on on the server. Every day use of your server involves reading and writing data to different partitions and directories, as such disk usage is something you simply cannot run away from.

The longer your server stays online, the more disk space you can expect to be used up on the server. There are, however, times when you might notice abnormal disk usage, such as your server filling up much too quickly. The common culprits include: backup files, database files, log files that keep growing and aren't rotated, and Yes, Email data!

Here at DT, we are generally Linux/Open Source babies, so this article focuses primarily on Linux boxes, but could apply in principle to other OSes.

The Symptoms

What we often see for servers running out of disk space includes: login sessions terminated due to the app not being able to write session data to the server; SQL-related errors thrown back due to database management system (DBMS) not being able to write data to disk; or Web sites going offline altogether.

Fixes

The obvious first step would be to check that you are indeed running out of server space. In your Linux box, launch the command line or SSH in and run the following command:
df -h
This should return results similar to:
# df -h
Filesystem  Size Used Avail Use% Mounted on
/dev/simfs 30G 15G 16G 49% /
none 512M 4.0K 512M 1% /dev
Once you have determined that your server has indeed run out of space, you would want to check how much space is used by each top level directory. To do this, run:
du -sh /*
This command will return a directory listing with the sizes of all directories. You can then cd into those directories and again du -sh to find more. As you can imagine, this process could be quite laborious; you can save yourself the stress by narrowing your search to the top directories i.e. the directories using up the most space on the server. This can be achieved by running:
du -xk | sort -n | tail -25
This command will return a list of the top 25 largest files and directories on the server. You can then check the contents of each of these directories to figure out what can be [safely] removed from the server to free up disk space.

Alternatively, if  you've run out of disk space, and can't do a disk based sort, the following command would help to cut down the size of the du output before doing the sort:

du -ax |  awk '{if($1 > 10240) print $1/1024 "MB" " " $2 }' | sort -rn | head -n 25

An ounce of Prevention ....

As the old adage goes, you might want to take a more proactive approach to managing disk usage on your server. One way of doing this is to use a disk utility -- these tools monitor your server and alert you when you go over a certain threshold (75%, 80%…). Here are a few:  

Comments

No comments yet.

Leave a comment

Comments are moderated. Your comment may appear after approval.