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.
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.
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.
Mental model
An aircraft cockpit instrument panel
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.
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.
Key commands
topLive 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.jshtopColourful, interactive top with mouse support and easier process management.
vmstat 2 5Sample 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 -mMemory 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 -hFree and used space on every mounted filesystem, human-readable.
Filesystem Size Used Avail Use% Mounted on /dev/sda1 49G 22G 25G 47% /
du -sh /varTotal size of a directory in human-readable form.
2.3G /var
iostat -x 2 3Detailed 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 aList 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 eth0ss -tulpnShow 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.comSend 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.comMap every hop between you and the target to find where latency lives.
nslookup example.comQuery DNS to resolve a hostname or inspect name-server records.
tail -f /var/log/syslogFollow a log file live, printing each new line as it is written.
journalctl -fFollow the entire systemd journal across all services and the kernel.
dmesg | tail -20Read the latest kernel ring-buffer messages — hardware and driver events.
Examples
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 -tulpnRun 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.
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.
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.
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.
Common issues
bash: vmstat: command not foundA minimal container image without the procps package.
Install it with apt update && apt install -y procps (or dnf install procps-ng).
ping: example.com: Temporary failure in name resolutionDNS is not configured — /etc/resolv.conf is empty or unreachable.
Check cat /etc/resolv.conf and connectivity, then fix DNS config or use a reachable nameserver.
free -m shows 0 free but the server is fineMemory is being used for buff/cache, which the kernel can reclaim instantly.
Read the available column — it is the real free memory. Ignore 'free' alone.
Permission denied while reading /proc/<pid>You are inspecting another user's process without privileges.
Re-run the check with sudo, or as root inside a container.
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.
Interview checkpoint
Hands-on lab
- 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
Quick quiz
Which command shows live process activity sorted by CPU usage?
Which command reports total, used and available memory in megabytes?
A low 'free' number in free -m always means the machine is out of memory.
Which command shows free space on every mounted filesystem?
Which command shows which ports are listening and which process owns them?
top shows a load average of 4.0 on a 2-core server and vmstat reports a large 'r' column. What does this mean?
Cheat sheet
topLive processes by CPUhtopInteractive, colourful topps aux --sort=-%cpuProcesses ranked by CPUuptimeLoad average trendfree -mMemory in MB — watch availablevmstat 1 3Run queue, memory, swap, CPUcat /proc/meminfoRaw kernel memory countersdf -hSpace per mounted filesystemdu -sh /varSize of a directory treeiostat -x 2 3Per-disk throughput and utillsblkList block devicesip aInterface addresses and statess -tulpnListening sockets and ownerping -c 4 hostReachability and latencytraceroute hostPath and per-hop latencynslookup hostDNS resolutiontail -f /var/log/syslogFollow a log filejournalctl -fFollow the systemd journaljournalctl -p err -bErrors from this bootdmesg | tail -20Kernel ring buffer