With sudo iptables -t nat --line-numbers -L
you should see something like this:
Chain PREROUTING (policy ACCEPT)
num target prot opt source destination
1 REDIRECT tcp -- anywhere anywhere tcp dpt:http redir ports 3128
Chain INPUT (policy ACCEPT)
num target prot opt source destination
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
Chain POSTROUTING (policy ACCEPT)
num target prot opt source destination
Remove the PREROUTING
rule(s) affecting port 3128
:
sudo iptables -t nat --line-numbers -L | tac | \
awk '/redir ports 3128/ {system("sudo iptables -t nat -D PREROUTING "$1)}'
Now check the rules again:
% sudo iptables -t nat --line-numbers -L
Chain PREROUTING (policy ACCEPT)
num target prot opt source destination
Chain INPUT (policy ACCEPT)
num target prot opt source destination
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
Chain POSTROUTING (policy ACCEPT)
num target prot opt source destination
Explanation
iptables -vt nat -L
lists the needed rules
% sudo iptables -vt nat -L
Chain PREROUTING (policy ACCEPT 11 packets, 1957 bytes)
pkts bytes target prot opt in out source destination
0 0 REDIRECT tcp -- eth0 any anywhere anywhere tcp dpt:http redir ports 3128
Chain INPUT (policy ACCEPT 2 packets, 194 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 152 packets, 14386 bytes)
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 152 packets, 14386 bytes)
pkts bytes target prot opt in out source destination
And we need the rules for port 3128
in reverse order, if you have more than one, and the line-numbers
% sudo iptables -t nat --line-numbers -L | tac | awk '/redir ports 3128/'
1 REDIRECT tcp -- anywhere anywhere tcp dpt:http redir ports 3128
a little bit awk
magic deletes the rules line by line
awk '/redir ports 3128/ {system("sudo iptables -t nat -D PREROUTING "$1)}'