2025-06-26 21:46:01 +01:00
## Debian & Ubuntu Server Setup & Hardening Script
2025-06-26 19:13:35 +01:00
2025-06-26 21:45:26 +01:00
**Version:** 3.8
**Last Updated:** 2025-06-26
**Compatible With:**
- Debian 12 (Bookworm)
- Ubuntu 20.04 LTS, 22.04 LTS, 24.04 LTS
2025-06-26 19:13:35 +01:00
2025-06-26 21:45:26 +01:00
---
2025-06-26 19:13:35 +01:00
2025-06-26 21:45:26 +01:00
## 📌 Overview
2025-06-26 19:13:35 +01:00
2025-06-26 21:45:26 +01:00
This script automates the secure provisioning and hardening of a fresh Debian or Ubuntu server. It covers essential system settings, user management, SSH hardening, firewall configuration, and optional installation of Docker and Tailscale.
2025-06-26 19:13:35 +01:00
2025-06-26 21:45:26 +01:00
It is designed to be **idempotent** , **safe** , and suitable for **production environments** .
2025-06-26 19:13:35 +01:00
2025-06-26 21:45:26 +01:00
---
2025-06-26 19:13:35 +01:00
2025-06-26 21:45:26 +01:00
## ⚙️ Features
2025-06-26 19:13:35 +01:00
2025-06-26 21:45:26 +01:00
- Root login disabled, new admin user creation
- SSH key-based login support and key detection
- UFW firewall configuration with custom port support
- SSH and system configuration backup and rollback safety
- Timezone and swap file setup
- Optional installation of:
- Docker & Docker Compose
- Tailscale (Mesh VPN)
- Logging to `/var/log/`
- Optional quiet mode for automated scripts
2025-06-26 19:13:35 +01:00
2025-06-26 21:45:26 +01:00
---
2025-06-26 19:13:35 +01:00
2025-06-26 21:45:26 +01:00
## 📥 Installation & Usage
2025-06-26 19:13:35 +01:00
2025-06-26 21:45:26 +01:00
### 1. Download the script
2025-06-26 19:13:35 +01:00
2025-06-26 21:51:07 +01:00
```
wget https://raw.githubusercontent.com/buildplan/setup_harden_server/refs/heads/main/setup_harden_debian_ubuntu.sh
2025-06-26 21:45:26 +01:00
chmod +x setup_harden_debian_ubuntu.sh
````
### 2. Run the script as root
2025-06-26 19:13:35 +01:00
2025-06-26 21:51:07 +01:00
```
2025-06-26 21:45:26 +01:00
sudo ./setup_harden_debian_ubuntu.sh
```
2025-06-26 19:13:35 +01:00
2025-06-26 21:45:26 +01:00
### 3. Optional: Run in quiet mode
2025-06-26 19:13:35 +01:00
2025-06-26 21:51:07 +01:00
```
2025-06-26 21:45:26 +01:00
sudo ./setup_harden_debian_ubuntu.sh --quiet
2025-06-26 19:13:35 +01:00
```
2025-06-26 21:45:26 +01:00
> 🔒 The script must be run as root (or with sudo privileges).
---
## 📂 What It Does
| Task | Description |
| ----------------------------- | ---------------------------------------------- |
| Admin User Creation | Creates new sudo user with password or SSH key |
| SSH Hardening | Disables root login, adjusts secure options |
| Firewall | UFW setup with customisable ports |
| Package Installation | Essential tools (curl, fail2ban, etc.) |
| System Config Backup | Creates backups before making changes |
| Swap File Setup | Creates a swap file with size selection |
| Timezone Selection | Interactive timezone configuration |
| Docker & Tailscale (optional) | Only installed when prompted |
---
## 🪵 Logs & Backups
* **Logs:** `/var/log/setup_harden_debian_ubuntu_*.log`
* **Config Backups:** `/root/setup_harden_backup_*`
* **SSHD Backup:** Restorable from the backup directory in case of issues
---
## 🧪 Tested On
2025-06-26 21:49:05 +01:00
* Debian 12
* Ubuntu 24.04
* VMs, and common VPS providers (OCI, DigitalOcean, etc.)
2025-06-26 19:13:35 +01:00
2025-06-26 21:45:26 +01:00
---
2025-06-26 19:13:35 +01:00
2025-06-26 21:45:26 +01:00
## ❗ Important Notes
2025-06-26 19:13:35 +01:00
2025-06-26 21:45:26 +01:00
* Always test in a VM or staging VPS before using in production.
2025-06-26 21:49:05 +01:00
* Run this script before setting up anything else on brand new VM/VPS.
2025-06-26 21:45:26 +01:00
* Ensure you have console or out-of-band access in case SSH becomes inaccessible.
* A system **reboot is recommended** after running the script.
2025-06-26 19:13:35 +01:00
2025-06-26 21:45:26 +01:00
---
2025-06-26 19:13:35 +01:00
2025-06-26 21:45:26 +01:00
## 🛠 Troubleshooting
2025-06-26 19:13:35 +01:00
2025-06-26 21:45:26 +01:00
* **SSH Locked Out?** Use the server console and restore:
2025-06-26 19:13:35 +01:00
2025-06-26 21:51:07 +01:00
```
2025-06-26 21:45:26 +01:00
cp /root/setup_harden_backup_*/sshd_config /etc/ssh/sshd_config
systemctl restart ssh
```
* **No internet?** The script requires internet access to install packages.
2025-06-26 19:13:35 +01:00
2025-06-26 21:45:26 +01:00
---
2025-06-26 19:13:35 +01:00
2025-06-26 21:45:26 +01:00
## 📝 License
2025-06-26 19:13:35 +01:00
2025-06-26 21:45:26 +01:00
This script is open-source and provided "as is" without warranty.
Use at your own risk.