• Categories
    Category
  • Categories
    Category
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial Comments FAQ Related Articles

How to Install and Configure UFW in Debian/Ubuntu

1039

Steps to Install and Configure UFW - Uncomplicated FireWall in Debian/Ubuntu

The Uncomplicated Firewall is an user friendly and well comfortable for host based firewalls. It provides a framework for managing netfilter and also a command-line interface for controlling the firewall. Install and configure UFW Firewall will be discussed in Debian/Ubuntu.


To Install UFW Firewall

Reducing the complexity as how to set IPTables. If you are executing Ubuntu Linux, you can find UFW as a default firewall tool.


Uses of UFW

To check that, if UFW is installed in your system using below command.

user1@linuxhelp:~$ sudo dpkg --get-selections | grep ufw
[sudo] password for user1:
ufw            install

If not, then install it by using the below command.

user1@linuxhelp:~$ sudo apt-get install ufw


How to Enable and Disable ufw

Run the following command to enable ufw.

Execute below command, to disable and check the UFW is running or not.

user1@linuxhelp:~$ sudo ufw disable
user1@linuxhelp:~$ sudo ufw status
Status: inactive

If the status is inactive, that means its not active or disable.

user1@linuxhelp:~$ sudo ufw enable
Firewall is active and enabled on system startup
user1@linuxhelp:~$ sudo ufw status
Status: active

Listing the current ufw rules

To view the default rules, just type as like below.

user1@linuxhelp:~$ sudo ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip


Adding new rules in ufw

By default all incoming connections are denied. If you like to remote your system then you need to permit proper port. For Example, you need to allow ssh connection. Execute the below command.

Allow access:

user1@linuxhelp:~$ sudo ufw allow ssh
Rule added
Rule added (v6)

If you want to check the status again, you can view an output like this.

user1@linuxhelp:~$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
22 (v6)                    ALLOW       Anywhere (v6)

The first rule explains the incoming connection to the " port 22" from " Anywhere" , here both UDP and TCP packets will be allowed. Then include the parameter tcp after the port number.

user1@linuxhelp:~$ sudo ufw allow ssh/tcp
Rule added
Rule added (v6)
user1@linuxhelp:~$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
22/tcp                     ALLOW       Anywhere
22 (v6)                    ALLOW       Anywhere (v6)
22/tcp (v6)                ALLOW       Anywhere (v6)


Deny Access

To deny ftp rule, just execute below command.

user1@linuxhelp:~$ sudo ufw deny ftp
Rule added
Rule added (v6)
user1@linuxhelp:~$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
22/tcp                     ALLOW       Anywhere
21/tcp                     DENY        Anywhere
22 (v6)                    ALLOW       Anywhere (v6)
22/tcp (v6)                ALLOW       Anywhere (v6)
21/tcp (v6)                DENY        Anywhere (v6)


To add Specific Port

To change the ssh port on our system from 22 into 2287. Then to permit port 2287, we will include it like below.

user1@linuxhelp:~$ sudo ufw allow 2287
Rule added
Rule added (v6)
user1@linuxhelp:~$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
22/tcp                     ALLOW       Anywhere
21/tcp                     DENY        Anywhere
2290                       ALLOW       Anywhere
2287                       ALLOW       Anywhere
22 (v6)                    ALLOW       Anywhere (v6)
22/tcp (v6)                ALLOW       Anywhere (v6)
21/tcp (v6)                DENY        Anywhere (v6)
2290 (v6)                  ALLOW       Anywhere (v6)
2287 (v6)                  ALLOW       Anywhere (v6)

To add port range into the rule, you have to open port from 2286 - 2357 using TCP protocol, then the script will be like below.

user1@linuxhelp:~$ sudo ufw allow 2286:2357/tcp
Rule added
Rule added (v6)
user1@linuxhelp:~$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
22/tcp                     ALLOW       Anywhere
21/tcp                     DENY        Anywhere
2290                       ALLOW       Anywhere
2287                       ALLOW       Anywhere
2286:2357/tcp              ALLOW       Anywhere
22 (v6)                    ALLOW       Anywhere (v6)
22/tcp (v6)                ALLOW       Anywhere (v6)
21/tcp (v6)                DENY        Anywhere (v6)
2290 (v6)                  ALLOW       Anywhere (v6)
2287 (v6)                  ALLOW       Anywhere (v6)
2286:2357/tcp (v6)         ALLOW       Anywhere (v6)

If you like to use UDP, simply execute the below command.

user1@linuxhelp:~$ sudo ufw allow 2286:2357/udp
Rule added
Rule added (v6)
user1@linuxhelp:~$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
22/tcp                     ALLOW       Anywhere
21/tcp                     DENY        Anywhere
2290                       ALLOW       Anywhere
2287                       ALLOW       Anywhere
2286:2357/tcp              ALLOW       Anywhere
2286:2357/udp              ALLOW       Anywhere
22 (v6)                    ALLOW       Anywhere (v6)
22/tcp (v6)                ALLOW       Anywhere (v6)
21/tcp (v6)                DENY        Anywhere (v6)
2290 (v6)                  ALLOW       Anywhere (v6)
2287 (v6)                  ALLOW       Anywhere (v6)
2286:2357/tcp (v6)         ALLOW       Anywhere (v6)
2286:2357/udp (v6)         ALLOW       Anywhere (v6)

Don' t forgot to put " TCP" or " UDP" explicitly or else you will receive an error message as like below.

ERROR:Must specify tcp or udp with multiple ports


To Add Specific IP

ufw also permits you to include rules based on IP.

user1@linuxhelp:~$ sudo ufw allow from 192.168.5.103
Rule added
user1@linuxhelp:~$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
22/tcp                     ALLOW       Anywhere
21/tcp                     DENY        Anywhere
2290                       ALLOW       Anywhere
2287                       ALLOW       Anywhere
2286:2357/tcp              ALLOW       Anywhere
2286:2357/udp              ALLOW       Anywhere
Anywhere                   ALLOW       192.168.5.103
22 (v6)                    ALLOW       Anywhere (v6)
22/tcp (v6)                ALLOW       Anywhere (v6)
21/tcp (v6)                DENY        Anywhere (v6)
2290 (v6)                  ALLOW       Anywhere (v6)
2287 (v6)                  ALLOW       Anywhere (v6)
2286:2357/tcp (v6)         ALLOW       Anywhere (v6)
2286:2357/udp (v6)         ALLOW       Anywhere (v6)

To use a subnet mask to extent the ranges

user1@linuxhelp:~$ sudo ufw allow from 192.168.5.0/24
Rule added
user1@linuxhelp:~$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
22/tcp                     ALLOW       Anywhere
21/tcp                     DENY        Anywhere
2290                       ALLOW       Anywhere
2287                       ALLOW       Anywhere
2286:2357/tcp              ALLOW       Anywhere
2286:2357/udp              ALLOW       Anywhere
Anywhere                   ALLOW       192.168.5.103
Anywhere                   ALLOW       192.168.5.0/24
22 (v6)                    ALLOW       Anywhere (v6)
22/tcp (v6)                ALLOW       Anywhere (v6)
21/tcp (v6)                DENY        Anywhere (v6)
2290 (v6)                  ALLOW       Anywhere (v6)
2287 (v6)                  ALLOW       Anywhere (v6)
2286:2357/tcp (v6)         ALLOW       Anywhere (v6)
2286:2357/udp (v6)         ALLOW       Anywhere (v6)


To Combine Parameters

For more rules, you will combine IP Address, protocol and port. We need to create rule that limit the connection only from IP 192.168.5.196, only protocol TCP and to port 22.

user1@linuxhelp:~$ sudo ufw allow from 192.168.5.196 proto tcp to any port 22
Rule added
user1@linuxhelp:~$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
22/tcp                     ALLOW       Anywhere
21/tcp                     DENY        Anywhere
2290                       ALLOW       Anywhere
2287                       ALLOW       Anywhere
2286:2357/tcp              ALLOW       Anywhere
2286:2357/udp              ALLOW       Anywhere
Anywhere                   ALLOW       192.168.5.103
Anywhere                   ALLOW       192.168.5.0/24
22/tcp                     ALLOW       192.168.5.196
22 (v6)                    ALLOW       Anywhere (v6)
22/tcp (v6)                ALLOW       Anywhere (v6)
21/tcp (v6)                DENY        Anywhere (v6)
2290 (v6)                  ALLOW       Anywhere (v6)
2287 (v6)                  ALLOW       Anywhere (v6)
2286:2357/tcp (v6)         ALLOW       Anywhere (v6)
2286:2357/udp (v6)         ALLOW       Anywhere (v6)


To Delete Rules

There are two methods for deleting rules.

Method 1:

To delete rules the match service ftp. So the 21/tcp mean ftp port will be deleted.

user1@linuxhelp:~$ sudo ufw delete deny ftp
Rule deleted
Rule deleted (v6)
user1@linuxhelp:~$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
22/tcp                     ALLOW       Anywhere
2290                       ALLOW       Anywhere
2287                       ALLOW       Anywhere
2286:2357/tcp              ALLOW       Anywhere
2286:2357/udp              ALLOW       Anywhere
Anywhere                   ALLOW       192.168.5.103
Anywhere                   ALLOW       192.168.5.0/24
22/tcp                     ALLOW       192.168.5.196
22 (v6)                    ALLOW       Anywhere (v6)
22/tcp (v6)                ALLOW       Anywhere (v6)
2290 (v6)                  ALLOW       Anywhere (v6)
2287 (v6)                  ALLOW       Anywhere (v6)
2286:2357/tcp (v6)         ALLOW       Anywhere (v6)
2286:2357/udp (v6)         ALLOW       Anywhere (v6)

It will delete the rule that matches service ftp

Method 2:

Using below command, to delete the 1st rule in the above example.

To show the number of rule to denote which rule that you have to delete. Execute the below command.

user1@linuxhelp:~$ sudo ufw status numbered
Status: active

     To                         Action      From
     --                         ------      ----
[ 1] 22                         ALLOW IN    Anywhere
[ 2] 22/tcp                     ALLOW IN    Anywhere
[ 3] 2290                       ALLOW IN    Anywhere
[ 4] 2287                       ALLOW IN    Anywhere
[ 5] 2286:2357/tcp              ALLOW IN    Anywhere
[ 6] 2286:2357/udp              ALLOW IN    Anywhere
[ 7] Anywhere                   ALLOW IN    192.168.5.103
[ 8] Anywhere                   ALLOW IN    192.168.5.0/24
[ 9] 22/tcp                     ALLOW IN    192.168.5.196
[10] 22 (v6)                    ALLOW IN    Anywhere (v6)
[11] 22/tcp (v6)                ALLOW IN    Anywhere (v6)
[12] 2290 (v6)                  ALLOW IN    Anywhere (v6)
[13] 2287 (v6)                  ALLOW IN    Anywhere (v6)
[14] 2286:2357/tcp (v6)         ALLOW IN    Anywhere (v6)
[15] 2286:2357/udp (v6)         ALLOW IN    Anywhere (v6)

Here you will delete the first rule. Select " y" , this will permanently delete the rule.

user1@linuxhelp:~$ sudo ufw delete 1
Deleting:
 allow 22
Proceed with operation (y|n)? y
Rule deleted
user1@linuxhelp:~$ sudo ufw status numbered
Status: active

     To                         Action      From
     --                         ------      ----
[ 1] 22/tcp                     ALLOW IN    Anywhere
[ 2] 2290                       ALLOW IN    Anywhere
[ 3] 2287                       ALLOW IN    Anywhere
[ 4] 2286:2357/tcp              ALLOW IN    Anywhere
[ 5] 2286:2357/udp              ALLOW IN    Anywhere
[ 6] Anywhere                   ALLOW IN    192.168.5.103
[ 7] Anywhere                   ALLOW IN    192.168.5.0/24
[ 8] 22/tcp                     ALLOW IN    192.168.5.196
[ 9] 22 (v6)                    ALLOW IN    Anywhere (v6)
[10] 22/tcp (v6)                ALLOW IN    Anywhere (v6)
[11] 2290 (v6)                  ALLOW IN    Anywhere (v6)
[12] 2287 (v6)                  ALLOW IN    Anywhere (v6)
[13] 2286:2357/tcp (v6)         ALLOW IN    Anywhere (v6)
[14] 2286:2357/udp (v6)         ALLOW IN    Anywhere (v6)


To Reset Rules

To reset all the rules, run the following command.

user1@linuxhelp:~$ sudo ufw reset
Resetting all rules to installed defaults. Proceed with operation (y|n)? y
Backing up ' after.rules'  to ' /etc/ufw/after.rules.20160423_105956' 
Backing up ' after6.rules'  to ' /etc/ufw/after6.rules.20160423_105956' 
Backing up ' user6.rules'  to ' /lib/ufw/user6.rules.20160423_105956' 
Backing up ' user.rules'  to ' /lib/ufw/user.rules.20160423_105956' 
Backing up ' before.rules'  to ' /etc/ufw/before.rules.20160423_105956' 
Backing up ' before6.rules'  to ' /etc/ufw/before6.rules.20160423_105956' 

On selecting " y" , then ufw will backup all existing rules before reset. Once the rules is Reset the firewall will be disabled. You can enable it again if you like to use this.

Tags:
michael
Author: 

Comments ( 0 )

No comments available

Add a comment

Frequently asked questions ( 5 )

Q

After installing also my 80 port is not accessible

A

Try UFW allow 80

Q

I have bookmarked this tutorial!! Really good

A

Thank you stay connected

Q

I'm getting dpkg error status while installing the package?

A

remove the dpkg lock file and proceed

Q

After installation also it is not working? Help me

A

Check the status of the daemon whether it is in running state or not

Q

After installing ufw also my 80 port is not accessible ?

A

you have to allow it in ufw using following command



# UFW allow 80

Related Tutorials in How to Install and Configure UFW in Debian/Ubuntu

Related Tutorials in How to Install and Configure UFW in Debian/Ubuntu

How to install Gparted on Debian 9.0
How to install Gparted on Debian 9.0
Sep 13, 2017
Installation SSL Certificate on Ubuntu/Linuxmint/Debian to Secure Apache
Installation SSL Certificate on Ubuntu/Linuxmint/Debian to Secure Apache
Sep 19, 2018
How to install and update OpenSSL on Debian 11.3
How to install and update OpenSSL on Debian 11.3
Oct 21, 2022
How to install qBittorrent on Debian 9.0
How to install qBittorrent on Debian 9.0
Sep 8, 2017
How to Install FileZilla in Debian
How to Install FileZilla in Debian
Nov 29, 2016
How to install Nmap on Debian 9.0
How to install Nmap on Debian 9.0
Sep 9, 2017
How to Install Laravel in Debian
How to Install Laravel in Debian
Dec 7, 2016
How to install Wireshark in Debian
How to install Wireshark in Debian
Mar 18, 2017

Related Forums in How to Install and Configure UFW in Debian/Ubuntu

Related Forums in How to Install and Configure UFW in Debian/Ubuntu

MariaDB
ryan class=
E: Unable to locate package mariadb-server
Sep 18, 2017
Linux
AadrikaAnshu class=
How to add timestamps to history On Any Linux Machine
Jun 18, 2019
vim
jacob class=
Change true vi-compatible editor from Debian Vim-compatible
Nov 8, 2021
debian
anandaamatya class=
RedNotebook on Debian buster
Jun 30, 2020
Ubuntu
Kirin class=
Videos Always Micro-Shutter
Mar 8, 2019
debian
luke class=
workbench for debian
May 26, 2017
debian
teddybarax class=
Softwae installation problem
Aug 24, 2020
debian
colbyvance class=
Trouble Installing Debian 10.8.0 (Gnome)
Feb 28, 2021

Related News in How to Install and Configure UFW in Debian/Ubuntu

Related News in How to Install and Configure UFW in Debian/Ubuntu

Debian IceDove kicks the bucket after Thunderbird revisits Debian Repositories
Debian IceDove kicks the bucket after Thunderbird revisits Debian Repositories
Feb 28, 2017
Mass update of Jessie - A better alternative to new version?
Mass update of Jessie - A better alternative to new version?
May 8, 2017
Debian 9.2 ‘Stretch’ OS is here, download distro now
Debian 9.2 ‘Stretch’ OS is here, download distro now
Oct 9, 2017
Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Ganesh Konka ?
Zentya 6.1 http proxy configuration

please send link for creating zentyal 6.1 for http proxy and firewall as gateway.

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.