Comprehensive automated security hardening for Raspberry Pi. One script to transform your Pi into a fortress.
This script transforms a fresh Raspberry Pi installation from default configuration to a hardened, secure system. It automates hours of manual security configuration into a single command.
Perfect for:
- Home servers and NAS systems
- IoT projects exposed to the internet
- Development environments
- Learning security best practices
- Any Pi that needs protection
- Automatic system updates with unattended security patches
- Strong password enforcement
- Secure file permissions and system limits
- Unused account lockdown
- Root login disabled
- Strong encryption (ChaCha20-Poly1305, AES-256-GCM)
- Key-based authentication support
- Maximum 3 login attempts
- Connection timeouts and session limits
- UFW firewall (default deny incoming)
- Fail2Ban intrusion prevention
- SYN flood protection
- IP spoofing protection
- Suspicious packet logging
- Rootkit detection (rkhunter, chkrootkit)
- System audit logging
- Custom security check script
- Disable Bluetooth (if not needed)
- Disable WiFi (for Ethernet-only setups)
- Create new admin user
- Secure shared memory
Important: This script is designed for Raspberry Pi OS installations configured with the Raspberry Pi Imager. Make sure you've set up your username, password, and SSH settings during the imaging process.
Download the script:
wget https://raw.githubusercontent.com/Sharonhazan/pi-fortress/main/pi-fortress.shMake it executable:
chmod +x pi-fortress.shRun as root:
sudo ./pi-fortress.shThe script will:
- β Update all system packages
- β Configure SSH security settings
- β Install and configure Fail2Ban
- β Set up UFW firewall
- β Install security monitoring tools
- β Apply network hardening
- β Create security check script
Total time: ~5-10 minutes (depending on your internet speed)
| Package | Purpose |
|---|---|
fail2ban |
Blocks IPs after failed login attempts |
ufw |
Simple, effective firewall |
unattended-upgrades |
Automatic security updates |
rkhunter |
Rootkit detection |
chkrootkit |
Additional rootkit scanner |
logwatch |
Log analysis |
auditd |
System auditing |
The script keeps password authentication enabled initially. After setting up SSH keys, you should disable it.
On your local machine: Generate SSH key (if you don't have one):
ssh-keygen -t ed25519 -C "your_email@example.com"Copy to your Pi:
ssh-copy-id username@your-pi-ipTest it works:
ssh username@your-pi-ipOnce working, disable password authentication: Edit SSH configuration:
sudo nano /etc/ssh/sshd_config.d/hardening.confUncomment this line:
PasswordAuthentication no
Restart SSH:
sudo systemctl restart sshReboot your Pi:
sudo rebootRun the security check:
sudo security-check.shThis shows:
- Failed login attempts
- Fail2Ban status
- Firewall rules
- Recent logins
- Open ports
- Rootkit scan
β Root login: DISABLED
β Password auth: Enabled initially (disable after key setup)
β Max auth tries: 3
β Login timeout: 60 seconds
β Idle timeout: 5 minutes
β Strong ciphers only
β Ban after: 3 failed attempts
β Ban duration: 2 hours
β Find time: 10 minutes
β Email alerts: Configurable
β Default incoming: DENY
β Default outgoing: ALLOW
β SSH port 22: ALLOW
β Custom ports: Easy to add
Allow HTTP:
sudo ufw allow 80/tcpAllow HTTPS:
sudo ufw allow 443/tcpAllow custom application:
sudo ufw allow 8080/tcpCheck firewall status:
sudo ufw status numberedView Fail2Ban status:
sudo fail2ban-client status sshdSee banned IPs:
sudo fail2ban-client status sshd | grep "Banned IP"Unban an IP:
sudo fail2ban-client set sshd unbanip 192.168.1.100Run rootkit scan:
sudo rkhunter --checkView auth log:
sudo tail -f /var/log/auth.log- β Configure your Pi with Raspberry Pi Imager (set username, password, and enable SSH)
- β This script is for fresh installations - test on existing systems carefully
- β Have physical access available in case of lockout
- β Know your current IP address for SSH access
- π Set up SSH keys before disabling password authentication
- π§ͺ Test SSH access in a new terminal before closing your current one
- π Document any custom ports you open in the firewall
- π Reboot to apply all changes
β Disabling password auth without SSH keys β Lockout
β Not allowing custom ports in UFW β Services won't work
β Forgetting to reboot β Some changes won't apply
Option 1: Physical Access Connect with keyboard and monitor, then:
sudo nano /etc/ssh/sshd_config.d/hardening.confSet this line:
PasswordAuthentication yes
Restart SSH:
sudo systemctl restart sshOption 2: SD Card Method
- Remove SD card from Pi
- Mount on another computer
- Edit
/etc/ssh/sshd_config.d/hardening.conf - Enable
PasswordAuthentication yes - Reinsert and boot
From another IP or local access:
sudo fail2ban-client set sshd unbanip YOUR_IPCheck firewall status:
sudo ufw statusAllow the port:
sudo ufw allow PORT_NUMBER/tcpEdit SSH config:
sudo nano /etc/ssh/sshd_config.d/hardening.confAdd this line:
Port 2222
Update firewall:
sudo ufw allow 2222/tcp
sudo ufw delete allow 22/tcpRestart SSH:
sudo systemctl restart sshInstall mail utilities:
sudo apt install mailutilsEdit Fail2Ban config:
sudo nano /etc/fail2ban/jail.localSet your email:
destemail = your@email.com
Restart Fail2Ban:
sudo systemctl restart fail2banEdit sysctl config:
sudo nano /etc/sysctl.d/99-security.confUncomment these lines:
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
Apply changes:
sudo sysctl -pContributions welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/improvement) - Commit your changes (
git commit -m 'Add improvement') - Push to the branch (
git push origin feature/improvement) - Open a Pull Request
- Support for other Linux distributions
- Additional security checks
- Automated testing
- Documentation improvements
- Translations
MIT License - see LICENSE file for details.
This script implements security best practices but no system is 100% secure. Use at your own risk. Always:
- Test in a safe environment first
- Keep backups of important data
- Monitor your logs regularly
- Stay updated on security threats
The author is not responsible for any damage, data loss, or security breaches.
Sharon Hazan
- GitHub: @Sharonhazan
- Repository: pi-fortress
- Issues: Report bugs or request features
- Discussions: Ask questions or share ideas
- Security Issues: Report privately via GitHub Security Advisories
β If Pi Fortress helped secure your Raspberry Pi, please star the repository!
Made with β€οΈ for the Raspberry Pi community