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

How To Set Up a Firewall with UFW on Ubuntu 21.04

  • 02:11 ufw allow 80/tcp
  • 02:29 ufw allow 3000:4000/tcp
  • 02:46 ufw allow 3000:4000/udp
  • 03:09 ufw allow from 192.168.6.115 to any port 22
  • 03:40 ufw allow from 192.168.7.0/23
  • 04:09 ufw deny out 25
  • 04:30 ufw status numbered
  • 04:48 ufw delete 6
  • 05:00 ufw status numbered
  • 05:19 ufw disable
  • 05:27 ufw status
  • 05:37 ufw reset
6834

To Set Up a Firewall with UFW on Ubuntu 21.04

Introduction:

UFW provides an interface for managing Netfilter firewalls, as well as Linux firewalls, and is aimed at providing an easy-to-use experience for users.

Installation Procedure:

Step 1:Check the OS version by using the below Command

root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 21.04
Release:	21.04
Codename:	hirsute

Step 2: Check the Status of the UFW by using the below command

root@linuxhelp:~# ufw status
Status: inactive

Step 3: Enable the UFW by using the below command

root@linuxhelp:~# ufw enable 
Firewall is active and enabled on system startup

Step 4: Check the IPv6 enabled in UFW by using the below command

root@linuxhelp:~# cat /etc/default/ufw | grep IPV6
IPV6=yes

Step 5: Make the default rule for incoming connections by using the below command

root@linuxhelp:~# ufw default deny incoming
Default incoming policy changed to 'deny'
(be sure to update your rules accordingly)

Step 6: Make the default rule for outgoing connections by using the below command

root@linuxhelp:~# ufw default allow outgoing 
Default outgoing policy changed to 'allow'
(be sure to update your rules accordingly)

Step 7: Allow the TCP port 80 connection by using the below command

root@linuxhelp:~# ufw allow 80/tcp
Rule added
Rule added (v6)

Step 8: Allow the port range from 3000 to 4000 TCP connection by using the below command

root@linuxhelp:~# ufw allow 3000:4000/tcp
Rule added
Rule added (v6)

Step 9: Allow the port range from 3000 to 4000 UDP connection by using the below command

root@linuxhelp:~# ufw allow 3000:4000/udp
Rule added
Rule added (v6)

Step 10: Allow the particular IP address to access the port 22 by using the below command

root@linuxhelp:~# ufw allow from 192.168.6.115 to any port 22
Rule added

Step 11: Allow the Subnet range of IP Address by using the below command

root@linuxhelp:~# ufw allow from 192.168.7.0/23
WARN: Rule changed after normalization
Rule added

Step 12: Deny the outgoing connections of port 25 by using the below command

root@linuxhelp:~# ufw deny out 25 
Rule added
Rule added (v6)

Step 13: List the Updated Rules in the UFW by using the below command

root@linuxhelp:~# ufw status numbered 
Status: active

     To                         Action      From
     --                         ------      ----
[ 1] 80/tcp                     ALLOW IN    Anywhere                  
[ 2] 3000:4000/tcp              ALLOW IN    Anywhere                  
[ 3] 3000:4000/udp              ALLOW IN    Anywhere                  
[ 4] 22                         ALLOW IN    192.168.6.115             
[ 5] Anywhere                   ALLOW IN    192.168.6.0/23            
[ 6] 25                         DENY OUT    Anywhere                   (out)
[ 7] 80/tcp (v6)                ALLOW IN    Anywhere (v6)             
[ 8] 3000:4000/tcp (v6)         ALLOW IN    Anywhere (v6)             
[ 9] 3000:4000/udp (v6)         ALLOW IN    Anywhere (v6)             
[10] 25 (v6)                    DENY OUT    Anywhere (v6)              (out)

Step 14: Delete the Rule number 6 by using the below command

root@linuxhelp:~# ufw delete 6
Deleting:
 deny out 25
Proceed with operation (y|n)? y
Rule deleted

Step 15: List the Updated Rules in the UFW by using the below command

root@linuxhelp:~# ufw status numbered
Status: active

     To                         Action      From
     --                         ------      ----
[ 1] 80/tcp                     ALLOW IN    Anywhere                  
[ 2] 3000:4000/tcp              ALLOW IN    Anywhere                  
[ 3] 3000:4000/udp              ALLOW IN    Anywhere                  
[ 4] 22                         ALLOW IN    192.168.6.115             
[ 5] Anywhere                   ALLOW IN    192.168.6.0/23            
[ 6] 80/tcp (v6)                ALLOW IN    Anywhere (v6)             
[ 7] 3000:4000/tcp (v6)         ALLOW IN    Anywhere (v6)             
[ 8] 3000:4000/udp (v6)         ALLOW IN    Anywhere (v6)             
[ 9] 25 (v6)                    DENY OUT    Anywhere (v6)              (out)

Step 16: Disable the firewall by using the below command

root@linuxhelp:~# ufw disable
Firewall stopped and disabled on system startup

Step 17: Check the Status of the UFW again by using the below command

 root@linuxhelp:~# ufw status 
Status: inactive

Step 18: Reset the UFW firewall to the default

root@linuxhelp:~# ufw reset
Resetting all rules to installed defaults. Proceed with operation (y|n)? y
Backing up 'user.rules' to '/etc/ufw/user.rules.20220127_140559'
Backing up 'before.rules' to '/etc/ufw/before.rules.20220127_140559'
Backing up 'after.rules' to '/etc/ufw/after.rules.20220127_140559'
Backing up 'user6.rules' to '/etc/ufw/user6.rules.20220127_140559'
Backing up 'before6.rules' to '/etc/ufw/before6.rules.20220127_140559'
Backing up 'after6.rules' to '/etc/ufw/after6.rules.20220127_140559'

By this To Set Up a Firewall with UFW on Ubuntu 21.04 have been completed

Tags:
michael
Author: 

Comments ( 0 )

No comments available

Add a comment

Frequently asked questions ( 5 )

Q

What happens when running the "ufw enable" command?

A

It will reload the firewall and enables the firewall to boot.

Q

What happens when running the "ufw reset" command?

A

It will Disable and reset the firewall to installation default

Q

Does UFW supports both ingress and egress filtering?

A

Yes, UFW supports both ingress and egress filtering and users may optionally specify a direction of either in or out for either incoming or outgoing traffic.

Q

Is UFW is a stand-alone Firewall?

A

No, UFW is not a stand-alone firewall rather than it uses IPtables for Configuration

Q

From what that UFW is written in?

A

UFW is written in Python.

Related Tutorials in How To Set Up a Firewall with UFW on Ubuntu 21.04

Related Tutorials in How To Set Up a Firewall with UFW on Ubuntu 21.04

How to install Meld tool in Ubuntu
How to install Meld tool in Ubuntu
Feb 25, 2017
How to install Dconf-Editor on Ubuntu 18.04
How to install Dconf-Editor on Ubuntu 18.04
Jul 14, 2018
How to install and update OpenSSL on Ubuntu 16.04
How to install and update OpenSSL on Ubuntu 16.04
Mar 9, 2017
How to install GLib 2.0 on Ubuntu 17.04
How to install GLib 2.0 on Ubuntu 17.04
May 22, 2017
How to Install Android Emulator on Ubuntu 20.4.1
How to Install Android Emulator on Ubuntu 20.4.1
Jul 13, 2021
How To Install AnyDesk on Ubuntu 16.04
How To Install AnyDesk on Ubuntu 16.04
Apr 4, 2018
How to install Genymotion 2.12.1 on Ubuntu 18.04
How to install Genymotion 2.12.1 on Ubuntu 18.04
Jul 9, 2018
How to install Timeshift 18.4 on Ubuntu 18.04
How to install Timeshift 18.4 on Ubuntu 18.04
Jul 6, 2018

Related Forums in How To Set Up a Firewall with UFW on Ubuntu 21.04

Related Forums in How To Set Up a Firewall with UFW on Ubuntu 21.04

Ubuntu
matthew class=
Failed to enable unit: Refusing to operate on linked unit file sshd.service
Apr 15, 2019
Ubuntu
mason class=
Passwd: You may not view or modify password information for root On Ubuntu 19.04
May 27, 2019
Ubuntu
isaac class=
/etc/apt/sources.list Permission denied
May 18, 2017
Firewall
kishore class=
How to check log for CSF firewall
Jan 2, 2018
Ubuntu
yousuf class=
lsb_release command not working : Debian
Jan 18, 2018
ifconfig command
jackbrookes class=
what is the location of the ifconfig program on your machine?
Jan 4, 2018
Ubuntu
mason class=
"E: Package 'php-mcrypt' has no installation candidate" error on Ubuntu 20.4.1
Mar 15, 2021
NFS
luke class=
clnt_create: RPC: Program not registered
Apr 25, 2017

Related News in How To Set Up a Firewall with UFW on Ubuntu 21.04

Related News in How To Set Up a Firewall with UFW on Ubuntu 21.04

How To Install Mixxx on Ubuntu 16.04
How To Install Mixxx on Ubuntu 16.04
Oct 11, 2017
Ubuntu 17.04 released with greater expectations
Ubuntu 17.04 released with greater expectations
Apr 15, 2017
Ubuntu Core now available on i.MX6 based TS-4900 thanks to Technologic Systems Inc.
Ubuntu Core now available on i.MX6 based TS-4900 thanks to Technologic Systems Inc.
Mar 1, 2017
Ubuntu 17.10 Artful Aardvark Beta 1 is now here. Download Now
Ubuntu 17.10 Artful Aardvark Beta 1 is now here. Download Now
Sep 2, 2017
Ubuntu Unity is no more: One Linux dream has been axed
Ubuntu Unity is no more: One Linux dream has been axed
Apr 7, 2017
What’s next for Ubuntu Linux Desktop?
What’s next for Ubuntu Linux Desktop?
Apr 11, 2017
Say Hi to Ubuntu's new mascot
Say Hi to Ubuntu's new mascot
Mar 22, 2019
KDE Connect App was removed from Google Play Store and brought back in 24 hours
KDE Connect App was removed from Google Play Store and brought back in 24 hours
Mar 22, 2019
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 Owen ?
How to add SSH key to my Gitlab account

I need to add the SSH key in my gitlab account. How to do so ????

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.