Chapter 08
SSH, servers and debugging
Reaching and securing a Linux server, keeping work alive, reading logs, and checking networks step by step.
SSH login
Also called ssh, known_hosts, authorized_keys, ssh-copy-id
- What
ssh user@hostopens a secure shell on another machine. The server's fingerprint is stored in~/.ssh/known_hosts; your public key goes in the server's~/.ssh/authorized_keys.- Why
- Key-based SSH is how you manage servers, deploy code and reach remote development machines.
ssh-copy-id deploy@203.0.113.10
ssh deploy@203.0.113.10
ssh -i ~/.ssh/work_key deploy@host
exitWatch out
Accepting a "host key has changed" warning without finding out why.
SSH config file
Also called ~/.ssh/config
- What
- A file of named Host blocks with the address, user, port and key for each machine.
- Why
- Short names work everywhere: ssh, scp, rsync, Git and VS Code Remote.
Host myvps
HostName 203.0.113.10
User deploy
IdentityFile ~/.ssh/id_ed25519
Host github-work
HostName github.com
IdentityFile ~/.ssh/work_keyscp and rsync
Also called scp, rsync
- What
scpcopies files over SSH;rsyncsynchronises folders, sending only changes. A trailing slash on the source means "the contents of".- Why
- Deploying a static site, pulling logs and simple backups all use them.
scp report.pdf myvps:~/
rsync -avz --dry-run ./site/ myvps:/var/www/site/
rsync -avz --delete ./site/ myvps:/var/www/site/
rsync -avz myvps:/var/log/nginx/ ./logs/Watch out
Running rsync --delete with source and destination swapped; always dry-run first.
First steps on a new server
Also called VPS, ufw, PermitRootLogin
- What
- Update packages, create a sudo user with key login, disable root and password login in
/etc/ssh/sshd_config, and enable a firewall allowing only what you need. - Why
- Public servers are scanned for weak SSH logins within minutes of going online.
sudo apt update && sudo apt upgrade -y
sudo adduser deploy && sudo usermod -aG sudo deploy
sudo ufw allow OpenSSH
sudo ufw enable
# sshd_config: PermitRootLogin no
# PasswordAuthentication no
sudo systemctl restart sshWatch out
Disabling password login before testing key login in a second session, and locking yourself out.
tmux
Also called terminal multiplexer, screen
- What
- Keeps terminal sessions running on a machine after you disconnect, with windows and split panes. The default prefix key is Ctrl+B.
- Why
- Long jobs over SSH survive dropped connections, and you can reattach from anywhere.
tmux new -s work
# Ctrl+B then D detach
tmux ls
tmux attach -t work
# Ctrl+B then % split side by side
# Ctrl+B then " split top and bottomsystemctl and journalctl
Also called systemd, services, /var/log
- What
- systemd starts and supervises services.
systemctlchecks, starts, stops and enables them;journalctlreads their logs. - Why
- The first place to look when a service will not start or keeps crashing.
systemctl status nginx
sudo systemctl restart nginx
sudo systemctl enable --now myapp
journalctl -u nginx --since "1 hour ago"
journalctl -u myapp -fWatch out
Restarting a failing service repeatedly without reading its log.
curl
Also called HTTP requests, curl -v
- What
- Sends HTTP and other requests from the terminal and prints the response.
- Why
- Tells you in seconds whether an API or site works, separate from any frontend.
curl -i https://example.com
curl -sL https://example.com -o page.html
curl -v https://api.example.com/health
curl -X POST -H "Content-Type: application/json" \
-d '{"name":"Ali"}' http://localhost:3000/usersWatch out
Leaving real tokens in commands that end up in shell history or shared screenshots.
DNS and reachability checks
Also called DNS, DNS lookup, dig, nslookup, ping, traceroute
- What
digornslookupshow what a name resolves to;pingchecks whether a host answers;tracerouteshows the path.- Why
- Separates "wrong address" from "host down" from "service broken".
dig +short example.com
nslookup example.com
ping -c 4 example.com
traceroute example.comWatch out
Concluding a server is down because ping fails, when many servers simply block ping.
Ports and listening processes
Also called ss, lsof, netstat, address already in use
- What
- A service listens on a port;
ss -tlnp(Linux) orlsof -i :PORTshows which process owns it. - Why
- Solves "address already in use" and checks whether your service is listening at all, and on which address.
sudo ss -tlnp
lsof -i :3000
sudo ufw statusWatch out
A service listening only on 127.0.0.1, which is unreachable from outside however the firewall is set.
Environment debugging checklist
- What
- A fixed order of checks: read the full error, ask what changed, then check which binary runs, PATH, permissions, environment variables, disk, processes, ports and logs.
- Why
- A method finds the cause faster than trying random fixes, and makes your help requests useful.
- Example
- A good help request includes the exact command, the full error, your OS and versions, and what you already checked.
Watch out
Changing several things at once, so you never learn which one fixed it.