Debug commands Cheat Sheet

Here you will find a list of useful shell commands I use for my daily debugging tasks.
Please give us your own useful commands !

  • Display kernel version information :
uname -a
  • Display firmware version :
cat /etc/REVISIONS
  • List running processes :
ps -A
  • Open an interactive process viewer (with ressources) :
htop
  • List processes using the file :
lsof myfile //where myfile is the filename
  • List files used by a process
lsof –p 3255 //where 3255 is the process number
  • Count all files opened
lsof | wc -l
  • List all IP sockets
lsof -i
  • List all files opened by a process
ls -l /proc/3255/fd/
  • List all TCP connections
netstat -tap
  • List all UDP connections
netstat -udp
  • List all open ports with associated processes
netstat -tulpn | grep LISTEN
  • Display total and used space on file system
df –h
  • Display all messages from the kernel ring buffer
dmesg
  • Capture and display network traffic on interface br0
tcpdump -i br0
  • Capture and display network traffic on interface br0 in ASCII format
tcpdump -i br0 -A
  • Capture and display network traffic on interface br0 in HEX format
tcpdump -i br0 -XX
  • Capture network traffic on interface br0 and save it to .pcap file (can be open with Wireshark)
tcpdump -w capture.pcap -i br0
  • Open .pcap file and display content
tcpdump -r capture.pcap 
  • Capture and display TCP packets on interface br0
tcpdump -i br0 tcp
  • Capture and display traffic on interface br0 with specific port
tcpdump -i br0 port 502
  • Capture and display traffic on interface br0 with specific source ip address
tcpdump -i br0 src 192.168.0.2
  • Capture and display traffic on interface br0 with specific destination ip address
tcpdump -i br0 dst 192.168.0.44
5 Likes

Advanced use of the “ps” command to print only the top 6 process from CPU usage, memory usage, CPU Time:

ps -A --format user,uid,comm,pid,pcpu,pmem,time,tty --sort=-pcpu | head -n 6
ps -A --format user,uid,comm,pid,pcpu,pmem,time,tty --sort=-pmem | head -n 6
ps -A --format user,uid,comm,pid,pcpu,pmem,time,tty --sort=-time | head -n 6

Batch ping an ip range (It return only the alive IP):

for i in {1…254} ;do (ping 192.168.1.$i -c 1 -w 5 >/dev/null && echo “192.168.1.$i” &) ;done

Run TCPdump in the background and save a new file automatically after a certain size:

/usr/sbin/tcpdump -U -i eth0 -w /media/sd/log_$(date +%Y-%m-%d_%H%M%S).pcap -C 10 &>/dev/null &

Print the different port used (With option for filtering what state they are on)

lsof -i -P -n
lsof -i -P -n | grep LISTEN
lsof -i -P -n | grep ESTABLISHED
lsof -i -P -n | grep CLOSE_WAIT
lsof -i -P -n | grep FIN_WAIT

4 Likes