How to view and delete firewall rules UFW

In this tutorial, we'll show you how to list and delete UFW firewall rules.

prerequisites

Users must be running UFW command sudo user.

UFW rules listed

One of the most common management tasks are listed in the firewall rules. You can check the status of UFW and lists all the rules:

sudo ufw status

If you have not enabled UFW, you will see the following:

Example Output
Status: inactive

Otherwise, if the UFW is active, the output will print a list of all active firewall rules:

Output Example

Status: active

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

For additional information, please use the status verbose:

sudo ufw status verbose

The output will contain information about logging, default information policy and the new configuration file:

Output Example

Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

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

With ID and order status numbered all active rules obtained. When you want to insert the new rule number or delete existing rules according to their number, which is very useful.

sudo ufw status numbered

Example Output Status: active

    To                        Action      From
    --                        ------      ----
[ 1] 22/tcp                    ALLOW IN    Anywhere
[ 2] 22/tcp (v6)                ALLOW IN    Anywhere (v6)

Delete UFW rules

There are two ways you can delete the UFW rules:

  • By rule number
  • Press Specifications

If you want to delete firewall rules via SSH, be sure not to delete the rule to allow SSH communications, so as not to lock yourself out of the remote server. By default, SSH listening on port 22.

Delete UFW rule by rule number is easier because you only need to find and type the number you want to delete the rule, rather than the full rules.

UFW rule by rule number deleted

To delete the UFW first rule number, you need to list the rules and find the number you want to delete the rule:

sudo ufw status numbered

This command will provide you with a list of all firewall rules, including the number of rules:

Output Example

Status: active

    To                        Action      From
    --                        ------      ----
[ 1] 22/tcp                    ALLOW IN    Anywhere
[ 2] 80/tcp                    ALLOW IN    Anywhere
[ 3] 443/tcp                    ALLOW IN    Anywhere
[ 4] 8069/tcp                  ALLOW IN    Anywhere

Knowing rule number, use ufw delete command followed by the number you want to delete the rule.

For example, to delete rule # 4, you can type:

sudo ufw delete 4

You will be prompted to confirm that you want to delete this rule:

Output Example

D

eleting:
 allow 22/tcp
Proceed with operation (y|n)? y

Type y, press Enter and delete rules:

Output Example Rule deleted

Each time a rule to delete, rule number will change. For safety reasons, please review the list of rules before deleting other rules and confirm.

UFW according to specifications delete rule

The second method is to use the delete rules ufw delete followed by the rule.

For example, if you add a rule to open port 2222, use the following command:

sudo ufw allow 2222

You can type the following command to delete the rule:

sudo ufw delete allow 2222

UFW reset and delete all the rules

Reset UFW disables the firewall and delete all active rules. When you want to restore all changes and start over, which is very useful.

To reset the UFW, type the following command:

sudo ufw reset

in conclusion

You have learned how to list and delete UFW firewall rules. When you configure a firewall, be sure to always allow only those incoming connections required for normal operation of the system.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159897.htm
ufw
ufw