Knowledge Base
Tutorials
Tutorials
  • How to install Joomla 4.2.2 on Ubuntu 20.04?
  • How to set and execute environment and shell variables in Linux?
  • How to employ Docker containers?
  • How to access PostgreSQL Image?
  • How to add and delete users on Ubuntu 20.04?
  • How to add swap space on Ubuntu 20.04?
  • How to back up, restore, and migrate a MongoDB database on Ubuntu 20.04?
  • How to configure additional SSH keys to your public cloud instance?
  • How to configure static IP on Ubuntu?
  • How to create a CLI with Python Fire on Ubuntu 22.04?
  • How to create a new user and manage permissions in MySQL?
  • How to create an instance with Terraform?
  • How to import and export a MongoDB database on Ubuntu 20.04?
  • How to install 7zip on Ubuntu 20.04?
  • How to install AIDE on Ubuntu 22.04?
  • How to install Anaconda on Ubuntu 20.04?
  • How to install and use Docker on Ubuntu 20.04?
  • How to install and use Elinks on Ubuntu 20.04?
  • How to install and use PostgreSQL on Ubuntu 20.04?
  • How to install Android Studio on Ubuntu 22.04?
  • How to install Angular on Ubuntu 20.04?
  • How to install Ansible on Ubuntu 20.04?
  • How to install Apache Kafka on Ubuntu 20.04?
  • How to install Apache Maven on Ubuntu 20.04?
  • How to install Asterisk on Ubuntu 20.04?
  • How to install Atom Text Editor on Ubuntu 20.04?
  • How to install Blender on Ubuntu?
  • How to install CFEngine3 on Ubuntu 20.04?
  • How to install Code::Blocks on Ubuntu 20.04?
  • How to install Composer on Ubuntu 22.04?
  • How to install Curl on Ubuntu 20.04?
  • How to install Desktop GUI on Ubuntu 20.04?
  • How to install Discord on Ubuntu 22.04?
  • How to install Django on Ubuntu 20.04?
  • How to install Docker Compose on Ubuntu 20.04?
  • How to install ELK on Debian 10 or Debian 11?
  • How to install Fail2ban on Debian 10?
  • How to install FFmpeg on Ubuntu 20.04?
  • How to install Flask on Ubuntu 20.04?
  • How to install Fish Shell in Ubuntu 20.04?
  • How to install Gedit on Ubuntu 20.04?
  • How to install Git on Ubuntu 20.04?
  • How to install Gitea on Ubuntu 20.04?
  • How to install Gitlab on Ubuntu 20.04?
  • How to install Go on Ubuntu 20.04?
  • How to install Google Chrome on Ubuntu 20.04?
  • How to install Gradle on Debian 10?
  • How to install Gradle on Ubuntu 20.04?
  • How to install Grafana on Ubuntu 20.04?
  • How to install Hadoop on Ubuntu 20.04?
  • How to install Homebrew on Linux?
  • How to install JAVA with APT on Ubuntu 20.04?
  • How to install Julia on Ubuntu 20.04?
  • How to install Jupyter on an Ubuntu Linux VM?
  • How to install Kdenlive on Ubuntu 20.04?
  • How to install Logwatch on Ubuntu 20.04?
  • How to install MariaDB on Ubuntu 22.04?
  • How to install Memcached on Debian 10?
  • How to install Microsoft Edge Browser on Ubuntu 22.04?
  • How to install MongoDB on CentOS 7?
  • How to install MongoDB on Linux?
  • How to install Mono on Debian 10?
  • How to Install Mono on Ubuntu 20.04?
  • How to install Mozilla Firefox on Ubuntu 20.04?
  • How to install MySQL on Ubuntu 22.04?
  • How to install Nagios on Ubuntu 20.04?
  • How to install Nginx on CentOS 8?
  • How to install Nginx on Ubuntu 22.04?
  • How to install Node.js on Ubuntu 22.04?
  • How to install NVIDIA CUDA on Ubuntu 20.04?
  • How to install Nvidia Optimus Driver on Ubuntu 22.04?
  • How to install OpenCV on Ubuntu 20.04?
  • How to install Perl on Ubuntu on 22.04?
  • How to install Pacman on Ubuntu 20.04?
  • How to install phpMyAdmin on Ubuntu 20.04?
  • How to install PIP on CentOS 8?
  • How to install Plex media server on Ubuntu 20.04?
  • How to install Podman on Ubuntu 20.04?
  • How to install Poetry on Ubuntu 22.04?
  • How to install Postman on Ubuntu 20.04?
  • How to install ProcessWire on Ubuntu 20.04?
  • How to install PyQuery package on Ubuntu 20.04?
  • How to install Python 3 on Ubuntu 22.04 and set up a programming environment?
  • How to install Python on Windows 10 using UI and Windows command prompt?
  • How to install R on Ubuntu 20.04?
  • How to install Ruby on Rails on Ubuntu 20.04?
  • How to install Ruby on Ubuntu 20.04 and setup programming environment?
  • How to install Rust on Ubuntu 22.04?
  • How to install Samba on Ubuntu 20.04?
  • How to install Skype on Ubuntu 20.04?
  • How to install Rancher on Ubuntu 20.04?
  • How to install Slack on Ubuntu 20.04?
  • How to install Springboot on Ubuntu 20.04?
  • How to install Spyder IDE in Ubuntu?
  • How to install Steam on Ubuntu 20.04?
  • How to install Strapi for Production on Ubuntu 20.04?
  • How to install TeamViewer on Ubuntu 20.04?
  • How to install TensorFlow on Ubuntu 20.04?
  • How to install Apache Web Server on Ubuntu 20.04?
  • How to install the Deno JavaScript Runtime on Ubuntu 20.04?
  • How to install Open LiteSpeed Web Server on Ubuntu 22.04?
  • How to install Sublime Text Editor on Ubuntu 20.04?
  • How to install tix package on Ubuntu 20.04?
  • How to install Vagrant on Ubuntu 20.04?
  • How to install Vim on Ubuntu 20.04?
  • How to install VirtualBox on Ubuntu 20.04?
  • How to install VLC Media Player on Ubuntu 20.04?
  • How to install VMware Workstation Player on Ubuntu 20.04?
  • How to install vscode on Ubuntu 20.04?
  • How to install Webmin on Ubuntu 20.04?
  • How to install Wine on Ubuntu 20.04?
  • How to install WordPress on Ubuntu 20.04?
  • How to install Xibo on Ubuntu 20.04?
  • How to install Zoom on Ubuntu 20.04?
  • How to install Yarn on Ubuntu 20.04?
  • How to keep Ubuntu 22.04 servers updated?
  • How to monitor system authentication logs on Ubuntu 20.04?
  • How to perform basic administration tasks for Storage Devices in Linux?
  • How to set up Jenkins on Ubuntu 20.04?
  • How to setup OpenVPN on Ubuntu 20.04?
  • How to setup time synchronization on Ubuntu 20.04?
  • How to setup a UFW on an Ubuntu or Debian Cloud Server?
  • How to setup SSH keys in Ubuntu 20.04?
  • How to SSH terminal through MobaXterm and PuTTY in Windows?
  • How to Use Ansible to Install and Set Up Docker on Ubuntu 20.04?
  • How to view system users on Ubuntu 20.04?
  • How to make OpenStack accessible through CLI?
  • How to make Block Storage available for Linux?
  • Script for automated incremental backups
  • How to configure a Git repository using Linux CLI?
  • How to enable remote access on MySQL?
  • How to Mount a S3 Bucket on Linux Instance?
  • How to access MySQL database credentials?
  • How to create an incremental backup using CLI?
  • How to set up web-based SQL server on OpenStack instance using SQL image?
Powered by GitBook
On this page
  • Overview
  • Prerequisites
  • Get Started
  • Conclusion

Was this helpful?

How to setup a UFW on an Ubuntu or Debian Cloud Server?

PreviousHow to setup time synchronization on Ubuntu 20.04?NextHow to setup SSH keys in Ubuntu 20.04?

Last updated 1 year ago

Was this helpful?

Overview

The best way to protect your server is by using a good security solution that has a lot of features and will make it hard for attackers to get in. Therefore, you must use a tool like Uncomplicated Firewall (UFW) so that you can block all types of traffic coming into your server. UFW is a firewall application that is mainly used to manage your firewall capabilities, which will help you with troubleshooting and maintaining your server's security. It also allows you to create custom rules, which will help you control traffic on your server.

This tutorial will demonstrate how to build up a UFW for Ubuntu 20.04 or Debian 11 Cloud Server.

Prerequisites

There are certain prerequisites that need to be met before you begin.

  • Ubuntu 20.04 or Debian 11 server configured with non-root sudo user privileges.

  • Stable internet connection.

Get Started

Step 1: Configure UFW with IPv6

Virtual Private Servers (VPS) are designed to provide a high level of performance and stability. However, the best way to protect them is by ensuring your firewall is open for both IPv4 and IPv6 connections. In case your VPS is designed for IPv6, check if it supports IPv6 by navigating to the firewall's configuration file using the following command.

sudo nano /etc/default/ufw

Make sure that IPv6 value is 'yes', save the file changes and exit.

Now, to restart the firewall, you need to disable it first:

sudo ufw disable

Output:

sudo ufw enable

Output:

Step 2: Define UFW default rules

Adding firewall rules for incoming and outgoing connections is a good practice to improve security and make your server more efficient. With the default UFW rules set in place, any external identity trying to reach your server will not be able to link to it. However, any internal application can connect externally. In a nutshell, these rules prevent incoming connections and allow outgoing connections.

To block all incoming connections, use the following command:

sudo ufw default deny incoming

To allow outgoing connections, use the following command:

sudo ufw default allow outgoing

Step 3: Open the Firewall to Connections

To allow connections to your server, you need to be able to communicate with it which requires changing the firewall rules. For instance, if your firewall is enabled, it would block all incoming connections. And, if you are using SSH to connect to your server, it would create an obstacle as you'd be locked out of the server. To avoid this issue, enable SSH connections to your server.

sudo ufw allow ssh

UFW allows you to make changes to your firewall by using the command which comes with some defaults such as ssh. It also lets you allow incoming connection to port 22/tcp instead of using the ssh command.

You can either allow incoming connections to port 22/tcp or the ssh command (as in the previous example).

$ sudo ufw allow 22/tcp

If you try and add this rule after you've already run ssh, you'll get the following output since the rule already exists:

In case your SSH server is configured and running on custom port 2222, you can allow connections using the following command. You can use the same syntax as above but substitute it with port 2222.

Note: Using the port number alone has an impact on both tcp and udp.

sudo ufw allow 2222/tcp

a. Ensure Web Server security

To secure your web server, SSH is considered as the primary solution. However, one another effective method to certify security is with File Transfer Protocol (FTP) access which allows you to access a server remotely.

For this, you need to authorize connections for port 80. This is useful if you have a web server application running such as Apache or Nginx that listens to connection requests over HTTP.

sudo ufw allow 80/tcp

b. Set Up Port Ranges

With UFW, you can allow or deny communication for a certain port or range of ports. To allow access to specific range of ports, use the following syntax. To accomplish this, you must specify the port at the low end of the range and the high end of the range, separated by a colon (:).

Note: It is necessary to mention the protocol (TCP or UDP).

The following command will authorize TCP or UDP access to ports ranging from 3000 to 4005.

sudo ufw allow 3000:4005/tcp
sudo ufw allow 3000:4005/udp

c. Set up IP Addresses

Access to certain IP addresses can be authorized within your firewall settings which means that you can allow connections from a specific IP address, for instance, 192.168.0.170 or 172.16.254.10. Make sure to substitute the IP address with the one you wish to set up.

sudo ufw allow from specific_server_ip_address

Step 4: Invalidating connections to specific ports

Denying access to a specific port can stop an external application to attack, or help you quickly restrict port usage. You can use the following command to restrict access to any port.

sudo ufw deny port_number/tcp

Step 5: Delete rules

If you wish to eliminate any rule, you can use the delete command and specify the rule after it.

sudo ufw delete allow port_number/tcp

If there are multiple rules, you can use the numbered list approach in which you can inspect the list of rules that are currently allowed.

sudo ufw status numbered

You can then mention the rule number to delete that specific rule.

sudo ufw delete number

The output diplays the deletion of rule number 23.

Step 6: Enable UFW

Now, that you have set up all the rules for your firewall, you can enable UFW so that the changes can propagate to your firewall.

sudo ufw enable

Output:

Verify your changes by checking the status that will display all the defined rules.

sudo ufw status

Output:

Now, to disable the firewall, run this command.

sudo ufw disable

Output:

Step 7: Restoring default server rules

If you wish to reset the rules to their default setting, run the following command. Press y if prompted to proceed with the reset.

sudo ufw reset

Conclusion

In this tutorial, we've delved into the concept of Uncomplicated Firewall. You can use it lock down or restrict access any inbound connection to your cloud server. Regardless of its uses, the commands are fairly simple and easy to understand if you follow the above steps as stated.

Turn back the firewall on:

The UFW firewall is successfully set up and configured to support IPv4 as well as IPv6. The next step is to set up some default connections rules for your firewall.