Linux, terminal and Git: Windows

Sefism.

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@host opens 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.
Shell
ssh-copy-id deploy@203.0.113.10
ssh deploy@203.0.113.10
ssh -i ~/.ssh/work_key deploy@host
exit

Watch 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.
Code
Host myvps
  HostName 203.0.113.10
  User deploy
  IdentityFile ~/.ssh/id_ed25519

Host github-work
  HostName github.com
  IdentityFile ~/.ssh/work_key

scp and rsync

Also called scp, rsync

What
scp copies files over SSH; rsync synchronises 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.
Shell
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.
Shell
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 ssh

Watch 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.
Shell
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 bottom

systemctl and journalctl

Also called systemd, services, /var/log

What
systemd starts and supervises services. systemctl checks, starts, stops and enables them; journalctl reads their logs.
Why
The first place to look when a service will not start or keeps crashing.
Shell
systemctl status nginx
sudo systemctl restart nginx
sudo systemctl enable --now myapp
journalctl -u nginx --since "1 hour ago"
journalctl -u myapp -f

Watch 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.
Shell
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/users

Watch 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
dig or nslookup show what a name resolves to; ping checks whether a host answers; traceroute shows the path.
Why
Separates "wrong address" from "host down" from "service broken".
Shell
dig +short example.com
nslookup example.com
ping -c 4 example.com
traceroute example.com

Watch 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) or lsof -i :PORT shows which process owns it.
Why
Solves "address already in use" and checks whether your service is listening at all, and on which address.
Shell
sudo ss -tlnp
lsof -i :3000
sudo ufw status

Watch 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.

08 · SSH, servers and debugging8 / 9