Chapter 1 · Lesson 8 of 19
Server basics

Check disk, memory and CPU usage

Commands like df, free and top show how much disk, memory and CPU a Linux server is using, so you can spot problems early.

By the end you will

Check disk, memory and CPU on a Linux server and tell if something is running low.

Time
12 min
Level
Beginner
Check
4 questions
You need
  • A Linux server you can reach over SSH

Most outages on a small server are not exotic. To check disk, memory and CPU on a Linux server, run df -h, free -h and uptime. The disk fills up with logs and images, memory runs out and the kernel kills your database, or something eats the CPU and the site crawls. Three commands, run in a minute, tell you which of these it is. Run them when something feels wrong, and also now and then when it does not.

How it works#

Disk, memory and CPU are three separate limited resources, and each fails differently.

Disk is space on a filesystem. A full disk means programs cannot write: databases stop, logs fail and apt breaks. Besides bytes, a filesystem has a fixed number of inodes, one per file. Millions of tiny files can use up inodes before bytes run out.

Memory is where running programs keep data. Linux uses memory that is not needed by programs as a cache for files, so the free column often looks small on a healthy server. The available column estimates what programs could get without swapping. When memory really runs out, the kernel’s out-of-memory killer ends a process. Swap is disk used as slow overflow memory (see add swap space on Ubuntu). A little use is fine, but heavy swapping makes a server crawl.

CPU is measured two ways. Usage is a percentage over a short interval. Load average counts processes that are running or waiting for a CPU, averaged over 1, 5 and 15 minutes. Compare load to the core count: one busy core means a load of about 1 on a 1 core machine, which is full, but only a quarter used on a 4 core machine. Load also counts processes stuck waiting for disk, so a high load with an idle CPU often points at slow storage.

A worked example: a site starts returning errors. You run df -h and see / at 100%. You run sudo du -h --max-depth=1 /var | sort -h and the biggest line is /var/lib/docker. You run docker system df and find that old images take most of it. You free space on purpose, and df -h drops back. The sequence is always the same: confirm, locate, act.

Step 1: Check disk space#

bash
df -h

-h prints sizes in human readable units. Look at the Use% column for the line mounted on /.

What you should see: a table with Filesystem, Size, Used, Avail, Use% and Mounted on. Several entries such as tmpfs are in memory and can be ignored.

To find what is using the space inside a folder:

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

--max-depth=1 lists each subfolder one level down, and sort -h orders human readable sizes. The biggest are at the bottom. Repeat on the largest folder to drill down.

What you should see: a list of sizes and paths ending in the total for /var.

Step 2: Check memory#

bash
free -h

Read the available column. It is the memory that programs can still use. The Swap row shows disk space used as emergency memory, and 0B total means no swap is set up.

What you should see: Mem: and Swap: rows with total, used, free, shared, buff/cache and available.

Step 3: Check CPU and load#

bash
nproc
uptime

nproc prints the number of CPU cores. uptime prints how long the server has run and three load averages (1, 5 and 15 minutes). If the load stays above the core count, the machine is overloaded.

What you should see: a single number from nproc, then one line from uptime ending with load average: and three values.

Step 4: Watch live usage#

bash
top

It shows the busiest processes first. Press q to quit. Press M to sort by memory and P to sort by CPU. If htop is installed (sudo apt install htop), it gives a friendlier view with colors and scrolling.

What you should see: a header with load, CPU and memory, then a process table that refreshes every few seconds.

Check it worked#

  • df -h prints a table with a / line and a Use% value.
  • free -h prints Mem: and Swap: rows.
  • nproc prints a single number.
  • top fills the screen with a process list and refreshes every few seconds.

For a one-line summary of the worst offenders, run ps aux --sort=-%mem | head -n 5. It should list the header and the four processes using the most memory.

Edge cases and gotchas#

  • Small free is normal. A server with 1 GB can show almost nothing free and still be healthy, because the cache will shrink on demand. Look at available and at swap activity.
  • Disk can be full while du finds little. A deleted log still takes space while a process keeps it open. sudo lsof +L1 lists such files, and restarting the process frees the space. Also try df -i for inodes.
  • Docker uses a lot of disk. Images, stopped containers and build cache live under /var/lib/docker, and du in other folders will not find them. Use docker system df to see them, and read free disk space with docker prune safely.
  • Containers have their own limits. free inside a container may show the host’s memory even when the container is limited. Check the container limits with Docker tools.
  • A burst is not a trend. One top snapshot during a backup can mislead. Compare the 1, 5 and 15 minute load values to see the trend.
  • Memory shown by top per process can double count shared memory. Do not add the numbers up. Use available for the overall picture.
  • Mounted volumes show as separate lines. Data on another disk will not appear under /. Read the Mounted on column.
  • Reserved blocks mean a full disk at 95%. Some filesystems reserve space for root, so Avail can reach zero while Use% is under 100.

Troubleshooting#

SymptomLikely causeFix
No space left on deviceDisk full or out of inodesCheck df -h and df -i, then find the big folder with du
du: cannot read directory with Permission deniedYour user cannot read some foldersAdd sudo
htop: command not foundNot installed by defaultInstall with apt or use top
Processes vanish and the log says Out of memory: Killed processThe kernel killed a processAdd swap, lower memory use or upgrade the plan
Load is high but CPU looks idleProcesses wait on slow diskCheck disk use and top for the wa value
Killed printed by a commandThe out-of-memory killer ended itSame as above, and read journalctl -k
df: ...: Permission deniedA mount is not readableAdd sudo or ignore that mount
Swap is full and the server crawlsMemory use is above RAMFind the process with top, then add RAM or fix the leak

Real-world scenario#

Ravi gets an alert that his site is slow. He logs in and runs uptime: the load is 9 on a 2 core server. top shows a backup script using all of the CPU and heavy swap. free -h says available is almost zero.

He kills the script, and the load falls within a minute. Then he reads the script, sees that it compresses the whole database in memory, and changes it to stream to disk. He also adds swap as a safety net.

The three commands told him which resource was the problem. Without them he might have upgraded the plan and not fixed anything.

Quick reference#

QuestionCommand
How full is the diskdf -h
Which folder is bigsudo du -h --max-depth=1 /var | sort -h
Are inodes used updf -i
How much memory is availablefree -h
How many coresnproc
How busyuptime
Who uses memoryps aux --sort=-%mem | head

Security and safety notes#

  • Do not delete files in /var/lib, /etc or /usr to free space. Services depend on them.
  • Do not remove a log file a service has open and expect the space back. Truncate or rotate it instead.

Next#

Read logs with journalctl.

Try it: Check your server's health

Look at disk, memory and CPU with the standard commands.

practice, nothing runs on your machine

Want a real machine? Free browser labs: Killercoda

Check what you learned4 questions
1Which column of free -h shows memory programs can still use?
2A 4 core server shows a load average of 6.00. What does that suggest?
3What should you do before deleting files on a nearly full disk?
4df -h shows 40% used but writes fail with No space left on device. What do you check?

Frequently asked questions

What does a load average of 1.00 mean?

On a machine with one CPU core it means the core was busy all the time. Compare the load to the core count from nproc, because a 4 core machine has room for about 4.

Why does free show little free memory?

Linux uses spare memory as cache, which it gives back when programs need it. Read the available column, not the free column.

The disk shows full but du finds little. Where did the space go?

A deleted file can stay on disk while a process holds it open, or the filesystem can be out of inodes. Check df -i and restart the process that holds the file.

Is high CPU always a problem?

No. A build or backup can use all cores for a while. It is a problem when it stays high with no reason, or when the load stays above the core count.

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios