server-security-hardening
Skill0 starsChanged 11 days agoMITView on GitHub
Harden a Ubuntu VPS end to end — admin user, sshd lockdown, UFW incl. the Docker bypass, DOCKER-USER guard, fail2ban, unattended-upgrades, sysctl, nginx hardening, upgrade+reboot verification. Use when setting up a new server, when asked to "harden/secure" a server, or after a major apt upgrade on such a server.
In this repository34 other files it gives its agents.
- chatgpt/AGENTS.mdAGENTS.md
- opencode/AGENTS.mdAGENTS.md
- zcode/AGENTS.mdAGENTS.md
- claude/CLAUDE.mdCLAUDE.md
- agent-doc-authoringSkill
- agent-experienceSkill
- ai-image-promptsSkill
- api-behavior-flowSkill
- architecture-and-performanceSkill
- behavioral-testingSkill
- behavior-auditSkill
- browser-tabs-uiSkill
- code-qualitySkill
- context-compactSkill
- design-principlesSkill
- design-to-deliverySkill
- design-validationSkill
- e2e-test-authoringSkill
- engineering-reviewSkill
- excalidrawSkill
- external-ai-consultSkill
- google-omni-videoSkill
- llm-agent-tool-disciplineSkill
- parallel-subagentsSkill
- qa-test-scenariosSkill
- reliable-executionSkill
- research-and-reuseSkill
- session-handoffSkill
- shadcn-librarySkill
- sudo-handoffSkill
- task-intakeSkill
- tauri-v2-docsSkill
- test-failure-triageSkill
- user-behavior-flowSkill
The file
We split this skill into the rules it gives an agent
6 rules. About 0 are common, 2 are distinctive, and 4 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 12
After a large apt upgrade on any of these servers (verify everything survived).
- Very specific: line 21
If the target repo ships a hardening script (e.g.
- Very specific: line 62
Never run the sample userdel -r during an ordinary hardening pass.
- Very specific: line 64
No application port on 0.0.0.0 — 127.0.0.1:<host>:<container> only.
- Distinctive: line 77
Run applicable post-change checks above after reload and again after any authorized reboot.
- Distinctive: line 78
Verify external exposure from an approved client and preserve rollback/recovery access.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Server Security Hardening — Ubuntu VPS
When to use
- A new server was bootstrapped and needs the security layer.
- The user asks to secure/harden a server.
- After a large
apt upgradeon any of these servers (verify everything survived).
Ground rules (apply before touching anything)
- This is an existing-server recipe, not blanket permission to upgrade, reboot, remove users or replace firewall policy. Inspect OS/Docker/firewall versions, current access and backups first. Keep an established SSH session and a recovery path; use
sudo-handoffif root access is genuinely needed and unavailable. - The usernames, ports and historical exclusions below belong to the original deployment. Infer the current host's authorized policy rather than copying them or treating an old backup exclusion as a universal preference.
- Run everything inside server-side tmux (
tmux new -d -s harden "bash x.sh 2>&1 | tee x.log"), fetch results withscp. Inline SSH stdout garbles when the user's connection drops; a half-applied sshd change is the worst outcome of a dropped packet. - Order is load-bearing: admin user(s) FIRST, sshd lockdown LAST. Never close root login before proving
ssh <admin>@<host>+sudo -n whoamiworks from your workstation. When REPLACING an admin user: create the new one, widenAllowUsersto include old+new, verify the new one from the workstation, then tightenAllowUsersanduserdel -rthe old one (and delete its sudoers file) — never a big-bang rename. - If the target repo ships a hardening script (e.g.
bootstrap/harden-server.sh, this procedure parameterized via its CONFIG block), that script is the canonical sequence; this skill explains the why and the verification.
The layers (in execution order)
- Admin user(s) — one or more; the original deployment used two (
<admin1>,<admin2>). Per user:useradd -m -s /bin/bash -G sudo <admin>; copy root'sauthorized_keyswith correct ownership (install -m 600 -o <admin> -g <admin>);passwd -l <admin>; passwordless sudo via/etc/sudoers.d/90-<admin>(chmod 440,visudo -cto validate). Reason: keys are per-host, and reusing root for daily work is the thing we're eliminating. - Sysctl —
/etc/sysctl.d/99-hardening.conf: rp_filter on, syncookies, accept/send redirects off, source-route off, log_martians, kptr/dmesg restrict, unprivileged BPF disabled. Apply withsysctl --system. - nginx —
server_tokens off;(uncomment the existing commented line innginx.conf— beware the commented line matches greps; verify withcurl -sI, header must shownginxwith no version). - DOCKER-USER guard — Docker's NAT rules BYPASS ufw INPUT for published container ports. Publishing
127.0.0.1:...is the primary control; the safety net is aDOCKER-USERchain rule dropping NEW/UNTRACKED traffic from external interfaces (v4iptables+ v6ip6tables, with ESTABLISHED,RELATED accept first). Installed as oneshotdocker-ufw-guard.service(After/Requires docker). Without this, a compose file withports: ["3101:3101"]would be internet-exposed despite UFW. - fail2ban —
backend = systemd(no auth.log on these images), jails:sshd(maxretry 4, ban 2h) andnginx-http-auth(portshttp,httpsplus the app's public entry range, e.g.3000:3099, logpath/var/log/nginx/error.log) for the basic-auth public entries. - unattended-upgrades — enable; local config
/etc/apt/apt.conf.d/52unattended-upgrades-local: security updates daily,Automatic-Reboot "false"(uptime is a hard requirement for the user — when/var/run/reboot-requiredappears, surface it to the user and let them choose the moment; never reboot on your own initiative), remove unused deps. - sshd lockdown (last) — ONE drop-in
/etc/ssh/sshd_config.d/00-hardening.conf:PermitRootLogin no,AllowUsers <admin1> <admin2> ...,PubkeyAuthentication yes,PasswordAuthentication no,KbdInteractiveAuthentication no,PermitEmptyPasswords no,X11Forwarding no,MaxAuthTries 3. Validatesshd -t, thensystemctl reload ssh(reload, not restart — keeps existing sessions). - Upgrade + reboot — separately from the hardening script, only when authorized. Preview package/removal changes and confirm a maintenance/recovery plan before a full upgrade. Check
/var/run/reboot-required; do not reboot until the user authorizes its timing, then run the post-reboot verification below.
sshd gotcha (bit us once)
sshd applies first-wins per directive. Include order means the drop-in must sort FIRST (00-), and older drop-ins (00-key-only.conf with prohibit-password) silently defeat a later 10-hardening.conf with no. Always end with a single merged 00-hardening.conf; verify with sshd -T | grep -E "permitrootlogin|allowusers" (that shows the EFFECTIVE values, not the files).
Post-reboot verification (every layer)
uname -r # new kernel actually booted
ip route show default # has `src <primary-ip>` (dual-IP egress fix)
sudo docker ps # containers restarted
sudo systemctl is-active docker-ufw-guard fail2ban unattended-upgrades
sudo iptables -S DOCKER-USER | head # guard rules present
sudo ufw status verbose # ACTIVE — see gotcha below
sudo sshd -T | grep -E "^(permitrootlogin|allowusers)"
curl -sk -o /dev/null -w "%{http_code}\n" https://127.0.0.1:<public-entry-port>/ # public entry 401
curl -s -o /dev/null -m 10 -w "%{http_code}\n" https://api.snapcraft.io # egress 200
nslookup google.com # DNS worksFrom your workstation, in this order: ssh <admin>@<host> works → sudo -n whoami → only then confirm root login is refused (Permission denied is the expected result).
UFW gotcha (bit us once)
A large apt upgrade can reinstall/reconfigure ufw and leave it inactive (ENABLED=no in /etc/ufw/ufw.conf) even though rules persist. After any major upgrade: sudo ufw status. Before enabling an inactive firewall, verify the actual SSH port/source allow rule and recovery access; enabling is not automatically safe just because old rules exist.
Rules that must never regress
- Confirm Docker's firewall backend. The
DOCKER-USERrecipe above applies to the iptables backend; nftables uses different hook/chain handling. Consult the matching official backend documentation and verify IPv4/IPv6 exposure; do not create a fictitiousDOCKER-USERchain for native nftables or flush existing policy. - Never run the sample
userdel -rduring an ordinary hardening pass. Account removal needs explicit authorization and preservation of owned files/keys after replacement access is proven. - No application port on 0.0.0.0 —
127.0.0.1:<host>:<container>only; databases get noports:at all. - Public surface = sshd (22) + nginx (80/443) + the deployment's public entry range (in the original deployment 3000–3099; TLS + basic auth + app auth;
/api/websocketlocations needauth_basic off— browsers drop the header on WS handshakes). - Compose review checklist before any deploy (privileged/host modes/docker.sock/host mounts/cap_add/user: root) — see project
AGENTS.md. - Compose/infra changes go through tmux + logs + scp, never long inline SSH.
Non-goals / options not deployed
- WireGuard-only management (no public basic-auth entry) — stronger, but the original deployment chose a public
IP:<port>basic-auth entry for now; kept there as a pending option. - Off-server backups — out of scope in the original deployment; the compose/data layer should eventually add them.
Validation
- Confirm current admin SSH and required privilege access before tightening old access; effective sshd/firewall values match the intended host policy.
- Run applicable post-change checks above after reload and again after any authorized reboot. Mark unavailable checks as not run; never infer successful reboot or protection from written config alone.
- Verify external exposure from an approved client and preserve rollback/recovery access; no unauthorized account removal, upgrade or reboot occurred.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

