24

I need all requests to port 80 to be forwarded to 8020. I Googled it and I got:

iptables -t nat -I PREROUTING --source 0/0 --destination 0/0 -p tcp --dport 80 -j REDIRECT --to-ports 8020

Now in the future if I have to undo it what do I do (apart from restarting the system)?

PeterJ
  • 135
  • 4
  • 4
  • 15
raj
  • 371
  • 1
  • 4
  • 9

4 Answers4

28

I find it a pain to completely reconstruct the iptable rule when I want to delete it. Instaed I list the rules with line numbers and then delete by number. For example:

iptables -t nat -L --line-numbers

Gives output like:

Chain PREROUTING (policy ACCEPT)
num  target     prot opt source               destination         
1    REDIRECT   tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:80 redir ports 8020 

Then to delete by number:

iptables -t nat -D PREROUTING 1

Caveat: When you delete a line, all the lines below will get a new line number. For example, if you had rules like:

1 rule A
2 rule B
3 rule C

and you delete rule 2, then you get:

1 rule A
2 rule C
brainsik
  • 381
  • 2
  • 2
18

Just delete the rule:

iptables -t nat -D PREROUTING --source 0/0 --destination 0/0 -p tcp --dport 80 -j REDIRECT --to-ports 8020
basvdlei
  • 1,326
  • 8
  • 13
0

I am wondering if using vim would be an option

iptables-save > editme

vim editme # remove the rule you dont want

iptables-restore < editme
AlexanderN
  • 133
  • 5
0

You can also drop the entire port forwarding rules using this code

sudo iptables -P FORWARD DROP