This Bash script automates the provisioning and hardening of a Debian 12 or Ubuntu server (20.04, 22.04, 24.04 LTS, or 24.10). It configures essential security settings, user management, SSH hardening, firewall rules, and optional features like Docker, Tailscale (with Headscale support), and system monitoring (SMTP and ntfy). The script is idempotent, supports configuration files for automation, and provides flexibility for missing or partial configurations.
## Features
- **System Checks**: Validates root privileges, internet connectivity, and OS compatibility.
- **User Management**: Creates a new admin user with sudo privileges and optional SSH key setup.
- **SSH Hardening**: Configures a custom SSH port, disables root login, and enforces key-based authentication.
- **Firewall (UFW)**: Sets up restrictive firewall rules with customizable ports.
- **Fail2Ban**: Protects against brute-force attacks on SSH and other ports.
You can provide a configuration file (e.g., `/etc/setup_harden.conf`) to automate setup. If variables are missing or invalid, the script will prompt interactively (unless in `--quiet` mode, where it skips optional settings).
- **Required Variables**: `USERNAME`, `HOSTNAME`, `SSH_PORT`. If missing, the script will prompt (non-quiet mode) or fail (quiet mode).
- **Optional Variables**: All others (e.g., `UFW_PORTS`, `SMTP_*`, `NTFY_*`, `TAILSCALE_*`). If partially provided, the script prompts for missing values or skips the feature in quiet mode.
- **SMTP Credentials**: `SMTP_USER` and `SMTP_PASS` are required for SMTP authentication when `SMTP_SERVER` is specified. In non-quiet mode, the script prompts for these if missing. In quiet mode, SMTP configuration is skipped if these are not provided.
- **Validation**: The script validates all inputs (e.g., username format, port numbers, URLs). Invalid values trigger prompts or skipping.
## Flexible Configuration Handling
- **Missing Variables**: If a config file lacks variables, the script prompts interactively (non-quiet mode) or skips optional features (quiet mode).
- **Partial Configurations**: For features like Tailscale, SMTP, or ntfy, if some variables are missing, the script prompts for them or skips the feature in quiet mode.
- **No Config File**: Falls back to full interactive mode (non-quiet) or minimal setup with optional features skipped (quiet).
- **Summary**: The final summary lists applied, skipped, or prompted settings.