Commands in Linux

This cheat sheet collects the Linux commands you use most often on a server: system information, files, search, disks, users, permissions, archives, packages, networking, the firewall, processes and logs. The examples work on current Ubuntu, Debian, AlmaLinux and Rocky Linux.

Commands that change the system need root rights. Run them as root or add sudo in front.

System Information

CommandWhat it does
uname -aShows the kernel version, hostname and architecture
uname -rShows only the kernel version
cat /etc/os-releaseShows the distribution name and version
hostnamectlShows the hostname, OS and kernel
lscpuShows CPU details: model, cores, threads
free -hShows RAM and swap usage in human-readable units
lsblkLists disks and partitions
lspciLists PCI devices
uptimeShows how long the system has been running and the load average
dateShows the current date and time
timedatectl set-timezone Europe/LondonSets the system time zone
cal 2026Shows the calendar for 2026

Shutdown and Reboot

CommandWhat it does
shutdown -h nowShuts the system down immediately
shutdown -h 22:30Schedules a shutdown at 22:30
shutdown -cCancels a scheduled shutdown
rebootRestarts the system
exitLogs out of the current session

Files and Directories

CommandWhat it does
pwdShows the current directory
ls -laLists all files, including hidden ones, with permissions and sizes
ls -lhLists files with sizes in KB, MB and GB
cd /var/wwwGoes to the /var/www directory
cd ..Goes one level up
cd ../..Goes two levels up
cd ~Goes to your home directory
cd -Returns to the previous directory
mkdir dir1Creates a directory
mkdir -p /tmp/dir1/dir2Creates a directory together with any missing parent directories
cp file1 file2Copies file1 to file2
cp -a dir1 dir2Copies a directory with its contents, permissions and timestamps
mv old_name new_nameMoves or renames a file or directory
rm file1Deletes a file
rm -r dir1Deletes a directory and everything in it (check the path twice)
rmdir dir1Deletes an empty directory
ln -s /path/to/target link_nameCreates a symbolic link
touch file1Creates an empty file or updates its modification time
treeShows the directory tree (install the tree package first)

Viewing File Contents

For more examples, see our guide on how to display the contents of a file in Linux.

CommandWhat it does
cat file1Prints the whole file
less file1Opens the file page by page with search (press q to exit)
head -n 20 file1Shows the first 20 lines
tail -n 50 file1Shows the last 50 lines
tail -f /var/log/syslogFollows a log file in real time
tac file1Prints the file in reverse order, from the last line

Searching for Files and Text

CommandWhat it does
find / -name file1Finds files and directories named file1, starting from the root
find /home -user user1Finds files owned by user1
find /var/log -name "*.log" -mtime -7Finds .log files modified in the last 7 days
find /usr/bin -type f -atime +100Finds files that have not been accessed for more than 100 days
grep -R "error" /var/logSearches for the word error in all files under /var/log
locate file1Finds files by name using a database (run updatedb first)
which nginxShows the full path of the nginx command
whereis nginxShows the binary, source and manual page locations

Disks and Mounting

CommandWhat it does
df -hShows free and used space on mounted file systems
du -sh /var/wwwShows the total size of a directory
du -sh * | sort -rh | headShows the 10 largest items in the current directory
lsblk -fLists partitions with file system types and UUIDs
mount /dev/sdb1 /mnt/dataMounts a partition to /mnt/data (the directory must exist)
umount /mnt/dataUnmounts the partition
mount -o loop image.iso /mnt/isoMounts an ISO image
mount -t cifs //server/share /mnt/share -o username=userMounts a Windows (SMB/CIFS) network share
mkfs.ext4 /dev/sdb1Creates an ext4 file system (erases all data on the partition)
fsck /dev/sdb1Checks and repairs a file system (run it on an unmounted partition)

Users and Groups

CommandWhat it does
whoamiShows the current user
id user1Shows the user ID and groups of user1
useradd -m -s /bin/bash user1Creates user1 with a home directory and the bash shell
passwd user1Sets or changes the password of user1
usermod -aG sudo user1Adds user1 to the sudo group (use wheel on AlmaLinux and Rocky Linux)
userdel -r user1Deletes user1 together with the home directory
groupadd developersCreates a group
groupdel developersDeletes a group
chage -E 2026-12-31 user1Sets the account expiration date
whoShows who is logged in
lastShows the login history

File Permissions

For a detailed explanation, see how to change file and folder permissions.

CommandWhat it does
ls -lShows permissions, owner and group of each file
chmod 644 file1Owner can read and write, everyone else can read
chmod 755 dir1Owner has full access, everyone else can read and enter
chmod -R 755 dir1Applies permissions recursively
chmod u+x script.shMakes a script executable for its owner
chown user1:group1 file1Changes the owner and group of a file
chown -R www-data:www-data /var/www/siteChanges the owner of a directory recursively
find / -perm -u=s -type fFinds files with the SUID bit set
chmod +t /home/publicSets the sticky bit: only owners can delete their files
chattr +i file1Makes a file immutable, even for root
lsattr file1Shows file attributes

Archives and Compression

CommandWhat it does
tar -czvf archive.tar.gz dir1Creates a gzip-compressed archive of dir1
tar -xzvf archive.tar.gzExtracts a .tar.gz archive
tar -xzvf archive.tar.gz -C /tmpExtracts the archive to /tmp
tar -cjvf archive.tar.bz2 dir1Creates a bzip2-compressed archive
tar -tf archive.tar.gzLists the contents of an archive
zip -r archive.zip dir1Creates a zip archive
unzip archive.zipExtracts a zip archive
gzip file1Compresses a file to file1.gz
gunzip file1.gzDecompresses a .gz file

Packages on Ubuntu and Debian

CommandWhat it does
apt updateRefreshes the package lists
apt upgradeInstalls available updates
apt install nginxInstalls a package
apt remove nginxRemoves a package and keeps its configuration files
apt purge nginxRemoves a package together with its configuration files
apt autoremoveRemoves dependencies that are no longer needed
apt search nginxSearches for packages
apt show nginxShows package details
dpkg -i package.debInstalls a local .deb file
dpkg -l | grep nginxChecks whether a package is installed
dpkg -L nginxLists the files installed by a package
dpkg -S /bin/pingFinds the package that owns a file

Packages on AlmaLinux and Rocky Linux

CommandWhat it does
dnf check-updateChecks for available updates
dnf upgradeInstalls updates
dnf install nginxInstalls a package
dnf remove nginxRemoves a package
dnf search nginxSearches for packages
dnf info nginxShows package details
rpm -qaLists all installed packages
rpm -qi nginxShows information about an installed package
rpm -ql nginxLists the files of a package
rpm -qf /etc/nginx/nginx.confFinds the package that owns a file
rpm -ivh package.rpmInstalls a local .rpm file

Networking

CommandWhat it does
ip aShows network interfaces and IP addresses
ip routeShows the routing table
ip addr add 192.168.1.10/24 dev eth0Adds an IP address to an interface
ip link set eth0 downDisables an interface (use up to enable it)
ss -tulpnShows listening TCP and UDP ports and the processes behind them
ping -c 4 example.comChecks whether a host is reachable
traceroute example.comShows the route packets take to a host
dig example.comQueries DNS records
host example.comResolves a hostname to an IP address and back
curl -I https://example.comShows the HTTP response headers
wget -c https://example.com/file.isoDownloads a file and resumes it if the download is interrupted
hostnamectl set-hostname server1Changes the hostname

Firewall

For more rules and examples, see our iptables guide.

CommandWhat it does
ufw statusShows UFW status and rules (Ubuntu)
ufw allow 22/tcpOpens port 22 in UFW
ufw enableTurns on UFW
firewall-cmd --list-allShows firewalld rules (AlmaLinux, Rocky Linux)
firewall-cmd --permanent --add-service=httpOpens HTTP in firewalld
firewall-cmd --reloadApplies firewalld changes
iptables -L -n -vShows iptables rules with packet counters

Processes and Services

CommandWhat it does
topShows running processes and resource usage in real time
htopA more convenient version of top (install the htop package)
ps auxLists all processes
ps -ef --forestShows processes as a tree
kill 1234Asks the process with PID 1234 to stop
kill -9 1234Stops the process immediately, without cleanup
pkill nginxStops processes by name
systemctl status nginxShows the status of a service
systemctl restart nginxRestarts a service
systemctl enable --now nginxStarts a service and enables it at boot
lsof -i :80Shows which process uses port 80

Logs and Monitoring

CommandWhat it does
journalctl -u nginxShows the log of a service
journalctl -fFollows the system log in real time
journalctl -bShows the log since the last boot
dmesg -T | tailShows the latest kernel messages with timestamps
tail -f /var/log/syslogFollows the system log on Ubuntu and Debian (/var/log/messages on AlmaLinux and Rocky Linux)
last rebootShows the reboot history
watch -n 2 free -hRepeats a command every 2 seconds
smartctl -a /dev/sdaShows the SMART status of a disk (smartmontools package)

Text Processing

CommandWhat it does
grep -i "error" file1Finds lines with error, ignoring case
grep -v "^#" file1Shows lines that do not start with #
sed 's/old/new/g' file1Replaces old with new and prints the result
sed -i 's/old/new/g' file1Replaces text directly in the file
sed '/^$/d' file1Removes empty lines
awk '{print $1}' file1Prints the first column
sort file1 | uniqSorts lines and removes duplicates
sort file1 | uniq -c | sort -rnCounts repeated lines, most frequent first
wc -l file1Counts lines
cut -d: -f1 /etc/passwdPrints the first field of each line (here, all usernames)
diff file1 file2Shows the differences between two files

Backup and Sync

CommandWhat it does
rsync -av /var/www/ /backup/www/Copies a directory and keeps permissions and timestamps
rsync -avz --delete /var/www/ [email protected]:/backup/www/Syncs a directory to another server over SSH
scp file1 [email protected]:/tmp/Copies a file to another server
tar -czf - /var/www | ssh [email protected] "cat > /backup/www.tar.gz"Archives a directory straight to another server
crontab -eOpens the cron schedule for regular backups

Getting Help

CommandWhat it does
man pingOpens the manual for a command
ping --helpShows a short help for a command
apropos networkFinds commands related to a keyword
whatis pingShows a one-line description of a command
historyShows the command history
alias ll='ls -la'Creates a shortcut for a command

Conclusion

These commands cover most everyday tasks on a Linux server. To practice them on a real system, you can use a BlueVPS Linux VPS with full root access over SSH.

Blog
Subscribe to our Newsletter