Linux
Intermediate 13 min read Linux

System Monitoring & Resource Usage

Monitoring is observing a system's live state — CPU, memory, disk and network — so you catch problems before they become outages. The Linux toolbox covers every layer: top and htop for live processes, vmstat and free for memory, df and du for disk, iostat for I/O, ip and ss for networking, and tail, journalctl and dmesg for logs and the kernel ring buffer.

Linux monitoringtophtopvmstatfreedfduiostatipsspingtraceroutenslookupjournalctldmesg
Introduction

What is this?

Monitoring is the practice of observing a system's live state — CPU, memory, disk and network — so you can spot problems before they become outages. Linux ships a purpose-built command for every layer: top and htop for live process activity, vmstat and free for memory pressure, df and du for disk space, iostat for I/O, ip and ss for networking, and tail, journalctl and dmesg for logs and kernel messages. A runbook is only as good as your ability to read these instruments.

Motivation

Why do you need it?

  • Answer 'why is the site slow?' in minutes instead of guessing blindly.
  • Establish a baseline of normal usage so anomalies stand out when they matter.
  • Right-size capacity before a growth spike turns into an outage.
  • Correlate symptoms across layers: a slow app is often a disk or memory problem elsewhere.
  • The exact same commands work on bare metal, VMs, containers and cloud instances.
Think about it

Mental model

Imagine

An aircraft cockpit instrument panel

Think of

Linux monitoring commands

A pilot never flies by feel — gauges show fuel, altitude and engine RPM. top is the engine monitor, free is the fuel gauge, df and du are the cargo weight, ip and ss are navigation and radio, dmesg is the maintenance log. You cannot run a reliable system blind; these instruments tell you when to act.

Mechanics

How it works

  • top reads /proc every few seconds and shows which processes are burning CPU and memory, sorted live, while htop adds colour and easier navigation.
  • vmstat samples kernel counters: running and blocked processes, memory, swap, I/O and the us/sy/id/wa CPU breakdown.
  • free reads /proc/meminfo to report total, used and available memory — with -m in megabytes — while df and du walk the filesystem for disk space.
  • Network tools inspect the stack: ip a lists addresses, ss lists listening and connected sockets, and ping, traceroute and nslookup test reachability and DNS.
  • For logs, tail -f streams a file, journalctl -f streams the systemd journal, and dmesg shows kernel ring-buffer messages from drivers and hardware.

Which monitoring command covers which layer of a Linux system.

Hardware
Kernel counters
Resources
Network
Applications & logs
Reference

Key commands

top

Live view of processes sorted by CPU, plus load average and CPU/memory summary.

top - 09:20:15 up 3 days,  4:12,  1 user,  load average: 2.10, 1.80, 1.55
Tasks: 214 total,   2 running, 212 sleeping,   0 stopped,   0 zombie
%Cpu(s): 25.3 us,  2.1 sy,  0.0 ni, 71.9 id,  0.7 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7977.6 total,    812.4 free,   5240.1 used,   1925.1 buff/cache

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
   4567 appuser   20   0 1648564 102456  17472 S  98.7   1.3 142:23.45 node /app/server.js
htop

Colourful, interactive top with mouse support and easier process management.

vmstat 2 5

Sample system stats every 2 seconds, 5 times — run queue, memory, swap, CPU.

procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0     61    812    321   1604    0    0    12    18  205 1300 25  2 72  1  0
 3  0     61    800    321   1604    0    0     0    44 1204 1389 48  3 49  0  0
free -m

Memory totals in megabytes — including the available column that really matters.

               total        used        free      shared  buff/cache   available
Mem:            7977        5240         812          56        1925        2513
Swap:           2048          61        1987
df -h

Free and used space on every mounted filesystem, human-readable.

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        49G   22G   25G  47% /
du -sh /var

Total size of a directory in human-readable form.

2.3G    /var
iostat -x 2 3

Detailed per-disk I/O stats — throughput, await and utilisation.

Device            r/s     w/s   rkB/s   wkB/s  await  %util
sda              12.4     8.2   128.0    96.0   0.80   1.6
ip a

List every network interface with addresses, MTU and state.

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
ss -tulpn

Show listening and connected TCP/UDP sockets with the owning process.

Netid State   Recv-Q Send-Q Local Address:Port  Peer Address:Port  Process
tcp   LISTEN  0      511     0.0.0.0:80           0.0.0.0:*         users:(("nginx",pid=4567,fd=6))
tcp   LISTEN  0      128     0.0.0.0:22           0.0.0.0:*         users:(("sshd",pid=1102,fd=3))
ping -c 4 example.com

Send 4 ICMP echo requests to test reachability and round-trip latency.

PING example.com (93.184.215.14) 56(84) bytes of data.
64 bytes from 93.184.215.14: icmp_seq=1 ttl=57 time=41.2 ms
--- example.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss
traceroute example.com

Map every hop between you and the target to find where latency lives.

nslookup example.com

Query DNS to resolve a hostname or inspect name-server records.

tail -f /var/log/syslog

Follow a log file live, printing each new line as it is written.

journalctl -f

Follow the entire systemd journal across all services and the kernel.

dmesg | tail -20

Read the latest kernel ring-buffer messages — hardware and driver events.

Hands-on

Examples

Investigate a slow server in five commands

Investigate a slow server in five commands
# 1. Who is eating the CPU?
top -b -n 1 | head -15
# 2. Is memory or swap the bottleneck?
free -m
# 3. Are processes queueing for the CPU?
vmstat 1 3
# 4. Is the disk full?
df -h
# 5. What is listening on which ports?
ss -tulpn

Run these in order and you separate the symptom (slow response) into a cause: a CPU-hogging process, memory pressure with swap thrashing, a full disk blocking writes, or a port/socket problem. Each tool answers one layer; the art is correlating them.

Real world

In production

  • First-response diagnosis on any incident starts with top, free -m, df -h and ss -tulpn — four commands that localise most outages.
  • Baselines captured with vmstat and free feed capacity planning, so you scale before the pager goes off.
  • Alerting rules (e.g. Nagios, Prometheus) are just automated versions of these checks: CPU saturation, memory pressure, disk filling.
  • Kubernetes and cloud consoles expose the same kernel counters, so the mental model transfers from a single VM to a fleet.
  • ss -tulpn is the standard answer to 'what is listening on port 80 and which process owns it?' in any audit or runbook.
Guidelines

Best practices

  • Collect a baseline when the system is healthy — you cannot spot an anomaly without knowing what normal looks like.
  • Watch the available column of free -m, not free: buff/cache memory is reclaimable and not wasted.
  • In vmstat, focus on the r column (run queue) and the wa (I/O wait) and swap si/so columns — they reveal the real bottleneck.
  • Scope log streams: journalctl -u <service> -f beats tailing the whole journal, and dmesg is for kernel/hardware issues only.
  • Tie every check to a layer (CPU, memory, disk, network) so your diagnosis is a deliberate elimination, not a lucky guess.
Watch out

Common mistakes

  • Panicking because free -m shows low 'free' memory, ignoring that buff/cache is reclaimable — check available instead.
  • Treating a high load average as a CPU problem when it can also come from I/O wait or an overloaded run queue.
  • Using netstat (deprecated) instead of ss, or ipconfig instead of ip a.
  • Running top once and concluding, rather than sampling over time with vmstat 1 5 or watching top over a minute.
Root cause analysis

Common issues

Error
bash: vmstat: command not found
Root cause

A minimal container image without the procps package.

Fix

Install it with apt update && apt install -y procps (or dnf install procps-ng).

Error
ping: example.com: Temporary failure in name resolution
Root cause

DNS is not configured — /etc/resolv.conf is empty or unreachable.

Fix

Check cat /etc/resolv.conf and connectivity, then fix DNS config or use a reachable nameserver.

Error
free -m shows 0 free but the server is fine
Root cause

Memory is being used for buff/cache, which the kernel can reclaim instantly.

Fix

Read the available column — it is the real free memory. Ignore 'free' alone.

Error
Permission denied while reading /proc/<pid>
Root cause

You are inspecting another user's process without privileges.

Fix

Re-run the check with sudo, or as root inside a container.

When things break

Debugging tips

  • top -b -n 1 | head -15 is script-friendly and can be captured to a file for later analysis.
  • ps aux --sort=-%cpu pins down the top CPU consumer; --sort=-%mem does the same for memory.
  • When vmstat shows a large 'wa' (I/O wait), the bottleneck is disk — confirm with iostat -x to find the busy device.
  • If ping works but nslookup fails, the problem is DNS, not the network.
Interviews

Interview checkpoint

Practice

Hands-on lab

Capture a system baseline and generate load
15 min · intermediate
In progress
Objectives
  • Record a baseline of CPU, memory, disk and network usage
  • Generate deliberate CPU and memory load
  • Watch live behaviour change in top and vmstat
  • Measure directory sizes with du and filesystem space with df
  • Clean up and confirm the system returns to baseline

Capture a system baseline and generate load

15 min · intermediate

0/7 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/6
1

Which command shows live process activity sorted by CPU usage?

2

Which command reports total, used and available memory in megabytes?

3

A low 'free' number in free -m always means the machine is out of memory.

4

Which command shows free space on every mounted filesystem?

5

Which command shows which ports are listening and which process owns them?

6

top shows a load average of 4.0 on a 2-core server and vmstat reports a large 'r' column. What does this mean?

Quick reference

Cheat sheet

CPU & processes
topLive processes by CPU
htopInteractive, colourful top
ps aux --sort=-%cpuProcesses ranked by CPU
uptimeLoad average trend
Memory & swap
free -mMemory in MB — watch available
vmstat 1 3Run queue, memory, swap, CPU
cat /proc/meminfoRaw kernel memory counters
Disk & I/O
df -hSpace per mounted filesystem
du -sh /varSize of a directory tree
iostat -x 2 3Per-disk throughput and util
lsblkList block devices
Network
ip aInterface addresses and state
ss -tulpnListening sockets and owner
ping -c 4 hostReachability and latency
traceroute hostPath and per-hop latency
nslookup hostDNS resolution
Logs & kernel
tail -f /var/log/syslogFollow a log file
journalctl -fFollow the systemd journal
journalctl -p err -bErrors from this boot
dmesg | tail -20Kernel ring buffer
Go deeper

Official documentation