0226e33ac7
- port of the wiki-local linux-server-hardening checklist (v1.2.0) into the bundle: audit listeners and accounts, SSH key-only, close unused accounts, scoped firewall, fail2ban, unattended-upgrades, surface reduction, verify-and-record; new `security` category, announced live by the listing per CONTRIBUTING - SKILL.md body ported verbatim from the source copy; front matter adapted to the four-key bundle contract (SPEC 4.2-4.3) - MEMORIES.md starting template shipped beside it (SPEC 4.7); the installed memory is add-only so live instances keep their entries (SPEC 6.8) - tests: bundled load, category listing, and category filter for the new skill; autotest fully green (84 tests, 93 percent coverage); e2e-verified via the installed pipx CLI Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
9.7 KiB
9.7 KiB
name, version, description, category
| name | version | description | category |
|---|---|---|---|
| linux-server-hardening | 1.2.0 | Load this skill when a Linux server needs hardening before or after exposure to a network (Debian or Raspberry Pi homelab) - a numbered, human-followable checklist that audits listeners and accounts, enforces SSH key-only, closes unused accounts, scopes the firewall, sets up fail2ban and automatic security updates, and verifies every change with a real probe. | security |
Linux Server Hardening
- 1. How to use this checklist
- 2. Audit the exposure
- 3. Harden SSH
- 4. Close unused accounts
- 5. Firewall the ingress
- 6. Brute force protection
- 7. Automatic security updates
- 8. Reduce the listening surface
- 9. Verify and record
- 10. Memory
A hardening checklist for a Debian-family Linux server, written
so a human can follow it top to bottom. Every heading and list
item has a stable id; cite one as item 3.4. Items are facts to
hold and gotchas; boxes are steps to do and verify. The ground
rule throughout: a probe decides, memory and recollection do not.
1. How to use this checklist
- 1.1 Work in order 2 to 9: audit first (read-only), then change, then verify. Never batch two changes between probes.
- 1.2 The skill assumes a Debian-family host with sudo; adapt names and paths to the distribution in front of you.
- 1.3 Before editing any live config, copy it aside:
sudo cp -a <file> /tmp/<file>.bak-$(date +%Y%m%d). - 1.4 Edit live config in place; never reconstruct it from a template or memory. Diff against the backup after the edit.
- 1.5 Every change gets a recorded one-line undo (the backup path, the prior shell, the old rule set).
- 1.6
sudois assumed on every command below; do not run a shell as root.
2. Audit the exposure
Read-only; change nothing here. Build the picture before touching anything.
- 2.1 Inventory the TCP and UDP listeners with their
owning processes:
sudo ss -tlnpandsudo ss -ulnp. - 2.2 For every listener, name the service, why it runs, and who may reach it (public, LAN, loopback). No unexplained listener stays unexplained.
- 2.3 Inventory accounts that can log in:
awk -F: '$7 ~ /(sh|bash)$/ {print $1":"$3}' /etc/passwd. - 2.4 Inventory accounts with a usable password:
sudo awk -F: '$2 ~ /^\$/ {print $1}' /etc/shadow. - 2.5 Inventory sudo members:
getent group sudo, and checkauthorized_keysof every human account for content. - 2.6 Record the firewall state:
sudo ufw status verbose(or the nftables equivalent). - 2.7 Record pending updates:
apt list --upgradable 2>/dev/null | tail -n +2 | wc -land flag the security ones. - 2.8 A listener is not an open port: cross the listener list with the firewall rules. "Open" means reachable from a given source, and only the firewall says which sources those are.
3. Harden SSH
- 3.1 Keep everything in a drop-in, not the main file:
/etc/ssh/sshd_config.d/10-no-password.conf(included at the top ofsshd_configon Debian). - 3.2 Write the drop-in with:
PasswordAuthentication no,KbdInteractiveAuthentication no,PermitRootLogin no,MaxAuthTries 3,X11Forwarding no,LoginGraceTime 30. - 3.3 Validate before activating:
sudo sshd -tmust print nothing. - 3.4 Activate gracefully:
sudo systemctl reload ssh. A reload drops no existing session; a restart can. - 3.5 Verify the effective config, not the file:
sudo sshd -T | grep -Ei '^(passwordauthentication| kbdinteractiveauthentication|permitrootlogin)'. - 3.6 Verify behavior with a live probe:
ssh -o PreferredAuthentications=none -o ConnectTimeout=5 localhostmust answerPermission denied (publickey). - 3.7 GOTCHA: if VNC or other services tunnel over SSH
(
ssh -L),AllowTcpForwardingmust stayyes. Turning it off silently kills the tunnels and looks like "server not responding". - 3.8 GOTCHA:
PermitRootLogin without-password(the default) still allows root login with a key. An empty/root/.ssh/authorized_keyssaves you today; saynoanyway. - 3.9 Check the protocol only on museum-grade hosts: SSH
protocol 2 is enforced by the software itself on OpenSSH
7.6 and later - the
Protocoldirective was removed from the config language and modern sshd cannot speak protocol 1. Verify withssh -v(Remote protocol version 2.0). A "force protocol 2" item in a modern guide is a staleness marker for the guide, not a gap in the host.
4. Close unused accounts
- 4.1 For each candidate account, verify first that
nothing uses it:
ps -eo user,args | grep <name>,sudo crontab -l -u <name>, enabled units, recent logins (sudo last | grep <name>). - 4.2 Lock the password:
sudo passwd -l <name>. - 4.3 Set the shell to
/usr/sbin/nologin:sudo usermod -s /usr/sbin/nologin <name>. - 4.4 Verify:
sudo passwd -S <name>showsL, andgetent passwd <name>shows the nologin shell. - 4.5 GOTCHA: a password lock does NOT block SSH key login.
An account with content in
authorized_keysstays reachable until its shell is nologin (or the keys are removed). - 4.6 Record the undo:
sudo passwd -u <name>plussudo usermod -s /bin/bash <name>(or the prior shell).
5. Firewall the ingress
- 5.1 Set the policy:
sudo ufw default deny incomingandsudo ufw default allow outgoing. - 5.2 Add allow rules scoped to a source, never to
Anywherewithout a reason; write a comment per rule:sudo ufw allow from 192.168.178.0/24 to any port 22 proto tcp comment 'SSH from LAN'. - 5.3 If access arrives through a proxy or jump host, allow exactly that one IP for that port.
- 5.4 Enable and verify:
sudo ufw --force enable; sudo ufw status verbose. The current SSH session must survive the enable; if it does not, you locked yourself out. - 5.5 Cover both stacks: repeat rules for the IPv6 prefixes in use, and note that an ISP global prefix can rotate.
6. Brute force protection
- 6.1 Install and enable fail2ban with the sshd jail
(Debian default ships it); verify it counts probes:
sudo fail2ban-client status sshd. - 6.2 GOTCHA: if public access arrives through a stream or reverse proxy, every internet probe reaches sshd with the proxy's IP. A ban can then only hit the proxy - cutting the owner's own public path (a self-DoS an attacker can trigger) while real attacker IPs stay invisible.
- 6.3 In that topology, add the proxy to ignoreip
(
/etc/fail2ban/jail.local,[DEFAULT]section:ignoreip = 127.0.0.1/8 ::1 <proxy-ip>), reload, and verify withsudo fail2ban-client get sshd ignoreip. - 6.4 Per-client banning in that topology belongs on the proxy host, against its own (real-client-IP) logs. Do not pretend it works on the backend box.
7. Automatic security updates
- 7.1 Install:
sudo apt-get install -y unattended-upgrades. - 7.2 Enable: write
/etc/apt/apt.conf.d/20auto-upgradeswithAPT::Periodic::Update-Package-Lists "1";andAPT::Periodic::Unattended-Upgrade "1";. - 7.3 Verify the timers are enabled:
systemctl is-enabled apt-daily.timer apt-daily-upgrade.timer. - 7.4 Verify the origins with
sudo unattended-upgrades --dry-run --debug: the distribution's security origin must be in the allowed list. - 7.5 GOTCHA: third-party repositories (vendor repos such as
the Raspberry Pi Foundation origin) are not in the default
allow list and stay manual; re-check
apt list --upgradableoccasionally.
8. Reduce the listening surface
- 8.1 For every listener that survived the audit without
a reason, disable both the service and its socket:
sudo systemctl disable --now <name>.service <name>.socket(example: rpcbind, when no NFS or rpc is in use). - 8.2 Verify the port is gone from
sudo ss -tlnpandsudo ss -ulnp; a socket unit can re-activate a service. - 8.3 Prefer not running a service over filtering it: a disabled service is closed to every source, forever.
9. Verify and record
- 9.1 Re-run the probes of section 2; compare listener by listener and rule by rule against the pre-hardening picture.
- 9.2 Test the public path from a real outside leg (a phone on mobile data), not from inside the LAN, and not through a VPN.
- 9.3 GOTCHA: consumer VPN exits (verified with Mullvad)
silently drop non-web ports; a failed TCP test through such a
tunnel says nothing about the port. If a question remains, use
https://check-host.net/check-tcp?host=<ip>:<port>or a phone on mobile data. - 9.4 Record what changed and how to undo it on the host's wiki entity page, then save the change set as one commit. A hardening step that is not written down did not happen.
10. Memory
- 10.1 After loading this skill, read MEMORIES once per session. When it is already in context, do not read it again.
- 10.2 Update it whenever an important memory about this skill comes up: a lasting preference, a correction, a repeated request, or a fact a run verified. Also update it whenever the user explicitly asks you to remember something for this skill. Section 1 of that file says how.