11

I did a dumb thing, so bear with me.

While running a ufw command to add a rule, I did a Ctrl-C, this led to the problem of the following error being generated every time I try to use UFW:

ERROR: initcaps
[Errno 2] iptables: Chain already exists.

I have done a search and can't find anything on how to clean it up, but I did find this bug report at: ufw errors after ctr+c interupt

While I do see it has been confirmed, is there anything I can do to clean this up until it is fixed? Every time I try to add a rule I get that error.

Thanks in advance for any help provided.

Edit: BTW, I already tried saving the user.rules file, uninstalling UFW, reinstalling UFW, and moving the user.rules file back. I thought it might clean up iptables. No success.

2 Answers2

25

This worked for me from here

sudo ufw disable
sudo iptables -F
sudo iptables -X
sudo ip6tables -F
sudo ip6tables -X
sudo ufw enable

I hope it is helpful to someone, one day.

Mahsa2
  • 353
4

This is what I did to clean it up, if ufw is enabled, disable it. Then remove all of the ufw rules from iptables and ip6tables.

#! /usr/bin/env bash
set -e
set -o pipefail

iptables --flush
rules=($(iptables --list | grep Chain | grep -Eo "ufw-[a-z-]+" | xargs echo))
for i in "${rules[@]}"
do
  iptables --delete-chain $i
done

ip6tables --flush
rules6=($(ip6tables --list | grep Chain | grep -Eo "ufw6-[a-z-]+" | xargs echo))
for i in "${rules6[@]}"
do
  ip6tables --delete-chain $i
done
btobolaski
  • 82
  • 6
  • 6
    I encountered this same problem when I had a script adding/removing rules too fast. I simplified your iptables command a bit with sudo iptables --list | awk '/^Chain ufw-/ {print $2}' | xargs. BTW xargs default command is /bin/echo. I didn't really care about the other rules on my iptables so I just wiped it clear withsudo ufw disable; sudo iptables -F; sudo iptables -X; sudo ip6tables -F; sudo ip6tables -X; sudo ufw enable. There's also another more sensitive approach outlined here: http://blog.cloud66.com/ufw-shenanigans/ – Six May 08 '15 at 10:11