topic

Linux Survival Basics

The commands to inspect a machine before guessing.

110 checked fixes

Commands in this topic

Linux Survival Basics Can be slow

Find the Files Eating Your Disk

The disk was full, but guessing at folders was the slow part.

find /var -type f -printf '%s %p\n' | sort -nr | head -20
Linux Survival Basics Can be slow

Find Errors Before Reading Every Log Line

The error was in the log. The problem was finding it without reading noise.

grep -iE 'error|failed|denied|timeout' /var/log/nginx/error.log | tail -40
Linux Survival Basics Can be slow

Find the Exact Log Line Before You Scroll

The error was there. The useful part was knowing exactly where it was.

grep -inE 'error|failed|denied|timeout' /var/log/nginx/error.log
Linux Survival Basics Can be slow

Show Only Recent Errors

The log had old failures too. I only cared about the newest ones.

grep -iE 'error|failed|denied|timeout' /var/log/nginx/error.log | tail -10
Linux Survival Basics Read-only

Check Owner and Mode in One Line

The file existed. The owner and mode explained why it still failed.

stat -c '%A %U:%G %n' /var/www/example/index.html
Linux Survival Basics Can be slow

Find the Processes Using Memory

The server felt slow. Memory pressure was the first thing to rule out.

ps -eo pid,comm,%mem,%cpu --sort=-%mem | head
Linux Survival Basics Can be slow

Show Big Files in Human Units

Byte counts are precise. Human units are faster under pressure.

find /var -type f -printf '%s %p\n' | sort -nr | head -10 | awk '{printf "%.1f MB %s\n", $1/1024/1024, $2}'
Linux Survival Basics Can be slow

Count Source Files by Extension

A quick extension count can show whether expected content made it into the source tree.

find source -type f -printf '%f\n' | sed -n 's/.*\.//p' | sort | uniq -c | sort -nr
Linux Survival Basics Read-only

Fingerprint a Debian or Ubuntu Host

Before package triage, prove what OS family and release you are actually on.

. /etc/os-release && printf '%s %s %s\n' "$ID" "$VERSION_ID" "$VERSION_CODENAME"
Linux Survival Basics Read-only

Compare Kernel and Distro Versions

The distro version and kernel version answer different questions.

printf 'kernel=%s arch=%s distro=%s\n' "$(uname -r)" "$(uname -m)" "$(lsb_release -ds)"
Linux Survival Basics Read-only

List Installed Package Versions

A package inventory beats memory when a server is drifting.

dpkg-query -W -f='${Package}\t${Version}\t${Architecture}\n' | sort
Linux Survival Basics Read-only

Find Which Package Owns a File

That binary came from somewhere. dpkg can tell you where.

dpkg-query -S /usr/sbin/nginx
Linux Survival Basics Read-only

Find Broken or Leftover dpkg States

Not every package row is cleanly installed.

dpkg-query -W -f='${db:Status-Abbrev}\t${Package}\n' | awk '$1 !~ /^ii$/'
Linux Survival Basics Read-only

Find the Largest Installed Packages

Disk cleanup starts with evidence, not random package removal.

dpkg-query -W -f='${Installed-Size}\t${Package}\n' | sort -nr | head -20
Linux Survival Basics Read-only

Spot Foreign-Architecture Packages

One unexpected architecture can explain confusing dependency output.

dpkg-query -W -f='${Architecture}\t${Package}\n' | awk '$1 != "amd64" && $1 != "all"'
Linux Survival Basics Can be slow

Find the Largest CI Logs

Huge logs often point to loops, noisy tests, or runaway debug output.

find logs/ -type f -printf '%s %p\n' | sort -nr | head -10
Linux Survival Basics Can be slow

Count Failures by Test File

Turn noisy test logs into a ranked failure list.

grep -RhoE '[A-Za-z0-9_./-]+\.(test|spec)\.(js|ts|py|rb)' logs/ | sort | uniq -c | sort -nr | head
Linux Survival Basics Sensitive output

Read TLS Certificate Subject and Issuer

The certificate can be valid but issued for the wrong name.

openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -subject -issuer -dates
Linux Survival Basics Can be slow

Find Nginx SSL Certificate Directives

The wrong certificate is often in the server block.

grep -RInE 'ssl_certificate|ssl_certificate_key|server_name' /etc/nginx/sites-enabled /etc/nginx/conf.d 2>/dev/null
Linux Survival Basics Can be slow

Find Apache DocumentRoot and Directory Rules

Apache 403 often comes from the directory block, not the file.

grep -RInE 'DocumentRoot|<Directory|Require all|Options|AllowOverride' /etc/apache2/sites-enabled /etc/apache2/conf-enabled /etc/httpd/conf.d 2>/dev/null
Linux Survival Basics Sensitive output

Read Recent Apache Error Log Lines

The Apache error log usually names the denied directory or rule.

sudo tail -80 /var/log/apache2/error.log 2>/dev/null || sudo tail -80 /var/log/httpd/error_log
Linux Survival Basics Can be slow

List Docker Container Restart Status

Restart loops are visible before rebuilding anything.

docker ps -a --format 'table {{.Names}} {{.Image}} {{.Status}} {{.RestartCount}}'
Linux Survival Basics Can be slow

Read Recent Docker Container Logs

Recent container logs usually show the failing command or dependency.

docker logs --tail 80 container_name
Linux Survival Basics Read-only

Inspect Docker Restart Policy

Policy and exit code explain whether Docker is restarting by design.

docker inspect -f '{{.HostConfig.RestartPolicy.Name}} {{.State.ExitCode}} {{.State.Error}}' container_name
Linux Survival Basics Read-only

List systemd Timers and Last Runs

A timer can be inactive, missed, or waiting for the wrong calendar.

systemctl list-timers --all --no-pager
Linux Survival Basics Read-only

Read One systemd Timer Status

Timer status shows whether the schedule is loaded and active.

systemctl status backup.timer --no-pager
Linux Survival Basics Sensitive output

Find SSH Too Many Authentication Failures Lines

The auth log proves whether the server refused after too many offered keys.

grep -i 'Too many authentication failures' /var/log/auth.log /var/log/secure 2>/dev/null | tail -20
Linux Survival Basics Sensitive output

List SSH Agent Keys

Too many loaded keys can cause the client to offer the wrong identities first.

ssh-add -l
Linux Survival Basics Sensitive output

Test SSH with One Identity File

Force one key when the agent offers too many identities.

ssh -o IdentitiesOnly=yes -i ~/.ssh/id_ed25519 user@example.com
Linux Survival Basics Sensitive output

Show Served Certificate SANs

SANs decide which hostnames the certificate covers.

openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -ext subjectAltName
Linux Survival Basics Can be slow

List Enabled Apache Sites

Apache may not be using the vhost file you edited.

find /etc/apache2/sites-enabled -maxdepth 1 -type l -printf '%f -> %l ' 2>/dev/null | sort
Linux Survival Basics Can be slow

Read Recent systemd Timer Logs

Timer logs show whether systemd attempted to trigger the task.

journalctl -u backup.timer --since "24 hours ago" --no-pager
Linux Survival Basics Sensitive output

Run SSH Verbose Auth Test

Verbose SSH shows which key was offered.

ssh -vvv -o IdentitiesOnly=yes -i ~/.ssh/id_ed25519 user@example.com
Linux Survival Basics Read-only

Show HTTPS Certificate with curl

curl can show the certificate path a client actually sees.

curl -Iv https://example.com/ 2>&1 | sed -n "/SSL connection/,/expire date/p"
Linux Survival Basics Read-only

Show Context Around the First App Error

The first error often explains more than the last one.

awk '{buf[NR%5]=$0} tolower($0) ~ /(error|exception|fatal)/ {for (i=NR-4;i<=NR;i++) if (i>0) print buf[i%5]; exit}' /var/log/app/app.log
Linux Survival Basics Can be slow

Spot OOM Kills in the Kernel Journal

Exit code 137 often means the kernel has something to say.

journalctl -k --since "2 hours ago" --no-pager -o short-iso | grep -Ei 'out of memory|oom|killed process'
Linux Survival Basics Read-only

Check Web File Owner and Mode

The file can exist and still be unreadable to Nginx.

stat -c '%A %U:%G %n' /srv/www/site/index.html
Linux Survival Basics Can be slow

Find Nginx root alias and access rules

One deny or alias can explain the whole 403.

grep -RInE 'root|alias|deny|allow' /etc/nginx/sites-enabled /etc/nginx/conf.d 2>/dev/null
Linux Survival Basics Sensitive output

Read Recent LetsEncrypt Log Lines

Certbot logs usually name the failed challenge.

sudo tail -80 /var/log/letsencrypt/letsencrypt.log
Linux Survival Basics Read-only

Check Domain A Records

A renewal can fail because DNS points elsewhere.

dig +short example.com A
Linux Survival Basics Read-only

Show Linux Route Table

No default route means packets have nowhere to leave.

ip route
Linux Survival Basics Read-only

Check logrotate Timer Status

The timer may be disabled, missed, or failing.

systemctl status logrotate.timer --no-pager
Linux Survival Basics Can be slow

Read Recent Logrotate Journal

The journal can show why rotation skipped.

journalctl -u logrotate --since '7 days ago' --no-pager
Linux Survival Basics Read-only

List Installed Debian Kernels

Know installed kernels before cleanup.

dpkg -l 'linux-image*' | awk '/^ii/{print $2,$3}'
Linux Survival Basics Sensitive output

Scan a Host SSH Key

Keyscan collects a presented key; it does not verify trust.

ssh-keyscan -t ed25519 hostname
Linux Survival Basics Read-only

Show the Real User Cron Jobs

Cron problems often hide behind comments, blank lines, and copied folklore.

crontab -l | sed -n '/^[[:space:]]*#/d;/^[[:space:]]*$/d;p'
Linux Survival Basics Read-only

Turn Cron Into a Readable Table

Cron is easier to debug when the schedule and command stop blending together.

crontab -l | awk 'NF && $1 !~ /^#/ {printf "%-16s %s\n", $1" "$2" "$3" "$4" "$5, substr($0,index($0,$6))}'
Linux Survival Basics Read-only

Map systemd Timers to Services

A timer is only half the scheduled job. The service is the payload.

systemctl list-timers --all --no-pager --plain | awk 'NR==1 || /\.timer/ {print $(NF-1), "->", $NF}'
Linux Survival Basics Can be slow

Find Running Package Manager Processes

A package lock is usually a symptom, not the first thing to delete.

ps -ef | grep -E 'apt|dpkg|dnf|yum|pacman' | grep -v grep
Linux Survival Basics Sensitive output

Find the dpkg Lock Owner

Find the process holding the dpkg lock before touching lock files.

sudo lsof /var/lib/dpkg/lock-frontend /var/lib/dpkg/lock 2>/dev/null
Linux Survival Basics Read-only

Check apt Daily Timers

Automatic apt timers can explain locks that seem mysterious.

systemctl list-timers apt-daily* --no-pager
Linux Survival Basics Can be slow

Find CRLF Lines in a Script

Find exactly which lines still contain carriage returns.

grep -n $'\r' script.sh | head
Linux Survival Basics Read-only

Verify fstab with findmnt

Bad fstab entries should be found before the next reboot.

findmnt --verify
Linux Survival Basics Read-only

Show Failed Mount Units

systemd often records mount failures as failed units.

systemctl --failed --no-pager
Linux Survival Basics Can be slow

Read Mount and fstab Warnings

The boot journal often names the mount or dependency that failed.

journalctl -b -p warning --no-pager | grep -iE 'mount|fstab|dependency'
Linux Survival Basics Can be slow

Find OOM Killer Lines in the Kernel Journal

Before restarting a service, prove whether the kernel killed it.

journalctl -k --since '24 hours ago' --no-pager | grep -iE 'out of memory|oom-killer|killed process'
Linux Survival Basics Can be slow

Show Top Memory Processes

Find current memory owners before restarting workloads.

ps -eo pid,comm,%mem,%cpu --sort=-%mem | head
Linux Survival Basics Sensitive output

Check a User Identity Record

Start sudo troubleshooting with the actual target account.

id username
Linux Survival Basics Sensitive output

Show User Groups

Group membership explains many sudo and access failures.

groups username
Linux Survival Basics Sensitive output

List sudo Group Members

Before granting sudo, see who already has it.

getent group sudo
Linux Survival Basics Read-only

Show a Service LimitNOFILE

A shell ulimit is not the service limit.

systemctl show nginx -p LimitNOFILE --no-pager
Linux Survival Basics Sensitive output

Inspect One Process Open Files

Look at one target process, not the whole host, when pressure is scoped.

sudo lsof -p 1234 | head
Linux Survival Basics Read-only

List Tables in a SQLite Database

Before querying a database file, see what tables are actually inside it.

sqlite3 app.db ".tables"
Linux Survival Basics Can be slow

List URLs from a Sitemap

Before comparing sitemap coverage, print the URLs plainly.

grep -o '<loc>[^<]*</loc>' public/sitemap.xml | sed 's#<loc>##;s#</loc>##'
Linux Survival Basics Read-only

Show Failed systemd Units

One command tells you which services systemd already knows are broken.

systemctl --failed --no-pager
Linux Survival Basics Read-only

Inspect One Service Without Pager Traps

Make systemctl status safe for scripts, screenshots, and quick incident notes.

systemctl status nginx --no-pager --lines=30
Linux Survival Basics Read-only

Check If a Service Is Active

Get a clean yes-or-no service state without the full status page.

systemctl is-active nginx
Linux Survival Basics Sensitive output

Show Recent Server Reboots

Confirm whether the server actually rebooted and when.

last -x reboot | head -5
Linux Survival Basics Read-only

List Upcoming systemd Timers

Cron is not the only scheduler on modern Linux servers.

systemctl list-timers --all --no-pager
Linux Survival Basics Read-only

Print the Exact systemd Exit Fields

Turn a noisy service failure into four fields you can paste into an incident note.

systemctl show app-worker --property=Result,ExecMainCode,ExecMainStatus,NRestarts --no-pager