• Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial News 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
{{postValue.id}}

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
{{postCtrl.cmtErrMsg}}

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.

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 David Lopez Guillen ?
Ayuda urgente instale SSL para servidor Opensuse y ahora no funciona tengo servicio web

hola segui este tutorial para tener un certificado ssl y ahora no se ve mi app en la red, espero alguien pueda ayudarme, tengo M9oodle en3.5 en un servidor open suse y ahora no funciona por favor ayuda.

https://www.linuxhelp.com/how-to-create-ssl-certificate-in-opensuse

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.