I am building a small office network with virtual machines. My schema is this:

  • Computer A: gateway, ip, iptables used for NAT [eth0=public internet dhcp, dhcp; eth1=gateway]
  • Computer B: client, ip, using gateway from Computer A.

NAT is working, and Computer B can access the internet using the A's gateway. I redirected some incoming ports from A to B (for instance, if A receives a request to port 80, it goes automatically to Computer B's Apache).

The thing is that I do not really understand how to open/close ports for Computer B from Computer A. I know how to close a port:

iptables -A INPUT -p tcp --dport 80 -j DROP

And it will refuse all incoming (not output) connections to port 80. However, this works for main interface eth0. I tried to, for instance, drop ingoing and outgoing connections for Computer B, port 80:

iptables -A FORWARD -i eth1 -o eth0 -p tcp --dport 80 -j DROP
iptables -A FORWARD -i eth0 -o eth1 -p tcp --dport 80 -j DROP

But it does not work. And I cannot figure out what I am doing wrong. Any clue?

  • The last lines of IPTABLES refer to IPTABLES on computer A or computer B? – Frankie Oct 29 '12 at 0:29

First line should be with sport (source port is 80). And also be careful about using -A. This append the rule to the chain (add the rule at the end). Rules in chains are evaluated from first to last, if one rule matches then the rest are not evaluated any more. So if you want to be sure use -I (insert at the beginning of chain):

iptables -I FORWARD -i eth1 -o eth0 -p tcp --sport 80 -j DROP
iptables -I FORWARD -i eth0 -o eth1 -p tcp --dport 80 -j DROP

I believe you were missing the 'OUTPUT' directive.

Please try this:

iptables -A OUTPUT -p tcp --dport 80 -j DROP

This will drop all outbound traffic on port 80.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.