Custom Firewall

UFW Firewall Setup on Ubuntu VM

You can attach a UFW firewall to your VMs. Enabling the UFW firewall may break SSH access to your VM or other open ports.Please follow these steps to ensure SSH access is maintained.


Solution

When enabling a firewall on a remote server that you connect to using SSH, make sure to adjust the default policy to allow connections instead of the default deny. Otherwise, enabling the firewall could disconnect your remote session and disable access to the server.


Step-by-Step Instructions

Step 1: Set the default policy to allow

Run the following command to set the default policy to allow all connections. This will prevent being locked out once the firewall is enabled:

sudo ufw default allow

Step 2: Enable the firewall

Enable the firewall with the following command. This will apply the settings:

1sudo ufw enable

Step 3: Allow SSH access (Port 22)

Use the following command to allow incoming TCP connections on port 22 (the default SSH port):

1sudo ufw allow 22/tcp

Step 4: Set the default policy to deny

Once SSH access is allowed, you can change the default policy to deny all incoming connections. This makes your server more secure by only allowing traffic on explicitly open ports:

1sudo ufw default deny

Following these steps ensures that SSH access remains available while securing your VM with the UFW firewall.

Last updated

Was this helpful?