Skip to content

Commands: Linux & shell

Linux and shell commands: processes, disks, network, logs, SSH.

Index
  • #linux
  • #shell
Updated

Also:bash · shell · sysadmin

23 items

Linux: Follow logs of a systemd service

Safe

Stream a service's journal, starting with the last 100 lines.

journalctl -u <service> -f -n 100
What each part does
  1. journalctlread the systemd journal
  2. -u <service>only this unit
  3. -ffollow new lines
  4. -n 100start with the last 100 lines
Example
$ journalctl -u nginx -f -n 100
Nov 20 09:12:01 web-1 nginx[812]: 10.0.3.4 - - "GET /healthz HTTP/1.1" 200

My note Add --since "10 min ago" when the service is chatty.

Also known as: journalctl · systemd logs · service logs

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: Follow a log file across rotation

Safe

Like tail -f but keeps following when logrotate replaces the file.

tail -F -n 200 <file>
What each part does
  1. tailprint the end of a file
  2. -Ffollow by name and retry: keeps going when the file is rotated or recreated (-f would stop)
  3. -n 200start with the last 200 lines
  4. <file>the log file

My note -F (capital) = --follow=name --retry. -f stops after rotation.

Also known as: tail -f · follow log

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: SSH into a host with a specific key

Safe

Connect using one explicit key (avoids 'Too many authentication failures').

ssh -i <key-file> -o IdentitiesOnly=yes <user>@<host>
What each part does
  1. sshSSH client
  2. -i <key-file>use this private key
  3. -o IdentitiesOnly=yesoffer only that key, not every key in the agent (avoids Too many authentication failures)
  4. <user>@<host>who and where

Also known as: ssh · Too many authentication failures · permission denied (publickey)

Linux: Open an SSH tunnel through a bastion

Safe

Forward a local port to a private host/port via a jump box. Then connect to localhost:<local-port>.

ssh -N -L <local-port>:<target-host>:<target-port> <user>@<bastion>
What each part does
  1. sshSSH client
  2. -Ndon't run a remote command: tunnel only
  3. -L <local-port>:<target-host>:<target-port>localhost:local-port forwards to target-host:target-port as seen from the bastion
  4. <user>@<bastion>the jump host to connect to
Example
$ ssh -N -L 5432:db.internal:5432 ops@bastion.example.com

Also known as: port forward · bastion · jump host · ssh tunnel

Linux: Vacuum old journal logs

Caution

Delete archived journal files older than N days to reclaim disk.

sudo journalctl --vacuum-time=<days>d
What each part does
  1. sudo journalctlmanage the journal as root
  2. --vacuum-time=<days>ddelete archived journal files older than N days (or --vacuum-size=500M)

My note Check size first: journalctl --disk-usage

Also known as: journal too big · /var/log/journal

Linux: Delete old rotated logs

Destructive

Remove compressed rotated logs older than N days. Run without -delete first to preview.

sudo find /var/log -type f -name "*.gz" -mtime +<days> -delete
What each part does
  1. sudo find /var/logsearch under /var/log as root
  2. -type ffiles only
  3. -name "*.gz"compressed rotated logs
  4. -mtime +<days>last modified more than N days ago
  5. -deletedelete matches (run without it first to see the list)

Also known as: clean /var/log · no space left on device

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: List installed kernels

Safe

Every kernel installed on the host. Compare with uname -r to see if a newer one is waiting for a reboot.

rpm -q kernel 2>/dev/null || dpkg -l 'linux-image*' | grep ^ii
What each part does
  1. rpm -q kernelRHEL / Amazon Linux / SUSE: installed kernel packages (AL2023 may name them kernel6.12, kernel6.18: rpm -qa "kernel*")
  2. 2>/dev/null ||not an rpm system? try dpkg
  3. dpkg -l 'linux-image*' | grep ^iiDebian / Ubuntu: installed kernel images (ii = installed)
  • #linux
  • #patching
  • #kernel

Also known as: installed kernels · old kernels · kernel list

Linux: Upgrade one package on Alpine

Caution

Upgrade a single package on Alpine (usually inside a container or Dockerfile).

Dry run first
apk update && apk add --upgrade --simulate <package>
apk update && apk add --upgrade <package>
What each part does
  1. apk updaterefresh the package index
  2. apk add --upgrade <package>upgrade it to the newest version in the repo

My note Everything: apk upgrade --no-cache

  • #linux
  • #patching

Also known as: apk upgrade · alpine update package

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: Find bundled Chromium binaries on a host

Safe

Puppeteer and Playwright download their own Chromium. Old copies show up as Chromium CVEs that OS patching never fixes.

sudo find / -xdev \( -name chrome -o -name chromium -o -name headless_shell \) -type f 2>/dev/null
What each part does
  1. find / -xdevsearch the root filesystem only
  2. -name chrome -o -name chromium -o -name headless_shellChromium binary names
  3. -type ffiles only

My note Fix it in the app: npm ls puppeteer, then npm install puppeteer@latest and redeploy. Delete stale ~/.cache/puppeteer folders.

  • #linux
  • #patching
  • #vulnerabilities

Also known as: chromium cve · puppeteer chromium · headless chrome · old chrome

Linux: Upgrade nginx and restart it safely

Caution

Restart only if the config still passes after the upgrade. A reload keeps the old binary running, so restart.

Dry run first
sudo dnf upgrade nginx --assumeno
sudo dnf upgrade nginx && sudo nginx -t && sudo systemctl restart nginx
What each part does
  1. dnf upgrade nginxnew nginx package (apt install --only-upgrade nginx on Ubuntu)
  2. nginx -ttest the config; stops the chain if it fails
  3. systemctl restart nginxstart the new binary
  • #linux
  • #patching
  • #systemd

Also known as: nginx update · nginx cve · upgrade nginx

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/…