Skip to content

Linux Command Reference

This page is deliberately a cheatsheet, not a tutorial: the commands you need a few times a year and never quite remember. Each section starts with the situation you are probably in when you need it. All commands tested on Ubuntu 22.04+.


System Information

You are on an unfamiliar box — or one you set up so long ago it might as well be — and need to know what you are dealing with.

Command Description
hostname Show the hostname
grep PRETTY_NAME /etc/os-release Show OS name and version
uname -a Kernel version, architecture, hostname
lscpu CPU model, cores, architecture (readable summary)
lspci -tv List all PCI devices
lsusb -tv List all USB devices
lsmod List loaded kernel modules
uptime Uptime and load averages in one line

Resources

Something is slow or full and you need to find out what.

Command Description
free -h RAM and swap usage
df -h Disk usage per filesystem
df -i Inode usage — "disk full" with free space means this
du -sh <dir> Total size of a directory
du -h --max-depth=1 <dir> \| sort -h Subdirectory sizes, largest last — drills down to the space eater
fdisk -l List all disks and partitions
swapon -s Show active swap devices

Running out of disk on an LVM setup? See Extend LVM Volume. Swap full after a memory spike? See Clear Swap Without Reboot.


Processes

Load is high, the fan is loud, or a service refuses to die.

Command Description
htop Interactive process viewer (or top if not installed)
ps aux --sort=-%mem \| head Top memory consumers
ps aux --sort=-%cpu \| head Top CPU consumers
pgrep -a <name> Find PIDs by process name
kill <pid> Ask a process to terminate (SIGTERM)
kill -9 <pid> Force kill (SIGKILL) — last resort, no cleanup happens
lsof -p <pid> Files and sockets a process has open

Networking

Connectivity is broken, or you want to know what is listening on this machine.

Modern tools

ifconfig and netstat are deprecated and no longer installed by default. Ubuntu 18.04+ uses ip and ss from the iproute2 suite. The old commands are listed here only so you can translate muscle memory.

Deprecated Modern replacement Description
ifconfig ip a Show all interfaces and IPs
ifconfig eth0 ip a show eth0 Show a specific interface
route -n ip route Show routing table
netstat -antp ss -antp All active connections with PID
netstat -tulpn ss -tulpn Listening TCP/UDP sockets with owning process

ss -tulpn is the one to remember: it answers "what is listening on which port, and which process owns it" — the first question in most firewall and service debugging. Speaking of which: UFW Firewall.


Services (systemctl)

A service will not start, or you are not sure what is even running.

Command Description
systemctl status <service> Status, recent log lines, and the exact failure reason
systemctl list-units --type=service --state=running All currently running services
systemctl list-units --type=service --state=failed Failed services — the short list of suspects
journalctl -u <service> -f Follow logs for a service live
journalctl -u <service> -n 50 Last 50 log lines for a service
journalctl -p err -b All errors since the last boot, across all services

Users

Who has access to this machine, and who is on it right now?

Command Description
w Who is logged in and what they are running
last Login history
id <user> UID, GID, and group memberships
cut -d: -f1 /etc/passwd List all user accounts
cut -d: -f1 /etc/group List all groups
crontab -l Cron jobs of the current user
crontab -l -u <user> Cron jobs of another user (as root)

For one-time scheduled commands instead of recurring cron jobs, see Schedule One-Time Commands.


APT Package Management

Installing, removing, and cleaning up packages.

apt <command>
Command Description
update Refresh the package index — run before upgrade or install
upgrade Upgrade installed packages; never removes anything
full-upgrade Upgrade and remove packages if the dependency resolution requires it (dist-upgrade is the older apt-get name for the same thing)
install <pkg> Install a package
remove <pkg> Remove a package, keep its config files
purge <pkg> Remove a package including config files
autoremove Remove dependencies nothing needs anymore
clean Delete cached package downloads
show <pkg> Package details, dependencies, description
list --installed All installed packages

The practical difference between upgrade and full-upgrade: upgrade is the safe everyday choice; full-upgrade is needed when an upgrade requires replacing or removing packages — typically on release upgrades or kernel transitions.

To automate security updates instead of running these by hand, see Unattended Upgrades.