Linux

Ubuntu Server CLI Cheat Sheet

Ubuntu Server CLI Cheat Sheet. A look at common Ubuntu Server CLI commands for networking, security, virtualization, Kubernetes, etc

One of the beautiful things about Linux is the power of the Linux CLI. This is what draws many to Linux to begin with. Granted, Windows has come a long way since the introduction of PowerShell, and now especially with PowerShell Core that is cross-platform. Many can argue that Windows may be on par with Linux. However, we want to take a look at some of the most helpful Ubuntu Server CLI commands that IT and Linux admins will want to be familiar with and that will be helpful in their administrator of Ubuntu Server. This Ubuntu Server CLI Cheat Sheet is a compilation of commands for administering everything from the network, security, packages, files, virtualization, and Kubernetes.

Ubuntu Server CLI Cheat Sheet

The cheat sheet is broken into the following categories:

  • Networking
  • Security
  • Packages
  • Files
  • System
  • Virtualization
  • Kubernetes and containers
  • OpenStack

Networking

Get the IP address of all interfaces

networkctl status

Display all IP addresses of the host

hostname -I

Enable/disable interface

ip link set  up ip link set  down

Manage firewall rules

enable firewall: sudo ufw enable 
list rules: sudo ufw status 
allow port: sudo ufw allow  
deny port: sudo ufw deny 

Connect remotely through SSH

ssh <user>@<host IP>

Security

Show which users are logged in

w

Get password expiration date for

chage -l 

Set password expiration date for

sudo chage 

Lock a user account

sudo passwd -l 

Unlock a user account

sudo passwd -u 

List open ports and associated
processes

sudo netstat -tulpn

Automatically detect and ban
abusive IP addresses

sudo apt install fail2ban

Show banned IP addresses

sudo fail2ban-client status 
sudo fail2ban-client status 

Enable kernel live patching

sudo snap install canonical-livepatch 
sudo canonical-livepatch enable 

Packages

Search for packages

apt search <string>
snap find <string>

List available updates

apt list --upgradable

Apply all available updates

sudo apt update && sudo apt upgrade

Install from the Ubuntu archive

sudo apt install <package>

Install from the snap store

sudo snap install <package>

Which package provides this file?

sudo apt install apt-file
sudo apt-file update
apt-file <filename or command>

Get the support status for installed packages

ubuntu-support-status

Files

List files

ls

List files with permissions and dates

ls -al

Common file operations

create empty: touch <filename>
create with content: echo "<content>" > <filename>  
append content: echo "<content>" >>  <filename>
display a text file: cat <file>  
copy: cp <file> <target filename>
move/rename: mv <file> <target directory/filename>
delete: rm 

Create a directory

mkdir <directory>

Create directories recursively

mkdir -p <directory1>/<directory2>

Delete a directory recursively

rm -r <directory>

Quick file search

locate <q>

Search string in file

grep <string> <filename>

Search string recursively in directory

grep -Iris <string> <directory>

Find files modified in the last n minutes

find <directory> -mmin -<n> -type f
eg. find . -mmin -5 -type f

Show only the nth column

col<n> "<separator>" <filename>
eg. col2 "," foo.csv

Display file paginated

less <filename>

Display first n lines

head -n <n> <filename>

Display last n lines

tail -n <n> <filename>

Follow file content as it increases

tail -f <filename>

Pack a directory into an archive

zip: zip -r <target> <source dir>
tar.gz: tar cvzf <target>.tar.gz <source dir>

Unpack an archive

zip: unzip <zip file>
tar.gz: tar xf <tar.gz file>

Copy file to remote server

scp <filename> <user@server>:<destination>
eg. scp config.yaml [email protected]:/config 

Copy directory recursively from remote server

scp -r <user@server>:<source> <destination>
eg. scp -r [email protected]:/config /tmp

System

Display kernel version

uname -r

Get disk usage

df -h

Get memory usage

cat /proc/meminfo

Get system time

timedatectl status

Set system timezone

timedatectl list-timezones
sudo timedatectl set-timezone <zone>

Get all running services

systemctl --state running

Start or stop a service

service <service> start/stop

Monitor new logs for a service

journalctl -u <service> --since now -f

Get the list of recent logins

last

Display running processes

htop

Kill process by id

kill <process id>

Kill process by name

pkill <process name>

Run command in the background

<command> &

Display background commands

jobs

Bring command <n> to the foreground

fg <n>

Kubernetes and containers

Install MicroK8s and list available add-ons

sudo snap install microk8s --classic
microk8s.status --wait-ready

Enable a MicroK8s add-on

microk8s.enable <service>

View MicroK8s nodes and running services

microk8s.kubectl get nodes
microk8s.kubectl get services

Launch a LXD container

lxd init
lxc launch ubuntu:18.04 <container name>

Or another distro

lxc launch images:centos/8/amd64
<container name>

Get a shell inside a LXD container

lxc exec <name> -- /bin/bash

Push a file to a LXD container

lxc file push <filename>
<container name>/<path>

Pull a file from a LXD container

lxc file pull <destination>
<container name>/<file path>

Virtualization

Install Multipass and launch an Ubuntu VM

sudo snap install multipass --classic
multipass launch <image> --name <VM name>

Omitting <image> will launch a VM with
the latest Ubuntu LTS

Find available images

multipass find

List existing VMs

multipass list

Get a shell inside a VM

multipass shell <VM name>

Openstack

Install OpenStack and launch an instance

sudo snap install microstack --classic
sudo microstack.init
microstack.launch
The Horizon dashboard is available at 10.20.20.1
Default credentials: admin / keystone

Subscribe to VirtualizationHowto via Email ๐Ÿ””

Enter your email address to subscribe to this blog and receive notifications of new posts by email.



Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at Virtualizationhowto.com, and a 7-time VMware vExpert, with over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, He has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family. Also, he goes through the effort of testing and troubleshooting issues, so you don't have to.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.