Linux command line survival guide for hosting customers
The two dozen commands you actually need to run a server, explained plainly.
You do not need to be a sysadmin to run a VPS. You need about twenty commands and the confidence to look things up. Here they are.
Getting around
pwd # print working directory — where am I?
ls -lah # list files, human sizes, hidden files
cd /var/www # change directory
cd .. # up one level
cd ~ # home directoryLooking at files
less /etc/nginx/nginx.conf # page through a file (q to quit, / to search)
head -50 file.log # first 50 lines
tail -50 file.log # last 50 lines
tail -f file.log # follow a log live (Ctrl-C to stop)
grep "error" file.log # find lines containing "error"Finding things
find /var/www -name "*.php" # by name
grep -r "password" /etc/nginx # search inside files, recursively
which nginx # where is this command?Files and permissions
cp file1 file2 # copy
mv file1 file2 # move / rename
rm file # delete (no undo — be careful)
mkdir -p a/b/c # make directories, including parents
chmod 644 file # permissions: owner rw, group r, others r
chown www-data:www-data file # change owner and groupPermission shorthand: read=4, write=2, execute=1, summed per owner/group/others. 755 = full for owner, read+execute for everyone else. 644 = read/write for owner, read for others. Directories need execute to be entered.
Processes and resources
top # live process list (q to quit); htop is nicer if installed
ps aux | grep nginx # is nginx running?
kill <PID> # politely ask a process to stop
kill -9 <PID> # force it (last resort)
free -h # memory
df -h # disk space
du -sh /var/log # size of a directoryServices (systemd)
Most long-running software is managed by systemd:
systemctl status nginx # is it running? recent logs?
systemctl restart nginx # restart it
systemctl reload nginx # reload config without dropping connections
systemctl enable nginx # start automatically at boot
journalctl -u nginx -n 100 # last 100 log lines for a service
journalctl -u nginx -f # follow the service log liveReload is almost always preferable to restart for web servers and databases — it re-reads configuration without dropping live connections.
Packages
apt update # refresh the package index (Debian/Ubuntu)
apt upgrade # install available updates
apt install nginx # install
apt remove nginx # uninstall, keep config
apt purge nginx # uninstall and delete config
apt search "web server" # search
# RHEL-family equivalents
dnf check-update && dnf upgrade
dnf install nginxNetworking
ip -br addr # my IP addresses
ip route # routing table
ss -tulpn # what is listening on which port
curl -I https://example.com # fetch headers only
ping -c 4 1.1.1.1 # is a host reachable?
dig example.com # DNS lookupThe one habit that matters
Before running a destructive command (rm, mv over something, a config change), ask: can I undo this? If not, take a snapshot or copy the file to .bak first. Almost every hosting disaster story is a five-second command with no undo.
Related reading