Step 7 of 9

Enable And Configure Firewall on Ubuntu Server

1 min

All ubuntu logo 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.

Allow outgoing trafic bash logo
sudo ufw default allow outgoing
Deny incoming trafic bash logo
sudo ufw default deny incoming

Allow SSH

By OpenSSH application
sudo ufw allow OpenSSH
By Service name
sudo ufw allow ssh
By Port number
sudo ufw allow 22/tcp
Non standard SSH port
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.

bash logo
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

bash logo
sudo ufw enable

You will be asked to confirm, type y and hit enter.

Check the status

bash logo
sudo ufw status
Output text logo
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.