This script automates basic Ubuntu server hardening steps. It interactively asks for user input to customize the setup.
Run this one-liner on your Ubuntu server:
bash <(curl -Ls https://raw.githubusercontent.com/enavid/ubuntu-hardener/main/harden.sh)- Updates all packages using
apt update && apt upgrade - Prompts for a new SSH port and configures it
- Prompts for firewall ports to allow (e.g.,
80,443,22) - Installs UFW firewall and applies your custom rules
- Optionally creates a new user and adds them to the sudo group
- Disables root login over SSH for better security
- Restarts SSH service to apply changes
- Install fail2ban and users with 5 failed SSH attempts will be blocked for 72 hour.
- Performs basic cleanup with
apt autoremove
- Make sure the new SSH port is allowed in your cloud provider’s firewall or security group before running this script.
- After the script finishes, you will no longer be able to log in as root via SSH.
- It is strongly recommended to create a new sudo user for administrative access.
- Ubuntu server
- Must be run as
rootor withsudo