This script is designed to perform various security configurations on a Linux-based system, specifically for enabling and configuring UFW (Uncomplicated Firewall), Fail2Ban, Unattended Upgrades, and SSH. It ensures that your system is securely configured with minimal manual intervention.
- The script should be run as root, as it modifies system configurations.
- The system must have the following packages available:
ufw(Uncomplicated Firewall)fail2banunattended-upgrades
- Resets and configures UFW with default rules for SSH, HTTP, and HTTPS.
- Installs and configures Fail2Ban to protect SSH from brute-force attacks.
- Installs and configures unattended-upgrades to ensure the system receives automatic security updates.
- Configures SSH to allow only the current user.
- Updates and cleans the system by performing an upgrade and ensuring all packages are up-to-date.
This function resets UFW to avoid conflicts and configures it to allow traffic on SSH, HTTP, and HTTPS ports.
ufw allow ssh
ufw allow http
ufw allow https
ufw logging onInstalls Fail2Ban and configures it to protect SSH. It also checks for the existence of the /var/log/auth.log file and creates it if missing.
apt-get install -y fail2banInstalls and configures the unattended-upgrades package for automatic security updates. It ensures that the Raspbian and Raspberry Pi Foundation repositories are included in the upgrade sources.
apt-get install -y unattended-upgrades
dpkg-reconfigure --priority=low unattended-upgradesConfigures SSH to allow only the current user for secure remote access. It checks if the current user is already added to the AllowUsers directive in /etc/ssh/sshd_config.
AllowUsers your_usernameUpdates the system and upgrades all installed packages to their latest versions.
apt-get update && apt-get upgrade -yThe main function ties all the above functions together and ensures the entire security configuration process is automated and executed in the correct order.
reset_and_configure_ufw
install_and_configure_fail2ban
install_and_configure_unattended_upgrades
configure_sshd
update_and_clean- Save the script to a file, for example,
security_config.sh. - Give it executable permissions:
chmod +x securepi.sh
- Run the script as root:
sudo ./securepi.sh
- It is also possible to run the script without needing to download:
curl https://raw.githubusercontent.com/lefht/securepi/refs/heads/main/securepi.sh | ssh pi_usr@hostip 'sudo bash -s'
If you prefer to use Ansible for configuration management, follow these steps:
-
Ensure that Docker is installed and configured on your system.
-
Clone the repository or ensure the
inventoryandplaybooksdirectories are accessible. -
Add your hosts and credentials and run the following command.
make run
- Root privileges: Ensure the script is executed as root for proper configuration changes.
- Logging: The script logs progress to the console for easy troubleshooting.
- SSH Configuration: This script configures SSH to only allow the current user. Ensure your user is correctly identified.
This script simplifies the process of securing a Raspberry Pi by automating common security configurations. By running it, you ensure your system is protected with a:
- firewall
- fail2ban
- automatic security updates
- secure SSH configuration.