# System Hardening Locking down a Linux system for homelab use. This covers SSH daemon configuration, firewall rules, intrusion prevention, automatic updates, and service auditing. These settings apply to any Debian-based system (Ubuntu, Raspbian, Debian) — the config files and workflows are the same. For a step-by-step walkthrough on Raspberry Pi nodes specifically, see [[Hardening a Raspberry Pi|Hardening Raspberry Pis]]. --- ## SSH Configuration ### Configuration File The main SSH daemon config lives at `/etc/ssh/sshd_config`. On Ubuntu, drop-in files from `/etc/ssh/sshd_config.d/*.conf` are processed first and can override settings in the main file — check these if a setting doesn't seem to take effect. To view only active (non-commented) settings: ```bash cat /etc/ssh/sshd_config | grep -v "^#" | grep -v "^quot; ``` `grep -v "^#"` strips comment lines, `grep -v "^quot;` strips blank lines. This pattern works for reading any config file quickly. ### Hardening Settings Edit the config with `sudo nano /etc/ssh/sshd_config` and set the following: |Setting|Value|Why| |---|---|---| |`PermitRootLogin`|`no`|Prevents direct SSH login as root. Attackers try root first — this shuts that down. SSH in as your user and `sudo` when needed.| |`PasswordAuthentication`|`no`|Keys only. Brute-force attacks become impossible since there's no password to guess.| |`X11Forwarding`|`no`|Disables GUI app forwarding over SSH. Unnecessary on a headless server and reduces attack surface.| |`MaxAuthTries`|`3`|Limits authentication attempts per connection from the default of 6. Minor hardening but good hygiene.| These lines may already exist but be commented out with `#`. Remove the `#` and set the value, or add them if they're missing entirely. ### Applying Changes Safely Follow this workflow every time you modify SSH config. #### Step 1 — Test the Config ```bash sudo sshd -t ``` No output means no errors. If it prints anything, fix the syntax before proceeding. #### Step 2 — Restart the Daemon ```bash sudo systemctl restart sshd ``` #### Step 3 — Verify Access Open a **new terminal tab** and SSH into the machine. Do not close your existing session until the new session works. Your current session is your lifeline if something went wrong. > [!warning] Always verify access in a new session before closing your existing one. If you disabled password auth and key auth isn't working, your current session is the only way to fix it. ### Lockout Recovery If your private key is lost (laptop dies, drive wiped) and password auth is disabled, you're locked out over the network. On a homelab machine, recover by plugging in a keyboard and monitor and logging in locally to either re-enable password auth or add a new public key. Precautions against lockout: - Back up your private key to an encrypted USB drive or a password manager that supports file attachments. - Generate keys from multiple devices and add all public keys to `authorized_keys` — it supports multiple entries. - Physical access is your safety net — this is a major advantage of a homelab over a cloud VPS. ### Final SSH Config Reference After hardening, the active config across homelab hosts: ``` Include /etc/ssh/sshd_config.d/*.conf PermitRootLogin no MaxAuthTries 3 PasswordAuthentication no KbdInteractiveAuthentication no UsePAM yes X11Forwarding no PrintMotd no AcceptEnv LANG LC_* Subsystem sftp /usr/lib/openssh/sftp-server ``` ### Optional — Cron Job for SSH Server Updates Schedule a daily check for OpenSSH updates: ```bash sudo crontab -e ``` Add the following line: ``` 0 5 * * * apt install openssh-server ``` The system will check for SSH server updates every day at 5 AM. --- ## Firewall — Uncomplicated Firewall (UFW) UFW provides a simple interface for managing `iptables` rules. The goal is to deny everything incoming by default, then explicitly allow only what's needed. ```bash sudo apt install ufw sudo ufw default deny incoming sudo ufw default allow outgoing sudo ufw allow in on tailscale0 sudo ufw allow 22/tcp sudo ufw enable ``` |Command|What It Does| |---|---| |`sudo ufw default deny incoming`|Deny all incoming traffic by default — closes all ports| |`sudo ufw default allow outgoing`|Allow all outgoing traffic — the node can reach other nodes/services| |`sudo ufw allow in on tailscale0`|Allow all traffic on the Tailscale interface| |`sudo ufw allow 22/tcp`|Allow SSH on port 22| |`sudo ufw enable`|Activate the firewall| > [!warning] Ensure port 22 and Tailscale are allowed **before** enabling UFW. Enabling without SSH allowed will lock you out. To check current rules after enabling: ```bash sudo ufw status verbose ``` --- ## Fail2Ban Even with key-only authentication, Fail2Ban adds a layer of defense by monitoring logs and auto-blocking IPs with repeated failed attempts. After installation, Fail2Ban should run automatically. Check its status: ```bash sudo systemctl status fail2ban ``` If it's not running, enable and start it: ```bash sudo systemctl enable fail2ban sudo systemctl start fail2ban ``` Fail2Ban reads its jail configuration from `/etc/fail2ban/jail.conf` (defaults) and `/etc/fail2ban/jail.local` (overrides). The SSH jail is enabled by default on most Debian-based systems. --- ## Automatic Security Updates Configure `unattended-upgrades` so critical patches are applied automatically between manual update sessions: ```bash sudo apt update && sudo apt upgrade -y sudo apt install unattended-upgrades -y sudo dpkg-reconfigure --priority=low unattended-upgrades ``` `unattended-upgrades` runs in the background and installs security updates as they become available. --- ## Disable Unused Services Every running service is a potential attack surface. List all active services and look for anything unnecessary (e.g., Bluetooth on a headless server): ```bash sudo systemctl list-units --type=service --state=running ``` Also check for default users with known usernames (like `pi` on Raspbian), which should be removed or renamed: ```bash cat /etc/passwd | grep pi ``` The goal is to minimize what's running and what's reachable. If a service isn't needed, disable it. --- ## See Also - [[SSH Key-Based Authentication]] — prerequisite: generating and deploying Ed25519 key pairs - [[Hardening a Raspberry Pi|Hardening Raspberry Pis]] — step-by-step walkthrough applying these settings to Raspberry Pi nodes ## Resources - [OpenSSH Manual — sshd_config](https://man.openbsd.org/sshd_config) - [Mozilla SSH Guidelines](https://infosec.mozilla.org/guidelines/openssh) - [UFW Documentation — Ubuntu](https://help.ubuntu.com/community/UFW) - [Fail2Ban Documentation](https://www.fail2ban.org/wiki/index.php/Main_Page)