DEV Community

JESTINKSUNIL
JESTINKSUNIL

Posted on

Ubuntu CLI cheat sheet

System

System information

# Displays all system information.
uname -a
# Shows current hostname and related details.
hostnamectl
# Lists CPU architecture information.
lscpu
# Shows system time.
timedatectl status
Enter fullscreen mode Exit fullscreen mode

System monitoring and management

# Displays real-time system processes.
top
# An interactive process viewer (needs installation).
htop
# Shows disk usage in a human-readable format.
df -h
# Displays free and used memory in MB.
free -m
# Terminates a process.
kill <process id>
Enter fullscreen mode Exit fullscreen mode

Running commands

# Runs command in the background.
[command] &
# Displays background commands.
jobs
# Brings command to the foreground.
fg <command number>
Enter fullscreen mode Exit fullscreen mode

Service management

# Starts a service.
sudo systemctl start <service>
# Stops a service.
sudo systemctl stop <service>
# Checks the status of a service.
sudo systemctl status <service>
# Reloads a service’s configuration without interrupting its operation.
sudo systemctl reload <service>
# Follows the journal, showing new log messages in real time.  
journalctl -f
# Displays logs for a specific systemd unit.
journalctl -u <unit_name>
Enter fullscreen mode Exit fullscreen mode

Cron jobs and scheduling

# Edits cron jobs for the current user.
crontab -e
# Lists cron jobs for the current user.
crontab -l
Enter fullscreen mode Exit fullscreen mode

Files

File management

# Lists files and directories.
ls
# Creates an empty file or updates the last accessed date.
touch <filename>
# Copies files from source to destination.
cp <source> <destination>
# Moves files or renames them.
mv <source> <destination>
# Deletes a file.
rm <filename>
Enter fullscreen mode Exit fullscreen mode

Directory navigation

# Displays the current directory path.
pwd
# Changes the current directory.
cd <directory>
# Creates a new directory.
mkdir <dirname>
Enter fullscreen mode Exit fullscreen mode

File permissions and ownership

# Changes file permissions.
chmod [who][+/-][permissions] <file>
# Makes a file executable by its owner.
chmod u+x <file>
# Changes file owner and group.
chown [user]:[group] <file>
Enter fullscreen mode Exit fullscreen mode

Searching and finding

# Finds files and directories.
find [directory] -name <search_pattern>
# Searches for a pattern in files.
grep <search_pattern> <file>
Enter fullscreen mode Exit fullscreen mode

Archiving and compression

# Compresses files into a tar.gz archive.
tar -czvf <name.tar.gz> [files]
# Extracts a compressed tar archive.
tar -xvf <name.tar.[gz|bz|xz]> [destination]
Enter fullscreen mode Exit fullscreen mode

Text editing and processing

# Opens a file in the Nano text editor.
nano [file]
# Displays the contents of a file.
cat <file>
# Displays the paginated content of a file.
less <file>
# Shows the first few lines of a file.
head <file>
# Shows the last few lines of a file.
tail <file>
# Prints every line in a file.
awk '{print}' [file]
Enter fullscreen mode Exit fullscreen mode

Packages

Package management (APT)

# Installs a package.
sudo apt install <package>
# Reinstalls a broken package.
sudo apt install -f --reinstall <package>
# Searches for APT packages.
apt search <package>
# Lists available package versions.
apt-cache policy <package>
# Updates package lists.
sudo apt update
# Upgrades all upgradable packages.
sudo apt upgrade
# Removes a package.
sudo apt remove <package>
# Removes a package and all its configuration files.
sudo apt purge <package>
Enter fullscreen mode Exit fullscreen mode

Package management (Snap)

# Searches for Snap packages.
snap find <package>
# Installs a Snap package.
sudo snap install <snap_name>
# Removes a Snap package.
sudo snap remove <snap_name>
# Updates all installed Snap packages.
sudo snap refresh
# Lists all installed Snap packages.
snap list
# Displays information about a Snap package.
snap info <snap_name>
Enter fullscreen mode Exit fullscreen mode

Users & Groups

User management

# Shows which users are logged in.
w
# Creates a new user.
sudo adduser <username>
# Deletes a user.
sudo deluser <username>
# Sets or changes the password for a user.
sudo passwd <username>
# Switches user.
su <username>
# Locks a user account.
sudo passwd -l <username>
# Unlocks a user password.
sudo passwd -u <username>
# Sets user password expiration date.
sudo chage <username>
Enter fullscreen mode Exit fullscreen mode

Group management

# Displays user and group IDs.
id [username]
# Shows the groups a user belongs to.
groups [username]
# Creates a new group.
sudo addgroup <groupname>
# Deletes a group.
sudo delgroup <groupname>
Enter fullscreen mode Exit fullscreen mode

Networking

Networking Commands

# Displays network interfaces and IP addresses.
ip addr show
# Shows network statistics.
ip -s link
# Shows listening sockets.
ss -l
# Pings a host and outputs results.
ping <host>
Enter fullscreen mode Exit fullscreen mode

Netplan Configuration

# Displays the current Netplan configuration.
cat /etc/netplan/*.yaml
# Tests a new configuration for a set period of time.
sudo netplan try
# Applies the current Netplan configuration.
sudo netplan apply
Enter fullscreen mode Exit fullscreen mode

Firewall Management

# Displays the status of the firewall.
sudo ufw status
# Enables the firewall.
sudo ufw enable
# Disables the firewall.
sudo ufw disable
# Allows traffic on a specific port or service.
sudo ufw allow <port/service>
# Denies traffic on a specific port or service.
sudo ufw deny <port/service>
# Deletes an existing rule.
sudo ufw delete allow/deny <port/service>
Enter fullscreen mode Exit fullscreen mode

SSH and Remote Access

# Connects to a remote host via SSH.
ssh <user@host>
# Securely copies files between hosts.
scp <source> <user@host>:<destination>
Enter fullscreen mode Exit fullscreen mode

LXD

LXD is a modern, secure, and powerful tool that provides a unified experience for
running and managing containers or virtual machines. Visit LXD Official Site for more information.

Initialization

# Initializes LXD before first use.
lxd init
Enter fullscreen mode Exit fullscreen mode

Creating Instances

# Creates a LXC system container (without starting it).
lxc init ubuntu:22.04 <container_name>

# Creates and starts a LXC system container.
lxc launch ubuntu:24.04 <container_name>

# Creates and starts a virtual machine.
lxc launch ubuntu:22.04 <vm_name> --vm
Enter fullscreen mode Exit fullscreen mode

Managing Instances

# Lists instances.
lxc list

# Shows status information about an instance.
lxc info <instance>

# Starts an instance.
lxc start <instance>

# Stops an instance.
lxc stop <instance> [--force]

# Deletes an instance.
lxc delete <instance> [--force|--interactive]
Enter fullscreen mode Exit fullscreen mode

Accessing Instances

# Runs a command inside an instance.
lxc exec <instance> -- <command>

# Gets shell access to an instance (if bash is installed).
lxc exec <instance> -- bash

# Gets console access to an instance.
lxc console <instance> [flags]

# Pulls a file from an instance.
lxc file pull <instance>/<instance_filepath> <local_filepath>

# Pushes a file to an instance.
lxc file push <local_filepath> <instance>/<instance_filepath>
Enter fullscreen mode Exit fullscreen mode

Using Projects

# Creates a project.
lxc project create <project> [--config <option>]

# Configures a project.
lxc project set <project> <option>

# Switches to a project.
lxc project switch <project>
Enter fullscreen mode Exit fullscreen mode

Ubuntu Pro

Ubuntu Pro delivers 10 years of expanded security coverage on top of Ubuntu’s Long Term
Support (LTS) commitment in addition to management and compliance tooling. Visit Ubuntu Pro to register for free on up to five machines.

Activating Ubuntu Pro

# Attaches your machine to Ubuntu Pro using a specific token.
sudo pro attach <token>
Enter fullscreen mode Exit fullscreen mode

Managing Services

# Displays the status of all Ubuntu Pro services.
sudo pro status

# Enables a specific Ubuntu Pro service, like ESM, FIPS, or Livepatch.
sudo pro enable <service>

# Disables a specific Ubuntu Pro service.
sudo pro disable <service>
Enter fullscreen mode Exit fullscreen mode

Extended Security Maintenance (ESM)

# Activates Extended Security Maintenance for infrastructure packages.
sudo pro enable esm-infra

# Activates ESM for applications, extending security coverage.
sudo pro enable esm-apps
Enter fullscreen mode Exit fullscreen mode

Livepatch Service

# Enables the Livepatch service, which applies critical kernel patches without rebooting.
sudo pro enable livepatch
Enter fullscreen mode Exit fullscreen mode

FIPS Mode

# Enables FIPS (Federal Information Processing Standards) mode, enforcing strict cryptographic standards.
sudo pro enable fips
Enter fullscreen mode Exit fullscreen mode

Updating Configuration

# Refreshes the Ubuntu Pro state to ensure the latest configuration and services are in place.
sudo pro refresh
Enter fullscreen mode Exit fullscreen mode

Detaching Ubuntu Pro

# Detaches the machine from Ubuntu Pro, disabling all services.
sudo pro detach
Enter fullscreen mode Exit fullscreen mode

This is a complete and structured Ubuntu CLI cheat sheet.

Top comments (0)