Ubuntu Server CLI Cheat Sheet 2024 v6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Ubuntu CLI cheat sheet

1 of 3

System Files

System information File management


uname -a : Displays all system information. ls : Lists files and directories.
hostnamectl : Shows current hostname and related details. touch <filename> : Creates an empty file or updates the last accessed date.
lscpu : Lists CPU architecture information. cp <source> <destination> : Copies files from source to destination.
timedatectl status : Shows system time. mv <source> <destination> : Moves files or renames them.
rm <filename> : Deletes a file.
System monitoring and management
top : Displays real-time system processes. Directory navigation
htop : An interactive process viewer (needs installation). pwd : Displays the current directory path.
df -h : Shows disk usage in a human-readable format. cd <directory> : Changes the current directory.
free -m : Displays free and used memory in MB. mkdir <dirname> : Creates a new directory.
kill <process id> : Terminates a process.
File permissions and ownership
Running commands chmod [who][+/-][permissions] <file> : Changes file permissions.
[command] & : Runs command in the background. chmod u+x <file> : Makes a file executable by its owner.
jobs : Displays background commands. chown [user]:[group] <file> : Changes file owner and group.
fg <command number> : Brings command to the foreground.
Searching and finding
Service management find [directory] -name <search_pattern> : Finds files and directories.
sudo systemctl start <service> : Starts a service. grep <search_pattern> <file> : Searches for a pattern in files.
sudo systemctl stop <service> : Stops a service
sudo systemctl status <service> : Checks the status of a service. Archiving and compression
sudo systemctl reload <service> : Reloads a service’s configuration without tar -czvf <name.tar.gz> [files] : Compresses files into a tar.gz archive.
interrupting its operation. tar -xvf <name.tar.[gz|bz|xz]> [destination] : Extracts a compressed tar archive.
journalctl -f : Follows the journal, showing new log messages in real time.
journalctl -u <unit_name> : Displays logs for a specific systemd unit. Text editing and processing
nano [file] : Opens a file in the Nano text editor.
Cron jobs and scheduling cat <file> : Displays the contents of a file.
crontab -e : Edits cron jobs for the current user. less <file> : Displays the paginated content of a file.
crontab -l : Lists cron jobs for the current user. head <file> : Shows the first few lines of a file.
tail <file> : Shows the last few lines of a file.
awk ‘{print}’ [file] : Prints every line in a file.
2024
Ubuntu CLI cheat sheet
2 of 3

Packages Networking

Package management (APT) Networking


sudo apt install <package> : Installs a package. ip addr show : Displays network interfaces and IP addresses.
sudo apt install -f –reinstall <package> : Reinstalls a broken package. ip -s link : Shows network statistics.
apt search <package> : Searches for APT packages. ss -l : Shows listening sockets.
apt-cache policy <package> : Lists available package versions. ping <host> : Pings a host and outputs results.
sudo apt update : Updates package lists.
sudo apt upgrade : Upgrades all upgradable packages. Netplan configuration (read more at netplan.io)
sudo apt remove <package> : Removes a package. cat /etc/netplan/*.yaml : Displays the current Netplan configuration.
sudo apt purge <package> : Removes a package and all its configuration files. sudo netplan try : Tests a new configuration for a set period of time.
sudo netplan apply : Applies the current Netplan configuration.
Package management (Snap)
snap find <package> : Search for Snap packages. Firewall management
sudo snap install <snap_name> : Installs a Snap package. sudo ufw status : Displays the status of the firewall.
sudo snap remove <snap_name> : Removes a Snap package. sudo ufw enable : Enables the firewall.
sudo snap refresh : Updates all installed Snap packages. sudo ufw disable : Disables the firewall.
snap list : Lists all installed Snap packages. sudo ufw allow <port/service> : Allows traffic on a specific port or service.
snap info <snap_name> : Displays information about a Snap package. sudo ufw deny <port/service> : Denies traffic on a specific port or service.
sudo ufw delete allow/deny <port/service> : Deletes an existing rule.

SSH and remote access


Users & groups ssh <user@host> : Connects to a remote host via SSH.
scp <source> <user@host>:<destination> : Securely copies files between hosts.
User management
w : Shows which users are logged in. Firewall management
sudo adduser <username> : Creates a new user. sudo ufw status : Displays the status of the firewall.
sudo deluser <username> : Deletes a user. sudo ufw enable : Enables the firewall.
sudo passwd <username> : Sets or changes the password for a user. sudo ufw disable : Disables the firewall.
su <username> : Switches user. sudo ufw allow <port/service> : Allows traffic on a specific port or service.
sudo passwd -l <username> : Locks a user account. sudo ufw deny <port/service> : Denies traffic on a specific port or service.
sudo passwd -u <username> : Unlocks a user password. sudo ufw delete allow/deny <port/service> : Deletes an existing rule.
Sudo change <username> : Sets user password expiration date.
SSH and remote access
Group management ssh <user@host> : Connects to a remote host via SSH.
id [username] : Displays user and group IDs. scp <source> <user@host>:<destination> : Securely copies files between hosts.
groups [username] : Shows the groups a user belongs to.
2024

sudo addgroup <groupname> : Creates a new group.


sudo delgroup <groupname> : Deletes a group.
Ubuntu CLI cheat sheet
3 of 3

LXD Ubuntu Pro

LXD is a modern, secure and powerful tool that provides a unified experience for Ubuntu Pro delivers 10 years of expanded security coverage on top of Ubuntu’s Long Term
running and managing containers or virtual machines. Visit https ://canonical.com/lxd Support (LTS) commitment in addition to management and compliance tooling.
for more information. Visit https://ubuntu.com/pro to register for free on up to five machines.

lxd init : initializes LXD before first use Activating Ubuntu Pro
sudo pro attach <token> : Attaches your machine to Ubuntu Pro using a specific
Creating instances token. This token is provided when you subscribe to Ubuntu Pro.
lxc init ubuntu:22.04 <container name> : Creates a lxc system container (without
starting it). Managing services
lxc launch ubuntu:24.04 <container name> : Creates and starts a lxc system sudo pro status : Displays the status of all Ubuntu Pro services.
container. sudo pro enable <service> : Enables a specific Ubuntu Pro service, like ESM, FIPS,
lxc launch ubuntu:22.04 <vm name> --vm : Creates and starts a virtual machine. or Livepatch.
sudo pro disable <service> : Disables a specific Ubuntu Pro service.
Managing instances
lxc list : Lists instances. Extended Security Maintenance (ESM)
lxc info <instance> : Shows status information about an instance. sudo pro enable esm-infra : Activates Extended Security Maintenance for infrastructure
lxc start <instance> : Starts an instance. packages, providing security updates beyond the standard release cycle.
lxc stop <instance> [--force] : Stops an instance. sudo pro enable esm-apps : Activates ESM for applications, extending security
lxc delete <instance> [--force|--interactive] : Deletes an instance. coverage for specific applications.

Accessing instances Livepatch service


lxc exec <instance> -- <command> : Runs a command inside an instance. sudo pro enable livepatch : Enables the Livepatch service, which applies critical
lxc exec <instance> -- bash : Gets shell access to an instance (if bash is kernel patches without rebooting.
installed).
lxc console <instance> [flags] : Gets console access to an instance. FIPS mode
lxc file pull <instance>/<instance_filepath> <local_filepath> : Pulls a file from sudo pro enable fips : Enables FIPS (Federal Information Processing Standards) mode,
an instance. enforcing strict cryptographic standards and practices.
lxc file pull <local_filepath> <instance>/<instance_filepath> : Pushes a file to
an instance. Updating configuration
sudo pro refresh : Refreshes the Ubuntu Pro state to ensure the latest
Using projects configuration and services are in place.
lxc project create <project> [--config <option>] : Creates a project.
lxc project set <project> <option> : Configures a project. Detaching Ubuntu Pro
lxc project switch <project> : Switches to a project. sudo pro detach : Detaches the machine from Ubuntu Pro, disabling all services.
2024

You might also like