Enable And Configure Firewall on Ubuntu Server
1 minAll Ubuntu servers come with the ufw (Uncomplicated Firewall) firewall preinstalled, it is disabled by default.
Setup the default policies
Since this is the first time setting up the firewall, it is a good practice to put some good default policies, My policy is simple, I want to allow all out going traffic by default, and block all incoming by default. This means that allowing an incoming connection is explicit, because that is where the real danger is.
sudo ufw default allow outgoing
sudo ufw default deny incoming
Allow SSH
sudo ufw allow OpenSSH
sudo ufw allow ssh
sudo ufw allow 22/tcp
sudo ufw allow 2222/tcp
# Replace 2222 with your actual SSH port number
This will allow ssh connection (port 22 by default)
Rate limiting SSH
You can easily enable rate limiting for ssh directly from ufw this will help protect against brute force attacks.
sudo ufw limit ssh
Once this is enabled, any IP that makes 6 failed attempts in a 30 seconds window will be blocked temporarily, this will help you stay safe without inconveniencing legitimate server users.
Enable the firewall
sudo ufw enable
You will be asked to confirm, type y and hit enter.
Check the status
sudo ufw status
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
22/tcp LIMIT Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
22/tcp (v6) LIMIT Anywhere (v6)
This means the firewall is active, and the OpenSSH application is allowed.
