Home » Ubuntu » Securing a network with uncomplicated firewall in Ubuntu System

Securing a network with uncomplicated firewall in Ubuntu System

Last updated May 17, 2021

It is said that the best way to improve server security is to reduce the attack surface. Network communication in any system happens with the help of logical network ports, be it TCP ports or UDP ports. One part of the attack surface is the number of open ports that are waiting for connection to be established. It is always a good idea to block all unrequired ports. Any traffic coming to these ports can be filtered, that is, allowed or blocked with the help of a filtering system.

The Linux kernel provides a built-in packet filtering mechanism called netfilter, which is used to filter the traffic coming in or going out of the system. All modern Linux firewall systems use netfilter under the hood. Iptables is a well-known and popular user interface to set up and manage filtering rules for netfilter. It is a complete firewall solution that is highly configurable and highly flexible. However, iptables need effort on the user’s part to master the firewall setup. Various frontend tools have been developed to simplify the configuration of iptables. UFW is among the most popular frontend solutions to manage iptables.

Uncomplicated firewall (UFW) provides easy-to-use interface for people unfamiliar with firewall concepts. It provides a framework for managing netfilter as well as the command-line interface to manipulate the firewall. With its small command set and plain English parameters, UFW makes it quick and easy to understand and set up firewall rules. At the same time, you can use UFW to configure most of the rules possible with iptables. UFW comes preinstalled with all Ubuntu installations after version 8.04 LTS.

In this recipe, we will secure our Ubuntu server with the help of UFW and also look at some advance configurations possible with UFW.

Prerequisites

You will need an access to a root account or an account with root privileges.

Install and Configure UFW

Follow these steps to secure network with uncomplicated firewall:

  • UFW comes preinstalled on Ubuntu systems. If it’s not, you can install it with the following commands:
$ sudo apt-get udpate 
$ sudo apt-get install UFW
  • Check the status of UFW:
$ sudo ufw status
  • Add a new rule to allow SSH:
$ sudo ufw allow ssh
  • Alternatively, you can use a port number to open a particular port:
$ sudo ufw allow 22
  • Allow only TCP traffic over HTTP (port 80):
$ sudo ufw allow http/tcp
  • Deny incoming FTP traffic:
$ sudo ufw deny ftp
  • Check all added rules before starting the firewall:
$ sudo ufw show added
  • Now enable the firewall:
$ sudo ufw enable
  • Check the ufw status, the verbose parameter is optional:
$ sudo ufw status verbose
  • Get a numbered list of added rules:
$ sudo ufw status numbered
  • You can also allow all ports in a range by specifying a port range:
$ sudo ufw allow 1050:5000/tcp
  • If you want to open all ports for a particular IP address, use the following command:
$ sudo ufw allow from 10.0.2.100
  • Alternatively, you can allow an entire subnet, as follows:
$ sudo ufw allow from 10.0.2.0/24
  • You can also allow or deny a specific port for a given IP address:
$ sudo ufw allow from 10.0.2.100 to any port 2222 
$ sudo ufw deny from 10.0.2.100 to any port 5223
  • To specify a protocol in the preceding rule, use the following command:
$ sudo ufw deny from 10.0.2.100 proto tcp to any port 5223
  • Deleting rules:
$ sudo ufw delete allow ftp
  • Delete rules by specifying their numbers:
$ sudo ufw status numbered 
$ sudo ufw delete 2
  • Add a new rule at a specific number:
$ sudo ufw insert 1 allow 5222/tcp # Inserts a rule at number 1
  • If you want to reject outgoing FTP connections, you can use the following command:
$ sudo ufw reject out ftp
  • UFW also supports application profiles. To view all application profiles, use the following command:
$ sudo ufw app list
  • Get more information about the app profile using the following command:
$ sudo ufw app info OpenSSH
  • Allow the application profile as follows:
$ sudo ufw allow OpenSSH
  • Set ufw logging levels [off|low|medium|high|full] with the help of the following command:
$ sudo ufw logging medium
  • View firewall reports with the show parameter:
$ sudo ufw show added # list of rules added 
$ sudo ufw show raw # show complete firewall
  • Reset ufw to its default state (all rules will be backed up by UFW):
$ sudo ufw reset

UFW also provides various configuration files that can be used:

  • /etc/default/ufw: This is the main configuration file.

  • /etc/ufw/sysctl.conf: These are the kernel network variables. Variables in this file override variables in /etc/sysctl.conf.

  • /var/lib/ufw/user[6].rules or /lib/ufw/user[6].rules are the rules added via the ufw command.

  • /etc/ufw/before.init are the scripts to be run before the UFW initialization.

  • /etc/ufw/after.init are the scripts to be run after the UFW initialization.

Related Posts

How to Install CouchDB on Ubuntu 21.04 Linux Operating System

How to Install CouchDB on Ubuntu 21.04 Linux Operating System

The CouchDB is an open-source database system, managed by the Apache Software Foundation. It is fault-tolerant, and schema-free NoSQL database management system.   CouchDB store data in document or files with JSON data structure. Each document contains fields and...

How to add swap space on Ubuntu 21.04 Operating System

How to add swap space on Ubuntu 21.04 Operating System

The swap space is a unique space on the disk that is used by the system when Physical RAM is full. When a Linux machine runout the RAM it use swap space to move inactive pages from RAM. Swap space can be created into Linux system in two ways, one we can create a...

How to Install Nginx on Ubuntu 21.04 Server

How to Install Nginx on Ubuntu 21.04 Server

Nginx is the most potent, open-source, and a high-performance Web server. It can work as a reverse proxy server also, nowadays, is used by most of the most significant websites on the internet. People pronounced “engine x” for Nginx; it is the hot choice for every...

How to Install VirtualBox on Ubuntu 21.04 Linux

How to Install VirtualBox on Ubuntu 21.04 Linux

VirtualBox is an Open Source tool, known as a cross-platform virtualization application or software. It used to run multiple operating systems or virtual machines simultaneously on a single hardware. In this VirtualBox installation tutorial, we will explain the...

0 Comments

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

17 + six =

Follow Us

Our Communities

More on Ubuntu

The Ultimate Managed Hosting Platform
Load WordPress Sites in as fast as 37ms!