# Server review: a five-minute health check

Check any Linux server in a fixed order: load, memory, disk, services, ports and logins. Know what each number means and what to do when one is wrong.

Course: [Linux & Bash for DevOps](https://devops.rajeev.pro/courses/linux), lesson 6. About 30 minutes, beginner. Source: https://devops.rajeev.pro/courses/linux/06-server-review

## What you will learn

- Run a complete health check in a fixed order, in about five minutes
- Read load average against CPU count, and memory as "available", not "free"
- Find what's filling a disk, including deleted files that still hold space
- List running and failed services and the ports they listen on
- Read the auth log and login history to see who has been on the server
- Tell CPU, disk and memory trouble apart with vmstat, iostat and sar

## Always check in the same order

When a server is "slow" or "acting up", random changes can hide the cause or make the incident worse. A
repeatable first pass records evidence before restarting services or changing configuration. Start with the
machine identity and uptime, then check CPU/load, memory, disks, services and recent activity.

This order moves from broad symptoms to narrower causes. It keeps two engineers from collecting unrelated
facts and makes it easier to compare a healthy machine with the one that is failing. Write down the values and
time you checked them; a single sample may miss a short spike.

> **Figure.** The five-minute health check in order. One, identity and uptime: hostname and uptime. Two, load: uptime and nproc. Three, memory: free -h. Four, disk: df -h, then du to find what's big. Five, services and ports: systemctl --failed and ss -tlnp. Six, logins and logs: auth log, last and journalctl.
>
> Same order, every server, every time. Write down anything that looks wrong as you go.

First, make sure you're on the right machine and see how long it's been up:

```sh
$ hostnamectl | head -n 3          # name, and whether it's a VM
$ uname -r                         # kernel version
6.8.0-48-generic
$ uptime
 10:42:19 up 23 days,  4:11,  2 users,  load average: 0.52, 0.48, 0.41
```

"Up 3 minutes" when nobody rebooted it is a finding in itself: something restarted the machine.

> **Video.** [Performance Analysis: The USE Method](https://www.youtube.com/watch?v=K9w2cipqfvc) (Deirdré Straughan)

## 1. Is it overloaded?

The three numbers at the end of `uptime` are the **load average** over 1, 5 and 15 minutes. They estimate the
average number of tasks running or waiting for CPU or uninterruptible I/O. The value is a count, not a
percentage, so compare it with the number of CPU cores:

```sh
$ nproc
4
```

| Load on 4 cores | Means                                         |
| --------------- | --------------------------------------------- |
| below 4         | there's spare capacity                        |
| about 4         | every core is busy, nothing waiting           |
| well above 4    | tasks are queueing: users feel it as slowness |

Read the three numbers as a trend: `3.9, 2.1, 0.8` is climbing right now, `0.8, 2.1, 3.9` is
calming down. Linux also counts tasks waiting for the disk, so high load with low CPU use often
means slow storage, not a busy processor. You'll find the busy processes in the next lesson.

### What load really counts

Load is not a percentage. It's a count of tasks that want to run, averaged over time: the ones on a core
now, the ones in the **run queue** waiting for a free core, and the ones stuck waiting for the disk (state
`D`, "uninterruptible sleep").

> **Figure.** Animated. A server has 4 CPU cores. First, 2 tasks run and 2 cores are idle: load about 2, spare capacity. Then 4 tasks run, one per core, and nothing waits: load about 4, every core busy. Then 4 tasks run, 2 more wait in the run queue for a free core, and 1 waits for the disk in state D: load about 7, so tasks are queueing and users feel the server as slow.
>
> Load = running + waiting for a core + waiting for the disk. Above nproc, something is always waiting.

Here's a 4-core test machine, before and 45 seconds after starting six CPU-hungry processes:

```sh
$ uptime
 12:13:59 up  2:06,  0 user,  load average: 5.47, 8.99, 7.45
$ nproc
4
$ uptime
 12:14:45 up  2:07,  0 user,  load average: 7.01, 8.91, 7.50
```

The 1-minute number moved from 5.47 to 7.01 while the 15-minute one barely changed: the averages smooth
things out, so a spike shows in the first number long before the last. And 7 on 4 cores means three tasks,
on average, were always waiting their turn.

## 2. Memory

Low `free` memory alone does not mean the server is out of RAM. Linux uses spare memory for filesystem cache
and can reclaim much of it when applications need it. The useful field in `free -h` is **available**, an
estimate of memory that can be used without swapping heavily.

```sh
$ free -h
               total        used        free      shared  buff/cache   available
Mem:           3.7Gi       1.4Gi       316Mi       3.2Mi       2.1Gi       2.3Gi
Swap:          1.0Gi       283Mi       740Mi
```

Look at **available**, not **free**. Linux uses spare RAM as a disk cache (`buff/cache`) and gives
it back as soon as programs need it, so a small "free" is normal and healthy.

Worry when **available** is close to zero, or **swap used** keeps growing: the server is moving
memory to disk, which is very slow. If memory runs out completely, the kernel's OOM killer stops a
process to save the machine, and `journalctl -k | grep -i oom` shows which one.

## 3. Disk

Disk problems can come from full bytes, exhausted inodes or a large file still held open after deletion.
`df -h` reports space by mounted filesystem; `df -i` reports inode availability. Use `du` to find large
directories and `find` to locate individual large files. Restricting a search to one filesystem avoids
wandering into mounted network shares or other disks.

```sh
$ df -h                           # space per filesystem
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        40G   38G  2.0G  96% /
$ df -i                           # inodes: the number of files. Can run out with space left
$ sudo du -xh --max-depth=1 /var | sort -h | tail -5    # which folders are big
$ sudo find / -xdev -type f -size +500M -exec ls -lh {} \; 2> /dev/null   # big single files
```

`-x` (and `-xdev`) keeps the search on one filesystem, so you don't wander into network shares.
The usual suspects are logs (`/var/log`), old releases, container images (`/var/lib/docker`) and
forgotten backups.

Before deleting anything, identify the owner and purpose of the data and check whether a process still has it
open. `lsof +L1` lists open files whose directory entry has been removed. Restart or reload the owning service
only after checking its operational impact. A common mistake is deleting files until `df` changes without
checking for an open file or inode exhaustion.

> **You deleted the file and df didn't change.** A deleted file keeps its space while a running process still has it open, which happens all the time with logs. `sudo lsof +L1` lists deleted files that are still open, and which process holds each one. Reload or restart that service to release the space. To empty a live log without this problem, truncate it instead of deleting it: `sudo truncate -s 0 app.log`.

## Watching it live: vmstat, iostat and sar

`uptime`, `free` and `df` are snapshots. When the server is slow **right now**, you want numbers every
second, to see whether it's the CPU, the memory or the disk. Three tools from the same family do that.
`vmstat` comes with every distribution; `iostat` and `sar` are in the `sysstat` package
(`sudo apt install sysstat`).

### vmstat: the one-line overview

`vmstat 1 5` prints a line every second, five times. Ignore the first line: it's the average since boot.
This is the same machine with six busy processes on four cores:

```sh
$ vmstat 1 5
procs -----------memory---------- ---swap-- -----io---- -system-- -------cpu-------
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st gu
 9  0 1048316 255228  61228 1357400   13  161   625   544 2269   12 42 22 35  1  0  0
13  0 1048316 251576  61228 1357464    0    0    48     0 3706 10481 32 68  1  0  0  0
12  1 1048316 167960  61348 1357376    0    0     0  1360 6195 17821 46 54  0  0  0  0
19  0 1048316 135276  61424 1357712    0    0     0   696 4869 11751 45 55  0  0  0  0
10  0 1048316 140672  61496 1357888    0    0    16  1016 4415 10050 51 49  1  0  0  0
```

And the same machine writing a large file straight to disk:

```sh
$ vmstat 1 5
procs -----------memory---------- ---swap-- -----io---- -system-- -------cpu-------
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st gu
 4  0 1047976 163544  61748 1349000   13  160   621   810 2283   12 42 22 36  1  0  0
 2  1 1047960 163292  61764 1349380   32    0   400 568320 4694 8574 10 20 55 16  0  0
 4  0 1047952 155228  61780 1350464   12    0  1172 502784 4365 7433 32 24 31 13  0  0
 2  0 1047948 141368  61796 1351180    4    0   416 433152 3740 6555 48 22 20 10  0  0
 1  1 1043576 143636  61824 1351472 5292    0  5572 481440 4488 7384 15 26 37 22  0  0
```

| Column           | Means                                   | In the runs above                              |
| ---------------- | --------------------------------------- | ---------------------------------------------- |
| `r`              | tasks running or waiting for a CPU      | 10–19 on 4 cores: CPU-bound                    |
| `b`              | tasks blocked on I/O (the `D` state)    | 1 during the disk write                        |
| `si`, `so`       | memory swapped in and out per second    | `si` 5292 in one second: memory pressure       |
| `bi`, `bo`       | blocks read from and written to disk    | `bo` around 500,000 during the write           |
| `us`, `sy`, `id` | CPU % in user code, in the kernel, idle | `id` 0–1 in the first run: no spare CPU        |
| `wa`             | CPU % idle **while waiting for I/O**    | 10–22 during the write, 0 in the CPU-bound run |

Read it in that order: is `r` above the core count, is `b` or `wa` high, is `si`/`so` non-zero? That tells
you which of the next tools to open.

### iostat: which disk, and how busy

`iostat -x` shows each disk's work. Its full output is wide, so pick the columns that matter:

```sh
$ iostat -dxy sdd 1 1 | awk '/Device|sdd/ {print $1, $2, $8, $9, $12, $(NF-1), $NF}' | column -t
Device  r/s   w/s     wkB/s      w_await  aqu-sz  %util
sdd     2.00  392.00  401408.00  1.63     0.65    65.20
```

| Column       | Means                                                           |
| ------------ | --------------------------------------------------------------- |
| `r/s`, `w/s` | reads and writes per second                                     |
| `wkB/s`      | kilobytes written per second: here about 400 MB/s               |
| `w_await`    | average milliseconds per write, including the wait in the queue |
| `aqu-sz`     | average number of requests queued on the disk                   |
| `%util`      | how much of the time the disk was busy                          |

`-y` skips the since-boot report and `-d` hides the CPU part. The number to watch is **`await`**: a few
milliseconds is fine for SSDs, tens or hundreds means requests are queueing. `%util` near 100% used to mean
"disk saturated", but SSDs and cloud volumes serve many requests in parallel, so trust `await` and
`aqu-sz` more.

### sar: the same numbers, and their history

`sar` reports CPU (`-u`), run queue and load (`-q`), memory (`-r`), disks (`-d`) and network (`-n DEV`):

```sh
$ sar -q 1 3
Linux 6.6.87.2-microsoft-standard-WSL2 (web-01) 	10/18/26 	_x86_64_	(4 CPU)

12:14:49      runq-sz  plist-sz   ldavg-1   ldavg-5  ldavg-15   blocked
12:14:50           10       704      7.57      9.00      7.54         0
12:14:51           12       694      7.57      9.00      7.54         0
12:14:52            8       695      7.57      9.00      7.54         0
Average:           10       698      7.57      9.00      7.54         0
$ sar -u 1 2
Linux 6.6.87.2-microsoft-standard-WSL2 (web-01) 	10/18/26 	_x86_64_	(4 CPU)

12:15:40        CPU     %user     %nice   %system   %iowait    %steal     %idle
12:15:41        all     16.83      0.00     33.67     18.34      0.00     31.16
12:15:42        all     36.83      0.00     21.74     13.30      0.00     28.13
Average:        all     26.74      0.00     27.76     15.84      0.00     29.66
```

Live, it overlaps with `vmstat`. Its real value is **history**. Turn on collection once
(`sudo systemctl enable --now sysstat`, and `ENABLED="true"` in `/etc/default/sysstat` on Ubuntu), and it
records every 10 minutes into `/var/log/sysstat/`. Then "the site was slow at 3 a.m." stops being a mystery:
`sar -q -s 02:30:00 -e 03:30:00` shows the load at that hour, and `sar -f /var/log/sysstat/sa17` reads
another day.

`%steal` is worth knowing on cloud VMs: time your VM wanted the CPU but the hypervisor gave it to someone
else. Steady steal on a burstable instance (like `t3`) usually means its CPU credits ran out.

### free, once more

```sh
$ free -h -w
               total        used        free      shared     buffers       cache   available
Mem:           3.7Gi       2.3Gi       267Mi        93Mi        60Mi       1.3Gi       1.4Gi
Swap:          1.0Gi       1.0Gi       476Ki
```

`-w` splits `buff/cache` into `buffers` and `cache`. **available** (1.4 GiB) is still fine here, but **swap
is full**: 1.0 GiB used of 1.0 GiB. Together with the `si` spike in `vmstat`, that says this machine has been
short of RAM at some point and pushed memory out to disk. `free -h -s 2` repeats every two seconds if you want
to watch it change.

| Symptom                             | Look at                                     |
| ----------------------------------- | ------------------------------------------- |
| load high, `r` high, `id` near 0    | CPU: `top`, `pidstat 1` to find the process |
| load high, `b` and `wa` high        | disk: `iostat -x 1`, then `sudo iotop`      |
| `si`/`so` non-zero, swap growing    | memory: `free -h`, `ps aux --sort=-rss`     |
| everything fine now, slow "earlier" | `sar` history for that hour                 |

> **Video.** [Linux Performance Analysis in 60 seconds](https://www.youtube.com/watch?v=ZdVpKx6Wmc8) (Brendan Gregg)

## 4. Services and ports

When the machine and resources look healthy, check whether required services failed and whether the expected
ports are listening. `systemctl --failed` summarizes failed systemd units. `ss -tlnp` shows listening TCP
sockets, numeric ports and owning processes when permissions allow. A service can be active but listen only
on loopback, or listen on the wrong port, so compare the result with the intended configuration.

Do not expose a port just because it is listening. Confirm the host firewall, cloud security group and service
bind address. A common mistake is interpreting "port is open locally" as proof that a remote client can reach
it; network routes and firewalls can still block the connection.

```sh
$ systemctl --failed                                  # anything that crashed
  UNIT          LOAD   ACTIVE SUB    DESCRIPTION
● myapp.service loaded failed failed My App

$ systemctl list-units --type=service --state=running # what's running
$ sudo ss -tlnp                                        # what's listening, and which process
State  Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0      511          0.0.0.0:80        0.0.0.0:*     users:(("nginx",pid=812,fd=6))
LISTEN 0      4096         0.0.0.0:22        0.0.0.0:*     users:(("sshd",pid=701,fd=3))
LISTEN 0      4096       127.0.0.1:5432      0.0.0.0:*     users:(("postgres",pid=944,fd=7))
```

Read the Local Address column carefully. `0.0.0.0` means the port accepts connections from
**anywhere**; `127.0.0.1` means only from this machine. A database listening on `0.0.0.0` is a
finding. The _Services with systemd_ lesson shows how to read why a service failed.

## 5. Who has been here?

```sh
$ who                     # who's logged in now
$ w                       # ... and what they're running
$ last -n 5               # recent logins (from /var/log/wtmp)
$ sudo lastb -n 5         # recent FAILED logins
$ sudo grep -c "Failed password" /var/log/auth.log              # brute-force attempts
$ sudo grep "Accepted" /var/log/auth.log | tail -n 5            # successful SSH logins
$ sudo journalctl -u ssh --since "1 hour ago"                   # the same, from the journal
```

Authentication events (SSH logins, `sudo` use, password changes) go to `/var/log/auth.log` on
Debian and Ubuntu, and `/var/log/secure` on Red Hat, Rocky and Amazon Linux. Every sudo command
is logged there with the user's name, which is one more reason to use sudo instead of a root shell.

Thousands of `Failed password` lines from many addresses are normal for any server on the
internet: bots try common passwords all day. They matter when a password login **succeeds** from
an address you don't know, which is why servers should use SSH keys and disable password logins.

### Logs in general

```sh
$ sudo journalctl -p err -b         # errors since the last boot
$ sudo journalctl -u nginx -n 50    # the last 50 lines from one service
$ ls -lh /var/log                   # classic log files
$ sudo tail -f /var/log/syslog      # everything, live (Debian/Ubuntu)
```

Old logs are rotated and compressed by **logrotate** (`/etc/logrotate.d/`), which is why you see
`syslog.1` and `syslog.2.gz`. If a disk keeps filling with logs, a missing or broken logrotate
rule is the usual cause.

> **In production.** Write your findings down as you go, with the command and the time. "Load 12 on 4 cores at 10:42, disk 96% on /, myapp.service failed" turns into a clear incident note, and the next person doesn't have to repeat your checks.

## Mission: Write a health report for a server

You've just been given SSH access to a server nobody has looked at in months. Run the health check and save a short report with the load next to the core count, available memory, the fullest disk, failed services, listening ports and the last five logins.

- You need: Any Linux server or VM you can SSH into
- You'll have: ~/review.txt

```sh
$ cat > ~/review.sh <<'EOF'
#!/bin/bash
echo "== $(hostname) at $(date '+%F %T')"
echo "-- load (cores: $(nproc))"; uptime
echo "-- memory"; free -h
echo "-- disk"; df -h -x tmpfs -x devtmpfs
echo "-- failed services"; systemctl --failed --no-legend
echo "-- listening"; sudo ss -tlnp
echo "-- last logins"; last -n 5
EOF
$ bash ~/review.sh > ~/review.txt 2>&1
$ less ~/review.txt
```

One redirect captures every check, output and errors. Keep both files: next time something feels
wrong, run the script again into a new file and compare the two with `diff`. What changed is
usually where the problem is.

## Try it

- Run `uptime` and `nproc`, and say whether the load is high for this machine.
- Run `free -h` and explain the difference between `free` and `available`.
- Find the five biggest folders under `/var` with `sudo du -xh --max-depth=1 /var | sort -h | tail -5`.
- List failed services with `systemctl --failed` and listening ports with `sudo ss -tlnp`.
- Count failed SSH logins in the auth log with `grep -c "Failed password"`.
- Run `vmstat 1 5` while compressing a big file, and find which columns change.
- Run the mini mission and read your `~/review.txt`.

## Quiz

1. A 4-core server shows "load average 3.9, 3.5, 2.1". What does that tell you?
   - It's overloaded right now
   - **It's busy but coping: about one task per core, and the load has been rising over 15 minutes** (answer)
   - Load average is a percentage, so it's at 3.9% use
   Why: Compare load with the number of cores (nproc). Up to 4 on 4 cores means work isn't queueing yet. The three numbers are 1, 5 and 15 minute averages, so it's rising.

2. free -h shows only 200 MB "free" but 2.1 GB "available". Is the server short of memory?
   - Yes, add RAM
   - **No: Linux uses spare RAM as disk cache and gives it back when programs need it; available is the number that matters** (answer)
   - Only if swap is zero
   Why: Unused RAM is wasted, so Linux fills it with cache (buff/cache). "available" estimates what programs can still get.

3. df says / is 100% full. You delete a 20 GB log file, but df still says 100%. Why?
   - df is cached for an hour
   - **A running process still has the deleted file open, so the space isn't freed until it closes the file** (answer)
   - Deleting files never frees space
   Why: A deleted file keeps its space while any process holds it open. sudo lsof +L1 lists such files; restarting or reloading that service releases the space.

4. Where do you look first to see failed SSH login attempts on Ubuntu?
   - **/var/log/auth.log (or journalctl -u ssh)** (answer)
   - /etc/passwd
   - ~/.bash_history
   Why: Authentication events go to /var/log/auth.log on Debian and Ubuntu and /var/log/secure on Red Hat family systems. On systemd machines, journalctl has them too.

5. What does sudo ss -tlnp show?
   - Running processes sorted by CPU
   - **TCP ports the server is listening on, and which process owns each one** (answer)
   - Disk usage per folder
   Why: -t TCP, -l listening, -n numbers instead of names, -p the process. It answers "what is this server exposing?".

6. A 4-core server has load 8. vmstat shows r 1, b 6, id 60, wa 35. What is the bottleneck?
   - **The disk: tasks are blocked waiting for I/O, while the CPUs sit mostly idle** (answer)
   - The CPU: load is twice the core count
   - The network
   Why: Load counts tasks waiting for the disk too. High b and wa with plenty of idle CPU points at storage; check iostat -x for await.

7. Users say the site was slow at 3 a.m. It's fine now. Which tool can show what the server looked like then?
   - vmstat 1 5
   - top
   - **sar, with sysstat collection turned on, for example sar -q -s 02:30:00 -e 03:30:00** (answer)
   Why: vmstat and top only show the present. sar reads the history that sysstat records every 10 minutes.

## Resources

- [free(1) manual](https://manpages.ubuntu.com/manpages/noble/man1/free.1.html) (Ubuntu manpages)
- [ss(8) manual](https://manpages.ubuntu.com/manpages/noble/man8/ss.8.html) (Ubuntu manpages)
- [journalctl(1) manual](https://manpages.ubuntu.com/manpages/noble/man1/journalctl.1.html) (Ubuntu manpages)
- [Linux load averages: solving the mystery](https://www.brendangregg.com/blog/2017-08-08/linux-load-averages.html) (Brendan Gregg)
- [Linux performance analysis in 60,000 milliseconds](https://www.brendangregg.com/Articles/Netflix_Linux_Perf_Analysis_60s.pdf) (Brendan Gregg (Netflix))
- [LPIC-1 exam 102, objectives 108.2 and 110.1](https://wiki.lpi.org/wiki/LPIC-1_Objectives_V5.0) (Linux Professional Institute)
