Skip to content

Linux & shell: Debug & troubleshoot

Linux & shell commands for debug & troubleshoot (tags: #debug #performance).

Index
13 items

Linux: Show errors since last boot

Safe

Only priority err and worse from the current boot: fastest way to see what's wrong on a box.

journalctl -b -p err --no-pager
What each part does
  1. journalctlread the systemd journal
  2. -bthis boot only (-b -1 for the previous one)
  3. -p errpriority err and worse (crit, alert, emerg)
  4. --no-pagerprint directly

Also known as: boot errors · journalctl -p err

Linux: Search a log for an error with context

Safe

Case-insensitive search with line numbers and 3 lines of context around each hit.

grep -n -i -C 3 "<pattern>" <file>
What each part does
  1. grepsearch text
  2. -nshow line numbers
  3. -icase-insensitive
  4. -C 33 lines of context before and after each match
  5. "<pattern>" <file>what to look for, and where
Example
$ grep -n -i -C 3 "timeout" /var/log/app/app.log

Also known as: grep context · find error in log

Linux: Check why a service failed

Safe

Status, last exit code and the most recent log lines of a unit.

systemctl status <service> --no-pager -l
What each part does
  1. systemctl status <service>state, main PID, exit code, and the last log lines of the unit
  2. --no-pagerprint directly instead of opening less
  3. -ldon't truncate long lines

Also known as: service failed · unit failed · systemctl status

Linux: Watch live CPU and memory hogs

Safe

Interactive process view sorted by CPU with full command lines. Press M to sort by memory, 1 for per-core.

top -c -o %CPU
What each part does
  1. toplive view of processes
  2. -cshow the full command line, not just the program name
  3. -o %CPUsort by CPU (press M to switch to memory, q to quit)

My note htop is nicer if installed: htop --sort-key=PERCENT_MEM

Also known as: top · htop · high cpu · high load

Linux: Check disk usage per filesystem

Safe

Size, used and available space per mount, with filesystem type.

df -hT
What each part does
  1. dfdisk free: size, used, available per mounted filesystem
  2. -hhuman-readable sizes (G, M)
  3. -Tshow the filesystem type (ext4, xfs, tmpfs...)
Example
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/nvme0n1p1 ext4   50G   49G  1.0G  98% /

My note Also run df -i: a disk can be 'full' on inodes with space left.

Also known as: df -h · disk full · no space left on device

Linux: Find the biggest directories

Safe

Top 20 directories by size under a path, staying on one filesystem (-x).

sudo du -xh --max-depth=1 <path> 2>/dev/null | sort -rh | head -20
What each part does
  1. sudo dudisk usage of directories
  2. -xstay on one filesystem (skip other mounts)
  3. -hhuman-readable sizes
  4. --max-depth=1 <path>one total per direct subdirectory of path
  5. 2>/dev/nullhide permission errors
  6. | sort -rhsort human sizes, largest first
  7. | head -20top 20
Example
$ sudo du -xh --max-depth=1 /var 2>/dev/null | sort -rh | head -20

Also known as: du · what is using disk · disk full

Linux: Find deleted files still holding disk space

Safe

Lists open files whose link count is 0: deleted but still held open by a process, so space isn't freed.

sudo lsof -nP +L1
What each part does
  1. sudo lsoflist open files (as root, to see every process)
  2. -nPno DNS or port-name lookups: faster
  3. +L1only files with link count < 1: deleted but still open; restart the process to free the space

My note Restart (or truncate via /proc/<pid>/fd/<n>) the holding process to free the space.

Also known as: df full but du small · deleted files open · lsof +L1

Linux: List listening ports and owning processes

Safe

TCP/UDP listening sockets with the PID/program that owns each one.

sudo ss -tulpn
What each part does
  1. sudo sssocket statistics (sudo to see other users' processes)
  2. -tuTCP and UDP sockets
  3. -llistening only
  4. -pshow the owning process and PID
  5. -nnumeric ports and addresses, no name lookups

Also known as: netstat -tulpn · port in use · address already in use · EADDRINUSE

Linux: Debug an HTTP request verbosely

Safe

Shows TLS handshake, request/response headers, final status code and total time.

curl -v -sS -o /dev/null -w "%{http_code} %{time_total}s\n" <url>
What each part does
  1. curlmake an HTTP request
  2. -vverbose: DNS, connect, TLS handshake, request and response headers
  3. -sShide the progress bar, still show errors
  4. -o /dev/nullthrow away the response body
  5. -w "%{http_code} %{time_total}s\n"after the request, print the status code and total time
  6. <url>the URL
Example
< HTTP/2 503
< retry-after: 30
503 0.214s

Also known as: curl -v · http debug · ssl handshake · connection refused

Linux: Resolve DNS for a host

Safe

Quick A/CNAME lookup. Use dig @<resolver> <hostname> to compare against a specific DNS server.

dig +short <hostname>
What each part does
  1. digDNS lookup tool
  2. +shortprint only the answers (IPs or CNAME targets)
  3. <hostname>the name to resolve; add @<server> to ask a specific DNS server

Also known as: nslookup · dns lookup · NXDOMAIN · could not resolve host

Linux: Check the OS release and running kernel

Safe

First step before patching: tells you which package manager to use (dnf, yum, apt, zypper, apk) and which kernel is running now.

cat /etc/os-release && uname -r
What each part does
  1. cat /etc/os-releasedistro name and version (NAME, VERSION_ID, e.g. Amazon Linux 2023, Ubuntu 22.04)
  2. uname -rthe kernel that is running right now (not necessarily the newest one installed)
Example
$ cat /etc/os-release && uname -r
NAME="Amazon Linux"
VERSION_ID="2023"
...
6.1.112-122.189.amzn2023.x86_64

Also known as: which distro · os version · kernel version · uname -r · os-release

Linux: Check versions of common runtimes on a host

Safe

Scanners flag these outside the OS package list. Compare each with the "Fixed In" version from the report.

node -v; python3 --version; java -version 2>&1 | head -1; nginx -v; git --version; /opt/splunkforwarder/bin/splunk version 2>/dev/null
What each part does
  1. node -vNode.js
  2. python3 --versionPython
  3. java -version 2>&1 | head -1Java (prints to stderr)
  4. nginx -vnginx
  5. git --versiongit
  6. splunk versionSplunk Universal Forwarder

Also known as: runtime versions · node version · python version · java version · splunk forwarder version

Linux: Check the installed version of a package

Safe

Works on RHEL/Fedora/Amazon Linux, Debian/Ubuntu and Alpine. For installed vs available: apt-cache policy <package> or dnf list --showduplicates <package>.

rpm -q <package> 2>/dev/null || dpkg-query -W -f='${Package} ${Version}
' <package> 2>/dev/null || apk info -v <package>
What each part does
  1. rpm -q <package>RHEL family and SUSE: name-version-release, or 'not installed'
  2. 2>/dev/null ||hide 'command not found' and try the next package manager
  3. dpkg-query -W -f='${Package} ${Version} ' <package>Debian / Ubuntu: name and installed version
  4. apk info -v <package>Alpine: name-version
Example
$ rpm -q openssl 2>/dev/null || dpkg-query -W -f='${Package} ${Version}\n' openssl 2>/dev/null || apk info -v openssl
openssl 3.0.13-0ubuntu3.4

My note To check many servers at once, run the same line through Ansible: ansible all -i <inventory> -m shell -a "...".

Also known as: package version · check version of package · dpkg -l · rpm -q · apt-cache policy · is package installed

My DevOps Playbook by Angelo John Calleja

New runbook

GitHub not connected

Saving is handled by the site: the GitHub token is stored in Cloudflare, not in your browser. Enter the editor password (the EDITOR_PASSWORD secret) to unlock saving in this browser.

Saving commits to gelogrammer/My_DevOps_Playbook. Create a fine-grained token with access to only this repo andContents: Read and write, then paste it here. It is stored only in this browser.

src/content/docs/…