For system administrators, the Linux command line is both a Swiss Army knife and a diagnostic toolkit. While graphical tools have their place, the command line offers unparalleled speed, precision, and depth when troubleshooting system issues—from minor performance glitches to critical service outages. This blog aims to demystify practical Linux CLI troubleshooting, equipping you with a systematic approach, essential tools, and best practices to resolve problems efficiently. Whether you’re debugging a misbehaving service, diagnosing resource bottlenecks, or untangling network issues, mastering these skills will transform you into a more confident and effective administrator.
Table of Contents
- Fundamentals of Linux CLI Troubleshooting
- Troubleshooting Common System Issues
- Advanced Troubleshooting Techniques
- Best Practices for Efficient Troubleshooting
- Conclusion
- References
1. Fundamentals of Linux CLI Troubleshooting
1.1 The Troubleshooting Mindset
Effective troubleshooting begins with a structured approach:
- Identify Symptoms: Gather user reports or system alerts (e.g., “server is slow” or “API requests timing out”).
- Gather Data: Use CLI tools to collect metrics, logs, and process information.
- Hypothesize Causes: Narrow down potential issues (e.g., “high CPU usage” or “disk I/O saturation”).
- Test Hypotheses: Validate theories with targeted commands (e.g., check
topfor CPU hogs). - Resolve and Verify: Apply fixes and confirm resolution (e.g., restart a service and monitor metrics).
Avoid “shotgun debugging” (randomly changing settings). Instead, iterate methodically to minimize downtime.
1.2 Key Tools for Initial System Assessment
Before diving into specific issues, start with these commands to understand the system’s state:
| Command | Purpose | Example Output & Explanation |
|---|---|---|
hostname | Show system hostname. | web-server-01 (identifies the machine in a cluster). |
uname -a | Display kernel version and system architecture. | Linux web-server-01 5.4.0-100-generic #113-Ubuntu SMP Thu Feb 2 12:34:51 UTC 2023 x86_64 |
uptime | Show system uptime and load averages (1/5/15 min). | 14:32:45 up 23 days, 45 min, 1 user, load average: 0.89, 0.75, 0.62 (load < CPU cores = healthy). |
w or who | List logged-in users and their activities. | user1 pts/0 2023-10-01 10:15 (192.168.1.100) (helps identify unauthorized access). |
htop (or top) | Real-time process monitor (CPU, memory, disk I/O). | Interactive interface with sorted processes; use F6 to sort by CPU/memory. |
Example Workflow:
Run uptime to check load averages. If high (> number of CPU cores), use htop to identify resource-heavy processes.
1.3 Log Analysis: The First Step to Diagnosing Issues
Logs are critical for understanding past events. Key log directories and tools:
/var/log/: Central log directory (e.g.,syslog,auth.log(Debian) orsecure(RHEL),nginx/access.log).journalctl: Query systemd logs (replaces traditional syslog on systemd-based distros like Ubuntu 16.04+ and RHEL 7+).
Essential Log Commands:
# Tail a log in real-time (e.g., Apache errors)
tail -f /var/log/apache2/error.log
# Search for "error" in authentication logs (Debian/Ubuntu)
grep -i "error" /var/log/auth.log
# View logs for the nginx service (systemd)
journalctl -u nginx.service
# Filter logs from the last hour
journalctl --since "1 hour ago" --until "now"
Tip: Use journalctl -p err to show only error-level logs for quick issue spotting.
2. Troubleshooting Common System Issues
2.1 High CPU Usage
Symptoms: Slow response, high load averages, processes stuck in “R” (running) state.
Diagnosis Steps:
-
Identify top CPU-consuming processes:
# List top 5 CPU-heavy processes (descending order) ps aux --sort=-%cpu | head -6 # Skip header with head -6Output might show a misbehaving
python3script using 90% CPU. -
Drill into per-thread CPU usage with
htop:
PressHinhtopto toggle threads. Look for threads within a process consuming excessive CPU. -
Check for CPU core saturation with
mpstat(part ofsysstatpackage):mpstat -P ALL 2 # Monitor all cores every 2 secondsA core with 100%
%usror%sysindicates saturation.
Resolution:
- If the process is non-essential, kill it with
kill -9 <PID>. - If essential, optimize the process (e.g., add caching to a script) or scale resources (e.g., upgrade CPU).
2.2 Memory (RAM) Shortages
Symptoms: OOM (Out-of-Memory) killer terminating processes, free -h showing low available RAM, swap thrashing.
Diagnosis Steps:
-
Check memory usage with
free -h:free -hOutput:
total used free shared buff/cache available Mem: 15Gi 12Gi 512Mi 256Mi 2.5Gi 2.1Gi Swap: 4.0Gi 3.8Gi 200MiLow
availableRAM (<10% of total) and high swap usage indicate memory pressure. -
Identify memory-hungry processes:
ps aux --sort=-%mem | head -6 # Sort by memory usage -
Check for OOM kills in logs:
dmesg | grep -i "out of memory" # Kernel logs OOM events
Resolution:
- Terminate non-critical processes.
- Add swap space temporarily (e.g.,
sudo fallocate -l 2G /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile). - Upgrade RAM or optimize memory leaks in applications.
2.3 Disk Space and I/O Bottlenecks
Symptoms: “No space left on device” errors, slow file operations, high iowait in top.
Diagnosis Steps:
-
Check disk usage with
df -h:df -h # Show mounted filesystems and usageA filesystem at 100% usage needs immediate attention.
-
Find large files/directories with
du:# List sizes of directories in /var (human-readable, summarize) du -sh /var/* | sort -rh | head -10Often,
/var/log/or/var/lib/docker/grows unchecked. -
Check for deleted but open files (still consuming space):
lsof | grep deleted # Files marked as deleted but held open by processesRestart the process to free space (e.g.,
sudo systemctl restart nginx). -
Monitor disk I/O with
iostat(sysstat package):iostat -x 2 # Check I/O stats every 2 secondsHigh
%iowait(>20%) indicates slow storage (e.g., a failing HDD).
Resolution:
- Delete large, unnecessary files (e.g., old logs with
truncate -s 0 /var/log/syslog). - Move data to a larger disk or enable log rotation (e.g.,
logrotate).
2.4 Network Connectivity and Performance
Symptoms: Timeouts, slow transfers, “connection refused” errors.
Diagnosis Steps:
-
Test basic connectivity with
pingandtraceroute:ping google.com # Check DNS and ICMP reachability traceroute google.com # Identify hop-by-hop latency issues -
Check open ports and listening services:
# List all TCP/UDP ports with processes (requires root) ss -tulpn # t: TCP, u: UDP, l: listening, p: process, n: numericLook for
LISTENstate on expected ports (e.g., 80 for HTTP). -
Check firewall rules with
ufw(Debian/Ubuntu) orfirewalld(RHEL):sudo ufw status # Debian/Ubuntu sudo firewall-cmd --list-all # RHEL/CentOSEnsure required ports (e.g., 443) are allowed.
-
Analyze traffic with
tcpdump(advanced):sudo tcpdump -i eth0 port 80 -c 10 # Capture 10 HTTP packets on eth0
Resolution:
- Open blocked ports with
sudo ufw allow 443/tcp. - Fix DNS issues by editing
/etc/resolv.confor restartingsystemd-resolved.
2.5 Service and Process Failures
Symptoms: Services not starting, systemctl status <service> shows “failed”.
Diagnosis Steps:
-
Check service status with
systemctl:systemctl status nginx.serviceOutput might show “Failed to start A high performance web server…“.
-
View service logs with
journalctl:journalctl -u nginx.service --no-pager # Show full logs for nginxLook for errors like “invalid PID file” or “configuration syntax error”.
-
Validate configuration files (critical for services like
nginx,apache, orsshd):nginx -t # Test nginx config syntax apachectl configtest # Test Apache config
Resolution:
- Fix syntax errors in config files (e.g., missing
;in nginx.conf). - Restore a working config from backup or version control.
3. Advanced Troubleshooting Techniques
3.1 Debugging with strace and ltrace
For processes behaving unexpectedly, trace system calls (strace) or library calls (ltrace):
-
strace: Trace system calls (e.g.,open(),read(),write()).# Trace a running process (PID=1234) strace -p 1234 # Trace a command and log to a file strace -o curl_trace.txt curl https://example.comLook for
ENOENT(file not found) orEACCES(permission denied) errors. -
ltrace: Trace dynamic library calls (e.g.,printf(),malloc()).ltrace -p 1234 # Trace library calls of PID 1234
3.2 Analyzing System Calls with perf
The perf tool (part of linux-tools-common) profiles CPU usage at the kernel level:
# Monitor real-time CPU usage by function
perf top
# Record CPU events for a process, then generate a report
perf record -g -p <PID> # -g: capture call graphs
perf report # Analyze the recorded data
Use perf to identify bottlenecks in custom applications or kernel modules.
3.3 Kernel Logs and dmesg
The kernel ring buffer (dmesg) logs low-level events (hardware issues, driver failures):
dmesg | grep -i error # Show kernel errors
dmesg -T # Show human-readable timestamps (e.g., "2023-10-01 14:30:00")
Look for messages like “USB device not recognized” or “ata1: failed to read log page” (disk issues).
4. Best Practices for Efficient Troubleshooting
4.1 Documentation and Note-Taking
Always document:
- Symptoms, timestamps, and initial metrics (e.g., “14:00: Load avg 5.0, CPU 90%”).
- Commands run and their outputs (use
scriptto log an entire session:script troubleshooting-20231001.log). - Fixes applied and their outcomes (e.g., “Restarted nginx; CPU dropped to 15%“).
4.2 Scripting for Repetitive Tasks
Automate routine checks with bash scripts. Example: Disk space alert script:
#!/bin/bash
THRESHOLD=90
df -h | awk -v threshold="$THRESHOLD" '$5+0 > threshold {print "ALERT: " $0}'
Save as disk_alert.sh, make executable (chmod +x), and schedule with cron to run hourly.
4.3 Safety First: Avoiding Destructive Commands
- Test commands in a sandbox before running on production (e.g., use
echo rm -rf /tmp/*to preview deletions). - Avoid
rm -rfas root—userm -i(interactive) or move files to/tmpfirst. - Use
sudosparingly: Run non-destructive commands as a regular user.
4.4 Version Control for Configuration Files
Track changes to /etc/ with git to revert mistakes:
cd /etc && sudo git init && sudo git add . && sudo git commit -m "Initial commit"
Commit changes before editing: sudo git commit -am "Update nginx.conf for SSL".
5. Conclusion
Linux CLI troubleshooting is a blend of art and science. By adopting a systematic mindset, mastering core tools like ps, top, and journalctl, and following best practices (documentation, scripting, safety), you can resolve issues faster and minimize downtime. Remember: the command line is your most powerful ally—invest time in learning its nuances, and you’ll become a more effective system administrator.
6. References
- Linux man pages (official documentation for commands).
- The Linux Command Line by William Shotts (free online book).
- Red Hat System Administrator’s Guide
- Ubuntu Server Troubleshooting Guide
- perf Wiki (kernel profiling with
perf).