NewCode CLOUD — 20% off your first month on all cloud VPSClaim

50 Essential Linux Commands for Server Administrators

You do not need to memorise hundreds of commands to manage a Linux server. A few dozen cover almost everything: finding files, checking what is using CPU or disk, reading logs, testing the network and managing services. This reference groups the essentials by task, with examples you can paste straight into a terminal on Ubuntu, Debian, AlmaLinux or Rocky Linux.

Commands that need administrator rights are shown with sudo. If you are logged in as root you can leave it out — but read why you should use a sudo user.

Getting help

Command What it does
man ls Full manual for a command (q to quit, /word to search)
ls --help Short summary of options
type cmd / which cmd Show whether a command exists and where
history Previous commands; !123 re-runs number 123

Press Tab to auto-complete file and command names, and Ctrl+R to search your command history. These two keys save more time than any other trick.

pwd                         # where am I?
ls -lah                     # list everything with sizes and permissions
cd /var/www                 # change directory; "cd -" goes back
tree -L 2                   # directory tree, 2 levels (apt/dnf install tree)
stat file.txt               # size, owner, timestamps
file backup.tar.gz          # what kind of file is this?

Reading files:

cat config.ini              # print a small file
less /var/log/syslog        # page through a large file (q to quit)
head -n 20 file.log         # first 20 lines
tail -n 50 file.log         # last 50 lines
tail -f /var/log/nginx/access.log   # follow a log live (Ctrl+C to stop)

Creating, copying, moving and deleting

mkdir -p /srv/app/releases  # create nested directories
cp -a src/ dest/            # copy, preserving permissions and timestamps
mv old.conf new.conf        # move or rename
rm file.txt                 # delete a file
rm -r directory/            # delete a directory and its contents
ln -s /srv/app/current /var/www/app   # symbolic link

Safe habits with rm: there is no recycle bin. Run ls with the same path first, avoid wildcards next to /, and never paste rm -rf commands you do not fully understand.

Finding things

find /var/www -name "*.php" -mtime -1     # PHP files changed in the last day
find / -xdev -size +500M 2>/dev/null      # files larger than 500 MB
grep -rn "DB_PASSWORD" /var/www/site/     # search inside files
grep -i error /var/log/nginx/error.log | tail
locate nginx.conf                         # fast filename search (needs updatedb)

grep and find together answer most “where is it?” questions. Add -l to grep to print only file names.

Editing files

nano is the easiest editor for beginners: arrows to move, Ctrl+O to save, Ctrl+X to exit. vim is installed almost everywhere and worth learning the basics of (i to insert, Esc then :wq to save and quit, :q! to quit without saving).

Before editing any configuration file, make a copy:

sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak.$(date +%F)

Permissions and ownership

chmod 640 file              # rw-r-----
chmod +x deploy.sh          # make a script executable
sudo chown deploy:www-data file
sudo chown -R deploy:deploy /home/deploy/app

The permissions model is explained in creating a sudo user.

Processes and resource usage

top                         # live CPU / memory view (q to quit)
htop                        # friendlier version (install it)
ps aux --sort=-%cpu | head  # top CPU consumers
ps aux --sort=-%mem | head  # top memory consumers
pgrep -a php                # find processes by name
kill 12345                  # ask a process to stop
kill -9 12345               # force stop (last resort)
uptime                      # load averages for 1, 5, 15 minutes
free -h                     # memory and swap usage

The load average is roughly the number of processes waiting for CPU or disk. Compare it with the number of cores (nproc): on a 4-core server, a load of 4 is fully busy. For a deeper investigation see troubleshooting high CPU load.

Disks and storage

df -h                       # free space per filesystem
df -i                       # free inodes (can run out before space does)
du -sh /var/*               # size of each folder in /var
du -h --max-depth=1 / 2>/dev/null | sort -h
lsblk                       # disks and partitions
sudo ncdu /                 # interactive disk usage browser (install it)

When a disk fills up, websites and databases fail in confusing ways. Fixing a full disk walks through the usual culprits.

Networking

ip a                        # IP addresses on each interface
ip r                        # routing table / default gateway
ss -tulpn                   # listening ports and which program owns them
ping -c 4 1.1.1.1           # basic reachability
mtr -rwc 100 example.com    # route and packet loss report
curl -I https://example.com # HTTP response headers only
dig example.com A +short    # DNS lookup (package dnsutils / bind-utils)

ss -tulpn answers “what is listening on port 80?” — invaluable when a service will not start because the port is already taken. To read mtr output correctly, see reading MTR and traceroute reports.

Packages

Task Ubuntu / Debian AlmaLinux / Rocky
Refresh package lists sudo apt update (automatic)
Upgrade everything sudo apt upgrade sudo dnf upgrade
Install sudo apt install nginx sudo dnf install nginx
Remove sudo apt remove nginx sudo dnf remove nginx
Search apt search redis dnf search redis
Which package owns a file dpkg -S /usr/bin/curl rpm -qf /usr/bin/curl

Services and logs

sudo systemctl status nginx          # is it running? recent log lines
sudo systemctl restart nginx         # restart
sudo systemctl reload nginx          # reload config without dropping connections
sudo systemctl enable --now redis    # start now and at every boot
systemctl list-units --failed        # anything broken?
sudo journalctl -u nginx -n 100      # last 100 log lines of a service
sudo journalctl -f                   # follow all system logs
sudo journalctl --since "1 hour ago" -p err

More on services: managing services with systemd.

Archives and transfers

tar -czf site-$(date +%F).tar.gz /var/www/site   # create a compressed archive
tar -xzf site.tar.gz -C /tmp/restore             # extract into a folder
scp file.tar.gz deploy@203.0.113.10:/tmp/        # copy a file to another server
rsync -avz --progress /var/www/ deploy@203.0.113.10:/var/www/
wget https://example.com/file.iso                # download

For moving large amounts of data between servers, rsync is faster and safer than scp because it can resume and only sends changes.

System information

hostnamectl                 # hostname, OS, kernel
cat /etc/os-release         # distribution and version
uname -r                    # kernel version
lscpu                       # CPU model and cores
timedatectl                 # time, time zone, NTP status
last -n 10                  # recent logins
who                         # who is logged in now

Keep long jobs running: tmux

If your SSH connection drops during a long upgrade or file copy, the command dies with it. Run it inside tmux:

tmux new -s work            # start a session
# ...run your command...
# Ctrl+B then D to detach; the job keeps running
tmux attach -t work         # reconnect later

This matters on long-distance connections, where a brief network glitch can drop an SSH session.

Pipes and redirection

Linux commands become powerful when combined:

cat access.log | awk '{print $1}' | sort | uniq -c | sort -rn | head
# top 10 IP addresses hitting your website

command > out.txt           # write output to a file (overwrite)
command >> out.txt          # append
command 2>&1 | tee run.log  # see output and save it, including errors

A safe-habits checklist

  • Read a command before you paste it, especially anything with rm, dd, mkfs or chmod -R.
  • Back up configuration files before editing them.
  • Use tmux for anything that takes more than a few minutes.
  • Test configuration before reloading: nginx -t, sshd -t, apachectl configtest.
  • Keep a text file of what you changed and when — future you will thank you.