Linux – the difference between OUTPUT and FORWARD chains in iptables

iptableslinux

CentOS 6.0

I'm studying iptables and am getting confused on the difference between FORWARD and OUTPUT chains. In my training documentation, it states:

If you're appending to (-A) or deleting from (-D) a chain, you'll want
to apply it to network data traveling in one of three directions:

  • INPUT – All incoming packets are checked against the rules in this chain.
  • OUTPUT – All outgoing packets are checked against the rules in this chain.
  • FORWARD – All packets being sent to another computer are checked against the rules in this chain.

This confuses me because, in my mind, packets leaving for a host WOULD be outgoing. So are there scenarios where a packet would be going to another computer but NOT be "outgoing"? How would iptables distinguish between the two?

Best Answer

OUTPUT is for packets that are emitted by the host. Their destination is usually another host, but can be the same host via the loopback interface, so not all packets that go through OUTPUT are in fact outgoing.

FORWARD is for packets that are neither emitted by the host nor directed to the host. They are the packets that the host is merely routing.

When you start digging into packet mangling and NAT, the full story is rather more complex.

Related Question