Ubuntu: Delete several ufw rules

How can several ufw rules be deleted at once? By using

$ ufw status numbered
Status: Aktiv Zu Aktion Von -- ------ --- [ 1] 80/tcp ALLOW IN Anywhere [ 2] 53 ALLOW IN Anywhere [ 3] 53 ALLOW OUT Anywhere (out) [ 4] 80/tcp (v6) ALLOW IN Anywhere (v6) [ 5] 53 (v6) ALLOW IN Anywhere (v6) [ 6] 53 (v6) ALLOW OUT Anywhere (v6) (out)

I get the numbers of the rules and can delete a single rule using e.g.

$ ufw delete 2

But is there a way to delete several rules at once? Something like

ufw delete 2 3 5 6

would be nice (but does not work).

2 Answers

I got three different ways how to delete multiple rules at once.

ufw delete

There is no feature to remove multiple numbers at once. But you can delete the ruls for v4 and v6 toghether:

ufw delete allow 53
ufw delete deny out 53

bash

But you can aso use bash to go though your numbers (be careful to go dnownwards, so the other numbers wont change):

for i in 6 5 3 2;do ufw delete $i;done

I like how you get asked for each number you'll get ased if you realy want to remove this rule.

no question asked

If you have many rules to delete you can use yes as a tool that gives you the answer you needed for all the interactive questions. But be careful, you won't get asked if something goes wrong

for i in 6 5 3 2;do yes|ufw delete $i;done

The accepted answer doesn't not work for me because ufw resets the numbers after a rule is deleted. I was lucky in that I wanted to delete the first n rows, so I adjusted the oneliner into the following

for n in {1..n}; do echo $n; yes | sudo ufw delete 1; done
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like