Online admissions open for the SEO, PHP web development and networking & cybersecurity courses. Enquire for the next batch.

Linux Commands Cheat Sheet

Cheat sheet · 111 entries Updated By the SkillNewton team

This cheat sheet covers the Linux commands that network and server administrators use daily, from moving around the file system to managing services, packages, users and remote connections. Commands work on Ubuntu and Debian as well as Fedora, RHEL, Rocky and AlmaLinux, with differences noted. Use it alongside a terminal on a lab machine or virtual machine, and test anything that deletes or overwrites files on a copy first.

How to use this cheat sheet

  1. Open a practice machine. Use a virtual machine, WSL on Windows or a cloud lab rather than a production server while you learn.
  2. Find the section for your task. Sections follow everyday jobs: files, permissions, processes, services, packages, networking and disk space.
  3. Read the manual for options. Run man command or command --help to see every option for a command you use often.
  4. Check before deleting. Run ls, find without -delete, or rsync -n first to see exactly what a destructive command will touch.
pwdPrint the current folder (working directory).
ls -lahList all files, including hidden ones, with permissions and human-readable sizes.
cd /var/wwwChange to an absolute path.
cd ..Go up one level. cd ~ goes to your home folder, cd - to the previous folder.
mkdir -p projects/site/cssCreate a folder, including any missing parent folders.
touch notes.txtCreate an empty file, or update the timestamp of an existing one.
cp config.php config.php.bakCopy a file.
cp -r site/ site-backup/Copy a folder and everything in it.
mv old.txt new.txtRename or move a file or folder.
rm file.txtDelete a file. There is no recycle bin.
rm -r old-folder/Delete a folder and its contents. Double-check the path; never run it as root on a path you have not checked.
ln -s /var/www/site/current /var/www/htmlCreate a symbolic link (shortcut) at the second path pointing to the first.

Viewing files

cat file.txtPrint a whole file. Best for short files.
less /var/log/syslogPage through a file. Space for next page, / to search, q to quit.
head -n 20 access.logShow the first 20 lines.
tail -n 50 error.logShow the last 50 lines.
tail -f /var/log/nginx/error.logFollow a file as new lines are written. Ctrl+C to stop.
wc -l access.logCount lines in a file.
diff -u old.conf new.confShow the differences between two files.
file backup.tar.gzIdentify a file type from its contents.
stat index.phpShow size, permissions, owner and timestamps.

Permissions and ownership

chmod 644 index.phpOwner read/write, group and others read only. Typical for web files.
chmod 755 deploy.shOwner full access, group and others read and execute. Typical for scripts and folders.
chmod 600 ~/.ssh/id_ed25519Owner read/write only. SSH refuses private keys with looser permissions.
chmod u+x deploy.shSymbolic mode: add execute for the owner (u).
chmod g+w shared.txtAdd write for the group (g).
chmod o-rwx secrets.txtRemove all permissions for others (o).
chmod -R u=rwX,go=rX site/Recursive: capital X adds execute only to folders and files that are already executable.
chown priya file.txtChange the owner of a file (needs sudo for files you do not own).
sudo chown -R www-data:www-data /var/www/siteChange owner and group recursively, here to the web server user on Debian and Ubuntu.
chgrp developers report.txtChange only the group.
umaskShow the default permission mask for new files (often 0022, giving 644 files and 755 folders).
r = 4   w = 2   x = 1      (add them for each of: owner, group, others)
7 = rwx   6 = rw-   5 = r-x   4 = r--   0 = ---

-rw-r--r--  644  owner read/write; group and others read
-rwxr-xr-x  755  owner full; group and others read and execute
-rw-------  600  owner only (SSH private keys, .env files)
d at the start of the line means a directory, l a symbolic link

Users and sudo

whoamiShow the user you are logged in as.
idShow your user ID, group ID and every group you belong to.
sudo apt updateRun one command as root. You enter your own password.
sudo -iOpen a root shell. Type exit as soon as you are done.
sudo adduser priyaCreate a user interactively with a home folder (Debian and Ubuntu).
sudo useradd -m -s /bin/bash priyaCreate a user with a home folder and Bash shell (works on all distributions).
sudo passwd priyaSet or reset a user's password.
sudo usermod -aG sudo priyaAdd a user to the sudo group on Debian and Ubuntu. On Fedora and RHEL the group is wheel. Keep -a or other groups are removed.
sudo userdel -r priyaDelete a user and their home folder.
sudo visudoEdit sudo rules safely; it checks the syntax before saving.
wShow who is logged in and what they are running.

Processes

ps auxList every running process with its user, PID, CPU and memory use.
ps aux | grep nginxFind processes whose command line contains a word.
pgrep -a php-fpmList matching process IDs with their command lines.
topLive view of processes, sorted by CPU. Press q to quit, M to sort by memory.
htopFriendlier, colourful version of top. Install it with apt or dnf if missing.
kill 1234Ask process 1234 to stop (SIGTERM). It can save work and exit cleanly.
kill -9 1234Force-stop a process (SIGKILL). Last resort: no clean-up happens.
pkill -f "php artisan queue"Stop processes whose full command line matches the text.
nohup ./backup.sh &Run a command in the background and keep it running after you log out.
jobsList background jobs in this shell; fg brings one back to the foreground.

Services and logs (systemd)

systemctl status nginxShow whether a service is running, its PID and its latest log lines.
sudo systemctl start nginxStart a service.
sudo systemctl stop nginxStop a service.
sudo systemctl restart nginxStop and start a service.
sudo systemctl reload nginxReload configuration without dropping connections (if the service supports it).
sudo systemctl enable --now nginxStart a service now and at every boot.
sudo systemctl disable nginxStop a service from starting at boot.
systemctl --failedList units that failed to start.
journalctl -u nginxShow the logs for one service.
journalctl -u nginx -fFollow a service's logs live.
journalctl --since "1 hour ago"Show all logs from the last hour.
journalctl -p err -bShow errors and worse since the last boot.

Packages: apt and dnf

sudo apt updateDebian/Ubuntu: refresh the list of available packages. Run before installing or upgrading.
sudo apt upgradeDebian/Ubuntu: install available updates.
sudo apt install nginxDebian/Ubuntu: install a package and its dependencies.
sudo apt remove nginxDebian/Ubuntu: remove a package but keep its configuration files.
sudo apt purge nginxDebian/Ubuntu: remove a package and its configuration files.
sudo apt autoremoveDebian/Ubuntu: remove dependencies that are no longer needed.
apt search nginxDebian/Ubuntu: search package names and descriptions.
apt show nginxDebian/Ubuntu: show details of a package.
sudo dnf check-updateFedora/RHEL: list available updates.
sudo dnf upgradeFedora/RHEL: install available updates.
sudo dnf install nginxFedora/RHEL: install a package. dnf search and dnf info find and describe packages.
sudo dnf remove nginxFedora/RHEL: remove a package.

Networking and remote access

ip aShow interfaces with their IPv4 and IPv6 addresses (short for ip addr).
ip routeShow the routing table; the default gateway is on the line starting default via.
ss -tulpnList listening TCP and UDP ports with the owning process. Use sudo to see every process name.
ping -c 4 example.comSend four echo requests to test reachability and latency.
curl -I https://example.comFetch only the HTTP response headers (status code, redirects, caching).
curl -L -o file.zip https://example.com/file.zipDownload a file, following redirects, and save it under a chosen name.
dig example.com A +shortLook up DNS records. Try MX, TXT or @1.1.1.1 to query a specific server.
ssh [email protected]Log in to a remote server over SSH. Add -p 2222 for a non-standard port.
ssh-keygen -t ed25519Create an SSH key pair; ssh-copy-id user@host installs the public key on a server.
scp report.pdf [email protected]:/home/priya/Copy a file to a server over SSH. Use -r for folders.
rsync -avz --delete site/ [email protected]:/var/www/site/Sync a folder over SSH, sending only changes. --delete removes extra files at the destination; run with -n first for a dry run.
# rsync: the trailing slash on the source matters
rsync -av site/  backup/    # copies the CONTENTS of site into backup/
rsync -av site   backup/    # copies the folder itself, giving backup/site/
rsync -avn --delete site/ backup/   # -n = dry run: list changes, touch nothing

Searching

find /var/www -name "*.php"Find files by name pattern under a folder.
find . -type f -mtime -7Files changed in the last seven days.
find / -type f -size +100M 2>/dev/nullFiles larger than 100 MB, hiding permission errors.
find /tmp -name "*.tmp" -deleteDelete matching files. Run without -delete first to check the list.
find site/ -type d -exec chmod 755 {} \;Run a command on every match, here setting folder permissions.
grep -rn "DB_PASSWORD" /var/www/siteSearch file contents recursively and show line numbers.
grep -i "error" app.logCase-insensitive search.
grep -v "^#" nginx.confShow lines that do not match, here hiding comment lines.
grep -rl "old-domain.com" .List only the names of files that contain the text.
grep -E "404|500" access.logExtended regular expression: match either pattern.
which phpShow the full path of a command that would run.

Archives and disk space

tar -czvf site-backup.tar.gz site/Create a gzip-compressed archive of a folder (c = create, z = gzip, f = file name).
tar -xzvf site-backup.tar.gzExtract a .tar.gz archive into the current folder.
tar -xzvf site-backup.tar.gz -C /tmp/restoreExtract into another folder (it must already exist).
tar -tzvf site-backup.tar.gzList the contents without extracting.
zip -r site.zip site/Create a .zip archive of a folder.
unzip site.zipExtract a .zip file. unzip -l lists contents first.
df -hFree and used space on each mounted file system.
df -iInode use. A disk can be "full" with free space if it runs out of inodes.
du -sh /var/logTotal size of one folder.
du -h --max-depth=1 /var | sort -hSize of each sub-folder, smallest to largest, to find what is filling a disk.
lsblkList disks and partitions with sizes and mount points. lsblk -f adds file system types.
free -hShow memory and swap use.

Frequently asked questions

What is the difference between apt and dnf?

Both are package managers. apt is used on Debian and Ubuntu and installs .deb packages; dnf is used on Fedora, RHEL, Rocky Linux and AlmaLinux and installs .rpm packages. The everyday commands (install, remove, upgrade, search) are very similar.

What does chmod 755 mean?

Each digit is the sum of read (4), write (2) and execute (1) for the owner, the group and everyone else. 755 means the owner can read, write and execute, while the group and others can read and execute.

Why should I avoid kill -9?

kill -9 sends SIGKILL, which stops a process instantly without letting it save data or clean up. Try a normal kill (SIGTERM) or systemctl stop first and use -9 only when a process will not respond.

Is ifconfig still used?

ifconfig and netstat come from the older net-tools package, which many current distributions no longer install by default. Use ip and ss instead.

Want to Learn This Properly?

The Networking & Cybersecurity: Foundation & Practical Implementation (Online) turns resources like this into practised skills, with projects on real websites.

WhatsApp Call Us Now

Free counselling

Send an Enquiry

Tell us which course you are interested in. We reply by email or phone with the next batch and how to enrol.

We reply by email or phone within working hours. No spam.