Linux — Practical
Linux — Practical (commands & one-liners)
Section titled “Linux — Practical (commands & one-liners)”Top resource consumers
Section titled “Top resource consumers”# top by memps -eo pid,user,%mem,rss,vsz,cmd --sort=-rss | head
# top by cpups -eo pid,user,%cpu,cmd --sort=-%cpu | head
# group by userps -eo user,rss --no-headers | awk '{a[$1]+=$2} END {for(u in a) print u, a[u]/1024 " MB"}'File system inspection
Section titled “File system inspection”df -h # disk usagedf -i # inodesdu -sh /var/log/* # by dirdu -ah . | sort -rh | head -20 # biggest files
# deleted but open files (still hold disk)sudo lsof | grep deleted | head
# what's mounted, typesmount | column -tfindmntNetwork triage
Section titled “Network triage”# listening socketssudo ss -tnlp
# connections to port 5432ss -tn 'sport = :5432' | head
# latency / routemtr --report --report-cycles=10 hosttraceroute -T -p 443 host
# DNS resolutiondig +short example.comdig +trace example.com
# packet capturesudo tcpdump -nn -i any -s 0 -w cap.pcap port 8080
# raw socket count by statess -tan | awk '{print $1}' | sort | uniq -cProcess inspection
Section titled “Process inspection”# what's it doing?strace -p PID -e trace=network 2>&1 | headsudo cat /proc/PID/statuscat /proc/PID/wchan; cat /proc/PID/stackls -la /proc/PID/fd | headsudo lsof -p PIDsudo gcore PID # core dump
# tree of processespstree -p
# threadsps -L -p PIDtop -H -p PID # per-thread CPUMemory deep
Section titled “Memory deep”# meminfocat /proc/meminfo | head -20
# swap usage by processfor f in /proc/[0-9]*/status; do awk '/Name|VmSwap/{printf "%s ", $2}' "$f"; echo; done | sort -k2 -n -r | head
# huge pagescat /proc/meminfo | grep -i huge
# slabsudo slabtop -o | head# system journaljournalctl -u nginx -f --since '10 min ago' --no-pagerjournalctl -p err --since todayjournalctl -k # kernel onlyjournalctl --vacuum-size=500M # trim
# rotate logssudo logrotate -d /etc/logrotate.d/nginx
# grepszgrep -h ERROR /var/log/syslog.* | tailPerformance one-shots (USE method)
Section titled “Performance one-shots (USE method)”# CPUmpstat -P ALL 1 5
# memorysar -r 1 5
# diskiostat -x 1 5
# netsar -n DEV 1 5
# all-in-onedstat -tcndmgyl 1 10eBPF (bcc / bpftrace)
Section titled “eBPF (bcc / bpftrace)”# top syscalls per processsudo bpftrace -e 'tracepoint:raw_syscalls:sys_enter { @[comm] = count(); } interval:s:5 { print(@); clear(@); }'
# slow file readssudo /usr/share/bcc/tools/biolatencysudo /usr/share/bcc/tools/biotopsudo /usr/share/bcc/tools/tcpconnectsudo /usr/share/bcc/tools/opensnoopBrendan Gregg’s bcc tools and bpftrace give visibility without restarts or instrumentation.
Useful flags / kernel tunings
Section titled “Useful flags / kernel tunings”# ephemeral port rangesysctl net.ipv4.ip_local_port_range
# raise file descriptors per processulimit -n 1048576
# keep TCP keep-alive close-ishsysctl -w net.ipv4.tcp_keepalive_time=60sysctl -w net.ipv4.tcp_keepalive_intvl=10sysctl -w net.ipv4.tcp_keepalive_probes=6
# allow more connections waitingsysctl -w net.core.somaxconn=65535sysctl -w net.ipv4.tcp_max_syn_backlog=65535
# avoid TIME_WAIT exhaustionsysctl -w net.ipv4.tcp_tw_reuse=1Persist in /etc/sysctl.d/99-tuning.conf.
Useful one-liners
Section titled “Useful one-liners”# count reqs by HTTP status from access logawk '{print $9}' access.log | sort | uniq -c | sort -rn
# top URLsawk '{print $7}' access.log | sort | uniq -c | sort -rn | head
# replace string across filesgrep -rl 'old' . | xargs sed -i 's/old/new/g'
# files modified in last hourfind /var -type f -mmin -60
# process tree of biggest mem hogpstree -ps $(ps -eo pid,rss --sort=-rss --no-headers | head -1 | awk '{print $1}')
# busy disksudo iotop -o -b -n 5 -d 1SSH config power
Section titled “SSH config power”Host bastion HostName bastion.example.com User ubuntu IdentityFile ~/.ssh/id_ed25519
Host *.internal ProxyJump bastion User ubuntu ServerAliveInterval 60
Host db1.internal LocalForward 5432 localhost:5432ssh db1.internal # tunnels through bastionpsql -h localhost -p 5432systemd-run for ad-hoc
Section titled “systemd-run for ad-hoc”systemd-run --user --scope -- nice -n 19 myjob
# resource-limited transient unitsystemd-run --uid=app --slice=mybatch.slice -p MemoryMax=512M -p CPUQuota=50% bash batch.shContainer debugging on host
Section titled “Container debugging on host”# find pid of container processdocker top <id> | awk 'NR>1 {print $2}'
# nsenter into pid for debuggingsudo nsenter -t PID -n ss -tnp # inspect namespace from host