🛡️ Server Hardening Guide
A comprehensive, production-ready Linux server hardening project. Covers UFW firewall, SSH, kernel parameters, fail2ban, audit logging, and more.
📋 Table of Contents
- Overview
- Project Structure
- Quick Start
- Hardening Phases
- Phase 1: UFW Firewall
- Phase 2: SSH Hardening
- Phase 3: Kernel Hardening (sysctl)
- Phase 4: Fail2Ban
- Phase 5: System Hardening
- Phase 6: Audit Logging
- Verification & Testing
- Rollback
- Git & Publishing
🎯 Overview
This project provides hardening scripts and configurations for a production Ubuntu/Debian server. Each phase is modular — you can apply them independently.
Threat model addressed: - Brute-force SSH attacks - Port scanning / enumeration - Privilege escalation - Network-based attacks - Unauthorized access
📁 Project Structure
server-hardening/
├── readme.md # This file
├── scripts/
│ ├── 01-ufw-setup.sh # UFW firewall rules
│ ├── 02-ssh-hardening.sh # SSH configuration
│ ├── 03-kernel-hardening.sh # sysctl parameters
│ ├── 04-fail2ban-setup.sh # Fail2Ban install & config
│ ├── 05-system-hardening.sh # System-level hardening
│ ├── 06-audit-setup.sh # Audit logging (auditd)
│ └── harden-all.sh # Run all phases in sequence
├── config/
│ ├── ufw/
│ │ └── user.rules.example # Example UFW rules
│ ├── ssh/
│ │ └── sshd_config.hardened # Hardened SSH config
│ ├── sysctl/
│ │ └── 99-hardening.conf # Kernel parameters
│ ├── fail2ban/
│ │ ├── jail.local # Fail2Ban jail config
│ │ └── filter.d/ # Custom filters
│ └── audit/
│ └── audit.rules # Audit rules
└── docs/
├── checklist.md # Security checklist
└── verification.md # How to verify hardening
🚀 Quick Start
# Clone or copy this project to your server
cd /opt/server-hardening
# Make scripts executable
chmod +x scripts/*.sh
# RECOMMENDED: Run phases individually and review each step
sudo bash scripts/01-ufw-setup.sh
sudo bash scripts/02-ssh-hardening.sh
sudo bash scripts/03-kernel-hardening.sh
sudo bash scripts/04-fail2ban-setup.sh
sudo bash scripts/05-system-hardening.sh
sudo bash scripts/06-audit-setup.sh
# OR run everything at once (experienced users only)
sudo bash scripts/harden-all.sh
⚠️ Always run these scripts with a secondary terminal session open in case of SSH lockout.
🔥 Phase 1: UFW Firewall
Script: scripts/01-ufw-setup.sh
Configures UFW with: - Default deny all incoming - Allow outgoing - SSH with rate limiting - HTTP/HTTPS - Custom application ports
sudo bash scripts/01-ufw-setup.sh
See the full UFW documentation: ../ufw-firewall/readme.md
🔐 Phase 2: SSH Hardening
Script: scripts/02-ssh-hardening.sh
Config: config/ssh/sshd_config.hardened
Applies: - Disable root login - Key-based auth only (no passwords) - Restrict allowed users - Change default port (optional) - Idle timeout configuration - Disable X11 forwarding
sudo bash scripts/02-ssh-hardening.sh
🧠 Phase 3: Kernel Hardening (sysctl)
Script: scripts/03-kernel-hardening.sh
Config: config/sysctl/99-hardening.conf
Kernel parameters hardened: - IP spoofing protection - SYN flood protection - ICMP redirect protection - Log martian packets - Disable IP source routing - Enable ASLR (Address Space Layout Randomization) - Restrict core dumps - Disable IPv6 (optional)
sudo bash scripts/03-kernel-hardening.sh
🚫 Phase 4: Fail2Ban
Script: scripts/04-fail2ban-setup.sh
Config: config/fail2ban/jail.local
Configures: - SSH brute-force protection (ban after 3 attempts) - HTTP auth protection - Custom ban times (1 hour default, 1 week for repeat offenders) - Email notifications (optional)
sudo bash scripts/04-fail2ban-setup.sh
🔩 Phase 5: System Hardening
Script: scripts/05-system-hardening.sh
Applies:
- Disable unused services (avahi, cups, bluetooth)
- Automatic security updates (unattended-upgrades)
- Secure shared memory (/dev/shm)
- Restrict cron to authorized users
- Set proper file permissions on sensitive files
- Disable USB storage (optional)
- Install and configure rkhunter (rootkit hunter)
- Disable compilers for non-root users
sudo bash scripts/05-system-hardening.sh
📋 Phase 6: Audit Logging
Script: scripts/06-audit-setup.sh
Config: config/audit/audit.rules
Tracks: - Authentication events (login/logout/sudo) - File system changes to critical paths - Network connections - Process execution - User/group modifications
sudo bash scripts/06-audit-setup.sh
# View audit logs
sudo ausearch -k ssh_auth | tail -20
sudo ausearch -k privileged | tail -20
🔍 Verification & Testing
See docs/verification.md for detailed testing steps.
# Quick verification commands
sudo ufw status verbose
sudo sshd -T | grep -E "permitrootlogin|passwordauthentication|port"
sudo sysctl net.ipv4.conf.all.rp_filter
sudo fail2ban-client status sshd
sudo systemctl list-units --state=running | grep -v systemd
↩️ Rollback
Each script creates backups before modifying configs:
# Restore SSH config
sudo cp /etc/ssh/sshd_config.bak /etc/ssh/sshd_config
sudo systemctl restart sshd
# Restore sysctl
sudo cp /etc/sysctl.d/99-hardening.conf.bak /etc/sysctl.d/99-hardening.conf
sudo sysctl --system
# Disable UFW (emergency)
sudo ufw disable
# Restore fail2ban
sudo systemctl stop fail2ban
sudo cp /etc/fail2ban/jail.local.bak /etc/fail2ban/jail.local
sudo systemctl start fail2ban
📤 Git & Publishing
This project is hosted on GitHub: https://github.com/doktorlafe/ubuntu26-hardening
First push (initial setup)
# 1. Add all untracked project files (config/, docs/, scripts/, readme.md)
git add config/ docs/ scripts/ readme.md
# 2. Commit them (adjust the message as needed)
git commit -m "Add hardening scripts, configs and docs"
# 3. Make sure the branch is named `main`
git branch -M main
# 4. Push and set the upstream (-u) so future pushes are just `git push`
git push -u origin main
Subsequent pushes
git add .
git commit -m "Describe the change"
git push
Useful checks
git remote -v # show the configured remote
git status # show modified / untracked files
git branch -vv # show branch + upstream tracking
⚠️ Note: the repo contains both
README.md(one line, tracked) andreadme.md(full guide). GitHub and case-insensitive filesystems (Windows/macOS) treat these as the same name, which can cause checkout conflicts. If you only want one, keepreadme.mdand delete the redundantREADME.md.