How To Setup a Firewall with UFW on an Ubuntu and Debian Cloud Server

If you’re running an Ubuntu or Debian cloud server, it’s important to secure your system from unauthorized access. One way to do this is by configuring a firewall. In this tutorial, we’ll show you how to set up UFW (Uncomplicated Firewall) on an Ubuntu or Debian cloud server to secure your system.

Step 1: Check the UFW Status

Before configuring UFW, you should check the status to see if it’s already enabled. To do this, run the following command:

sudo ufw status

This will display the current status of UFW and the rules that have been configured.

Step 2: Allow Incoming SSH Traffic

SSH is used to remotely access your server, so it’s important to allow incoming SSH traffic. To do this, run the following command:

sudo ufw allow ssh

Step 3: Enable UFW

Once you’ve allowed incoming SSH traffic, you can enable UFW with the following command:

sudo ufw enable

This will enable UFW and apply the rules you’ve configured.

Step 4: Allow Incoming HTTP and HTTPS Traffic

If you’re running a web server, you’ll need to allow incoming HTTP and HTTPS traffic. To do this, run the following commands:

sudo ufw allow http
sudo ufw allow https

This will allow incoming traffic on port 80 for HTTP and port 443 for HTTPS.

Step 5: Allow Incoming Traffic for Other Services

If you’re running other services, such as FTP or SMTP, you’ll need to allow incoming traffic for those services. To do this, run the following commands:

sudo ufw allow ftp
sudo ufw allow smtp

Step 6: Deny Incoming Traffic

If you want to deny incoming traffic for a specific service, you can do so by running the following command:

sudo ufw deny ssh

This will deny incoming traffic on port 22 for SSH.

Step 7: Delete a Rule

If you need to delete a rule, you can do so by running the following command:

sudo ufw delete allow ftp

This will delete the rule that allows incoming traffic on port 21 for FTP.

Step 8: Disable UFW

If you want to disable UFW, you can do so by running the following command:

sudo ufw disable

This will disable UFW and remove all the rules you’ve configured.

Conclusion

By following this tutorial, you can configure UFW on an Ubuntu or Debian cloud server to secure your system from unauthorized access. It’s important to allow incoming traffic only for the services you need and deny incoming traffic for any unnecessary services. With UFW, you can control the incoming traffic to your system and keep your server secure.

If you have any questions or comments, feel free to leave them in the section below.

Additional Resources: