Protect Linux servers from automated dictionary attacks, unauthorized ports, and malicious SSH brute force
By following this guide, you will lock down unnecessary ports on your Ubuntu server using Uncomplicated Firewall (UFW) and deploy Fail2ban to automatically block malicious IP addresses attempting brute-force SSH logins.
Spin up an Ubuntu 24.04 instance with instant activation to test this tutorial:
Deploy IndSoft VPSAny Ubuntu 22.04 or 24.04 server. Works on all IndSoft VPS and Dedicated Servers.
Configure safe default policies: drop incoming connections and allow outgoing traffic.
sudo ufw default deny incoming
sudo ufw default allow outgoingCRITICAL: Always allow SSH before turning on UFW to avoid locking yourself out of your server.
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enableInstall Fail2ban and copy the base configuration to a persistent local jail file.
sudo apt install fail2ban -y
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.localConfigure maxretry = 5 and bantime = 1h under the [sshd] section in jail.local, then restart the service.
sudo systemctl restart fail2ban
sudo fail2ban-client status sshdRun this quick verification check to confirm your installation is responding properly:
sudo fail2ban-client pingQuick resolutions for frequent setup pitfalls and configuration issues.
sudo ufw allow 22/tcp.
backend = systemd is set under [DEFAULT] in /etc/fail2ban/jail.local.
sudo fail2ban-client set sshd unbanip YOUR_IP_ADDRESS.