DEV Community

Sadeek M
Sadeek M

Posted on

Basic Linux Debugging Guide

Debugging in Linux is essential for troubleshooting system issues, application bugs, or network problems. Here’s a simple guide to get you started with common Linux debugging tools and techniques.

1. Checking System Logs
Linux logs are a great starting point for debugging. Logs capture events related to the kernel, applications, and services.

View Logs with journalctl

# List all processes
ps aux

# View processes in real-time
top
Enter fullscreen mode Exit fullscreen mode

Find a Process by Name with pgrep

pgrep <process_name>
Enter fullscreen mode Exit fullscreen mode

Debug a Process with strace
strace traces system calls made by a process.

strace -p <pid>`
Enter fullscreen mode Exit fullscreen mode

Debug a Process with gdb
gdb is used for debugging compiled applications.

gdb <program_name> <pid>
Enter fullscreen mode Exit fullscreen mode
  1. Networking Debugging Check Active Connections with netstat or ss

Using netstat

netstat -tuln
Enter fullscreen mode Exit fullscreen mode

Using ss

ss -tuln
Enter fullscreen mode Exit fullscreen mode

Test Connectivity with ping or curl

ping <hostname_or_ip>
Enter fullscreen mode Exit fullscreen mode

curl -v http://
Check Firewall Rules with iptables or ufw

Using iptables

sudo iptables -L -v
Enter fullscreen mode Exit fullscreen mode

Using ufw

sudo ufw status verbose
Enter fullscreen mode Exit fullscreen mode
  1. Disk and Filesystem Debugging Check Disk Usage with df and du

Disk space usage

df -h
Enter fullscreen mode Exit fullscreen mode

Directory size usage

du -sh <directory_name>
Enter fullscreen mode Exit fullscreen mode

Check Disk Health with fsck and smartctl

Check and repair filesystem (run in unmounted mode)

sudo fsck /dev/sdX
Enter fullscreen mode Exit fullscreen mode

Check disk health using smartctl

sudo smartctl -a /dev/sdX
Enter fullscreen mode Exit fullscreen mode
  1. Memory Debugging Check Memory Usage with free and vmstat

Memory usage

free -h
Enter fullscreen mode Exit fullscreen mode

System performance and memory statistics

vmstat 1 5
Enter fullscreen mode Exit fullscreen mode

Analyze Memory Leaks with valgrind

valgrind --leak-check=yes ./your_program
Enter fullscreen mode Exit fullscreen mode
  1. Kernel Debugging Check Kernel Messages with dmesg
dmesg | tail
Enter fullscreen mode Exit fullscreen mode

Debug Kernel Panics with kdump
Ensure kdump is installed and configured for capturing kernel crash dumps.

sudo systemctl start kdump
Enter fullscreen mode Exit fullscreen mode

Conclusion
Effective debugging in Linux requires a good understanding of system logs, processes, network, disk, and memory. Start with logs, monitor processes, trace system calls, and analyze system resources. Mastering these tools will help you quickly diagnose and resolve issues.

Top comments (1)

Collapse
 
sunnybhambhani profile image
Sunny Bhambhani

Good one!