mithlond-public/OKF/guides/03-ssh-hardening.md
Lukasz Boryn c12604ebb4 feat(template): publish the Mithlond self-hosted Gitea template
A spare-machine Gitea forge, hardened, backed up and with CI, as a template.
Personal values are placeholders listed in the README.
2026-10-16 15:04:18 +02:00

7.2 KiB

type, purpose, answers
type purpose answers
Guide Lock admin SSH to keys on port 2234, LAN-only, with WireGuard for remote access.
How is admin SSH to the host hardened?

03 — SSH Hardening

Key-only SSH auth on non-standard port. Password auth disabled. Applies to admin SSH only (host management). Git SSH on port 2222 is handled by the Gitea container.

Prerequisites

  • Arch Linux installed and accessible on LAN
  • SSH key pair generated on your local machine
  • Know the server's local IP: run ip a on server or check router DHCP table

1. Copy your public key to the server

Run this from your local machine, not the server:

ssh-copy-id -p 22 admin@192.168.1.50

If ssh-copy-id is not available:

cat ~/.ssh/id_ed25519.pub | ssh admin@192.168.1.50 \
  "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

Test key login works before disabling password auth:

ssh -p 22 admin@192.168.1.50
# should log in without asking for password

2. Confirm nothing will block the new port

Before moving SSH off port 22, make sure no firewall is rejecting 2234. A loaded ruleset that permits only port 22 means the restart in step 3 locks you out.

systemctl is-active nftables   # expect: inactive
sudo nft list ruleset          # expect: no output

If a ruleset is loaded, flush it — systemctl stop nftables does not remove already-loaded rules from the kernel:

sudo nft flush ruleset
sudo systemctl disable nftables

The host firewall gets its real config and is re-enabled in 08-firewall.md. Until then this box must stay LAN-only — no router port-forwards.

3. Harden sshd config

Use a drop-in rather than editing /etc/ssh/sshd_config. The stock file stays untouched, so package upgrades never conflict and diff stays readable.

sudo tee /etc/ssh/sshd_config.d/99-hardening.conf > /dev/null <<'EOF'
Port 2234
PermitRootLogin no
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
AuthenticationMethods publickey
X11Forwarding no
AllowUsers admin
EOF

KbdInteractiveAuthentication no matters: without it OpenSSH can still offer keyboard-interactive as a password path, which PasswordAuthentication no alone does not close.

Validate before applying — this parses the config without touching the running daemon:

sudo sshd -t && echo "CONFIG OK"

Do not continue unless it prints CONFIG OK.

Requires Include /etc/ssh/sshd_config.d/*.conf in /etc/ssh/sshd_config (present by default on Arch) and no active sshd.socket (socket activation overrides the Port directive):

grep -n "^Include" /etc/ssh/sshd_config
systemctl is-active sshd.socket   # expect: inactive or not-found

4. Restart sshd

sudo systemctl restart sshd

Established sessions survive an sshd restart — your current session stays alive.

Warning: Do NOT close your current SSH session yet. Open a second terminal and test:

ssh -p 2234 admin@192.168.1.50

If it connects — the old session is safe to close. If it fails — fix the config in the still-open session.

5. Verify the hardening from your local machine

Run all four. The first must succeed; the last three must fail exactly as shown.

# key auth on the new port — must print OK
ssh -p 2234 -o BatchMode=yes -o PasswordAuthentication=no admin@192.168.1.50 'echo OK'

# password auth — must return: Permission denied (publickey)
ssh -p 2234 -o PubkeyAuthentication=no -o PreferredAuthentications=password \
    -o BatchMode=yes admin@192.168.1.50

# root login — must return: Permission denied (publickey)
ssh -p 2234 -o BatchMode=yes root@192.168.1.50

# old port — must return: Connection refused
ssh -p 22 -o BatchMode=yes admin@192.168.1.50

Read the failure messages carefully. Connection refused means the host is reachable and nothing is listening — expected on port 22. No route to host on a host that answers ping means a firewall is rejecting that port, not that sshd is misconfigured.

6. Configure fail2ban for SSH

Create /etc/fail2ban/jail.d/sshd.conf:

[sshd]
enabled  = true
port     = 2234
backend  = systemd
maxretry = 5
bantime  = 1h
findtime = 10m

No logpath — with backend = systemd fail2ban reads the journal, and Arch does not always define the %(sshd_log)s macro, which aborts jail startup.

Type port carefully. Nothing cross-checks it against sshd's own config, and once 09 points bans at nftables it becomes the port the ban rule blocks. Wrong here means bans that register and block nothing — see 09 §4.

sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd

Expect Journal matches: _SYSTEMD_UNIT=sshd.service. A non-zero Total failed is normal — the rejected logins from step 5 count.

7. Remote admin: WireGuard, never a port-forward

Admin SSH (2234) is never port-forwarded — decided when the box went public; an external probe confirms 2234 filtered. Only 80, 443 and 2222 are forwarded (06 §1, once 09 is done). From outside the LAN, ssh -p 2234 …@git.example.com therefore times out, by design.

To administer from anywhere, tunnel onto 192.168.1.0/24 through the router's built-in WireGuard server (many consumer routers have one; adapt if yours differs): the router listens on one UDP port but answers nothing without a valid key, and nothing on the server itself is exposed.

On the router: VPN Server → WireGuard → add a peer (client) → download its config. Before installing it, check for a key-export bug seen on some routers: after a VPN server reset, a regenerated profile has come back with the client's PrivateKey set to the server's own key (self-pairing — no handshake ever completes, wg show reports 0 B received):

sed -n 's/^PrivateKey = //p' <downloaded-client.conf> | wg pubkey
# must NOT print the router's own public key (shown on its WireGuard page)
getent hosts example.com   # must match the config's Endpoint IP — ISPs rotate it

If the pubkey matches, regenerate the peer on the router (firmware bug, no fix on this end). If the Endpoint IP is stale, edit it to the current one.

On the client (laptop), wireguard-tools installed:

sudo systemctl stop wg-quick@wg0
sudo systemctl reset-failed wg-quick@wg0 2>/dev/null
sudo ip link delete wg0 2>/dev/null
sudo cp <downloaded-client.conf> /etc/wireguard/wg0.conf
sudo systemctl start wg-quick@wg0
sudo wg show wg0                      # want a recent "latest handshake"
ping -c1 192.168.1.50 && ssh -p 2234 admin@192.168.1.50

The stop/reset-failed/delete dance is not optional: systemctl restart fails with wg0' already exists if the interface is up outside systemd's bookkeeping (common after sleep/resume), and a bare start silently no-ops if the unit still thinks it is active.

Checklist

  • Key login works on port 2234
  • Password login rejected on port 2234
  • Root login rejected
  • Port 22 refuses connections
  • fail2ban watching port 2234
  • 2234 not forwarded (LAN-only); remote admin via router WireGuard (§7)

Next step

Continue with 04-dns-records.md.