Ubuntu Common Commands - CodeSwordsman

Frequently used Ubuntu commands for APT packages, systemd services, Netplan networking, disk mounts and UFW firewall.

Ubuntu 20.04 / 22.04 / 24.04Updated 2026-10-2937 commands

Package Management (APT)

Refresh package index before installing anything

apt update

Upgrade installed packages

apt upgrade -y

Install a package without prompting

apt install -y nginx

Remove a package together with its config files

apt remove --purge nginx

Remove automatically installed unused dependencies

apt autoremove --purge

Search packages in configured repositories

apt search nginx

Check whether a package is installed

apt list --installed | grep nginx

Install a local deb package

dpkg -i ./package.deb

List installed deb packages and versions

dpkg -l | grep nginx

Service Management (systemd)

Start a service

systemctl start nginx

Restart a service

systemctl restart nginx

Enable at boot and start immediately

systemctl enable --now nginx

Reload unit files after editing them

systemctl daemon-reload

List currently running services

systemctl list-units --type=service --state=running

Show today's logs for a service

journalctl -u nginx --since today

Networking (Netplan)

List Netplan configuration files

ls /etc/netplan/

Test a config and auto-rollback on timeout to avoid losing access

sudo netplan try

Apply the network configuration

sudo netplan apply

Show the routing table and default gateway

ip route

Show DNS resolver status

resolvectl status

List locally listening ports

ss -tunlp

Disks & Mounts

Show block devices, filesystems and mount points

lsblk -f

List disk partition information

sudo fdisk -l

Mount a partition temporarily

sudo mount /dev/sdb1 /data

Show partition UUID for use in fstab

sudo blkid /dev/sdb1

Show disk usage and filesystem types

df -hT

Remount everything in fstab to validate the configuration

sudo mount -a

System Info & Time

Show Ubuntu version details

lsb_release -a

Set the system hostname

hostnamectl set-hostname web01

Set the timezone to Asia/Shanghai

timedatectl set-timezone Asia/Shanghai

Check time synchronization status

timedatectl status

Show the running kernel version

uname -r

Firewall (UFW)

Show firewall status and existing rules

sudo ufw status verbose

Allow SSH before enabling the firewall

sudo ufw allow 22/tcp

Allow a port from a specific subnet

sudo ufw allow from 10.0.0.0/8 to any port 3306

Enable the firewall

sudo ufw enable

Delete an existing allow rule

sudo ufw delete allow 80/tcp

This website uses cookies to ensure you get the best experience on our website. Privacy Policy.