Essential Ubuntu Commands: A Practical CLI Cheat Sheet
If you manage an Ubuntu server, these are some of the most useful commands to know. This cheat sheet covers system updates, files, directories, disk space, services, networking, logs, permissions, processes, and other everyday server administration tasks.
Most examples work on modern Ubuntu releases, including Ubuntu 24.04 LTS.
System Updates
Update the package list
Checks configured repositories for the latest available packages. This does not install updates.
sudo apt update
See available updates
apt list --upgradable
Install available updates
sudo apt upgrade
Perform a full upgrade
Allows APT to install or remove packages when necessary to complete an upgrade.
sudo apt full-upgrade
Always review the packages APT proposes to remove before approving a full upgrade on a production server.
Remove packages that are no longer needed
sudo apt autoremove
Clean the package cache
sudo apt autoclean
Install a package
sudo apt install package-name
Example:
sudo apt install htop
Remove a package
sudo apt remove package-name
Completely remove a package and its configuration
sudo apt purge package-name
System Information
Check the Ubuntu version
lsb_release -a
Or:
cat /etc/os-release
Check the kernel version
uname -r
Show detailed system information
hostnamectl
Check how long the server has been running
uptime
Check the current hostname
hostname
Check the current date and time
date
Check timezone configuration
timedatectl
CPU and Memory
Check memory usage
free -h
View running processes and resource usage
top
Use a more user-friendly process monitor
htop
If it is not installed:
sudo apt install htop
Check CPU information
lscpu
Check the number of CPU cores
nproc
Disk Space
Check disk usage
df -h
Check the size of the current directory
du -sh .
Check the size of a specific directory
du -sh /var/log
Show the size of files and directories in the current location
du -sh *
Sort directories by size
du -sh * | sort -h
View disks and partitions
lsblk
Interactive disk-space analyzer
ncdu
Install it with:
sudo apt install ncdu
Then, for example:
sudo ncdu /
Navigating the Filesystem
Show your current location
pwd
List files
ls
Detailed file listing
ls -lh
Show hidden files
ls -la
Detailed listing with human-readable file sizes
ls -lah
Change directory
cd /path/to/directory
Go up one directory
cd ..
Go to your home directory
cd ~
Go to the previous directory
cd -
Creating, Copying, Moving, and Deleting Files
Create a directory
mkdir new-directory
Create nested directories
mkdir -p path/to/new/directory
Create an empty file
touch file.txt
Copy a file
cp file.txt copy.txt
Copy a directory
cp -r source-directory destination-directory
Move a file
mv file.txt /new/location/
Rename a file
mv old-name.txt new-name.txt
Delete a file
rm file.txt
Delete an empty directory
rmdir directory-name
Delete a directory and everything inside it
rm -rf directory-name
Be extremely careful with rm -rf. Deleted files do not go to a recycle bin.
Viewing and Editing Files
Display an entire file
cat file.txt
Scroll through a large file
less file.txt
Press q to exit.
Show the first 10 lines
head file.txt
Show the last 10 lines
tail file.txt
Show the last 100 lines
tail -n 100 file.txt
Watch a file for new lines in real time
Excellent for monitoring log files:
tail -f /var/log/nginx/error.log
Press Ctrl+C to stop watching.
Edit a file with Nano
nano file.txt
In Nano:
Ctrl+Osaves the fileCtrl+XexitsCtrl+WsearchesCtrl+Kcuts a lineCtrl+Upastes a line
Searching for Files
Find a file by name
find /path -name "filename.txt"
Search the entire server:
find / -name "filename.txt" 2>/dev/null
Case-insensitive search
find / -iname "filename.txt" 2>/dev/null
Find directories
find / -type d -name "directory-name" 2>/dev/null
Searching Inside Files
Search a file for text
grep "search text" file.txt
Case-insensitive search
grep -i "error" file.txt
Search recursively through a directory
grep -R "search text" /path/to/directory
Include line numbers
grep -n "search text" file.txt
Search logs for errors
grep -i "error" /var/log/nginx/error.log
Users and Permissions
Show the current user
whoami
Show user and group information
id
Show groups
groups
Change a user’s password
passwd username
Change file ownership
chown user:group file.txt
Recursively change ownership:
chown -R user:group directory/
Set standard file permissions
chmod 644 file.txt
Set standard directory permissions
chmod 755 directory/
Make a script executable
chmod +x script.sh
Run a command as another user
sudo -u username command
Managing Services with systemd
Check a service
systemctl status nginx
Start a service
sudo systemctl start nginx
Stop a service
sudo systemctl stop nginx
Restart a service
sudo systemctl restart nginx
Reload a service without a full restart
sudo systemctl reload nginx
Check whether a service is running
systemctl is-active nginx
Enable a service at startup
sudo systemctl enable nginx
Disable a service at startup
sudo systemctl disable nginx
Show failed services
systemctl --failed
Logs and Troubleshooting
View recent system logs
journalctl
Show recent errors
journalctl -xe
View logs for a specific service
journalctl -u nginx
Show the last 100 log entries for a service
journalctl -u nginx -n 100
Watch service logs live
journalctl -u nginx -f
Show logs from the current boot
journalctl -b
Show kernel messages
dmesg
Process Management
Show running processes
ps aux
Find a particular process
ps aux | grep nginx
Find a process ID
pgrep nginx
Stop a process
kill PID
Example:
kill 12345
Forcefully terminate a process
kill -9 PID
Use kill -9 only when a normal termination does not work.
Networking
Show network interfaces and IP addresses
ip addr
Short version:
ip a
Show routing information
ip route
Test connectivity
ping google.com
Check your server’s public IP
curl ifconfig.me
Check HTTP response headers
curl -I https://example.com
Follow redirects
curl -IL https://example.com
Download a file
wget https://example.com/file.zip
Or:
curl -O https://example.com/file.zip
DNS Troubleshooting
Look up a domain
dig example.com
Look up a specific DNS record
dig example.com A
dig example.com MX
dig example.com TXT
dig www.example.com CNAME
Get a short DNS answer
dig +short example.com
Query a specific DNS resolver
dig @1.1.1.1 example.com
Trace DNS delegation
dig +trace example.com
Ports and Connections
Show listening ports and the processes using them
ss -tulpn
Check whether port 443 is listening
ss -tulpn | grep :443
Check whether port 80 is listening
ss -tulpn | grep :80
SSH
Connect to a server
ssh username@server-ip
Connect using a hostname
ssh username@example.com
Generate an SSH key
ssh-keygen
Copy your SSH public key to a server
ssh-copy-id username@server
Copy a local file to a remote server
scp file.txt username@server:/path/
Copy a remote file to your computer
scp username@server:/path/file.txt .
Archives and Compression
Create a ZIP archive
zip -r archive.zip directory/
Extract a ZIP archive
unzip archive.zip
Create a compressed TAR archive
tar -czf backup.tar.gz directory/
Extract a TAR.GZ archive
tar -xzf backup.tar.gz
View the contents without extracting
tar -tzf backup.tar.gz
Command History
Show command history
history
Search your history
history | grep nginx
Repeat the previous command
!!
An even better trick is pressing:
Ctrl+R
Then start typing part of an old command to search your command history interactively.
Cron Jobs
View your cron jobs
crontab -l
Edit your cron jobs
crontab -e
Check the cron service
systemctl status cron
Rebooting and Shutting Down
Reboot the server
sudo reboot
Shut down immediately
sudo shutdown -h now
Schedule a reboot in 10 minutes
sudo shutdown -r +10
Cancel a scheduled shutdown or reboot
sudo shutdown -c
Useful Keyboard Shortcuts
| Shortcut | Action |
|---|---|
Tab |
Auto-complete commands and paths |
Ctrl+C |
Stop the current command |
Ctrl+L |
Clear the terminal |
Ctrl+R |
Search command history |
Ctrl+A |
Move to beginning of line |
Ctrl+E |
Move to end of line |
Ctrl+U |
Delete from cursor to beginning |
Ctrl+K |
Delete from cursor to end |
↑ / ↓ |
Browse previous commands |
A Simple Ubuntu Server Update Routine
For routine server maintenance, start by refreshing the package information:
sudo apt update
See exactly what can be upgraded:
apt list --upgradable
Install the available updates:
sudo apt upgrade
Check whether Ubuntu requires a reboot:
test -f /var/run/reboot-required && echo "Reboot required" || echo "No reboot required"
Check for failed services:
systemctl --failed
Check disk space:
df -h
Check memory:
free -h
If a reboot is required:
sudo reboot
The Commands Worth Memorizing
If you only memorize a handful of Ubuntu commands, make them these:
pwd
ls -lah
cd
cp
mv
rm
nano
cat
less
tail -f
grep
find
df -h
du -sh
free -h
top
htop
ps aux
systemctl status
systemctl restart
systemctl --failed
journalctl -xe
ip a
ss -tulpn
curl -I
dig
apt update
apt list --upgradable
apt upgrade
history
reboot
These commands cover a surprisingly large percentage of everyday Ubuntu server administration.