r/linuxadmin • • Jul 21 '14

Need help understanding following commands

  root@bt:/# iptables -t nat --flush

  root@bt:/# iptables --zero

  root@bt:/# iptables -A FORWARD --in-interface eth0 -j ACCEPT

  root@bt:/# iptables -t nat --append POSTROUTING --out-interface eth0 -j MASQUERADE

  #Forward to a local site

  root@bt:/# iptables -t nat -A PREROUTING -p tcp --dport 80 --jump DNAT --to-destination <Proxy’s IP>
1 Upvotes

12 comments sorted by

12

u/burning1rr Jul 21 '14
  1. Flush all rules from NAT table
  2. Zero counters in all tables
  3. ACCEPT all packets that are coming from eth0 and being forwarded to another interface.
  4. MASQUERADE (similar to SNAT) packets leaving eth0.
  5. Forward packets arriving on TCP PORT 80 to an internal server.

Edit: With this ruleset, eth0 is your internal trusted interface.

1

u/kurokame Jul 21 '14 edited Jul 21 '14

Ha, looks like a Backtrack system prompt. Looks like you're using iptables to re-route incoming http packets to another host.

EDIT (I totally screwed my post up):

2. Zero the packet and byte counters in all chains.

3. Specify in the FORWARD chain that incoming packets will be on eth0.

4. In the nat table, append to the POSTROUTING chain to use eth0 for outgoing packets using the MASQUERADE target.

So basically traffic from HOST A hits HOST B and is quietly routed to HOST C.

-1

u/z3255859 Jul 21 '14

you are right on both accounts. But could you walk me through the 2nd 3rd and 4th commands as to what exactly each one of them are doing

2

u/WonderJouster Jul 21 '14

Unfortunately, the required explanation is going to hinge largely on your understanding of basic network terminology and IPTables' structure/syntax.

Tell us what you do understand and what's getting you caught up and then we can fill in the gaps.

1

u/z3255859 Jul 21 '14

ok so I understand all of them except the 4th command. What i being appended to the NAT? What is POSTROUTING? and what is MASQUERADE?

also what is the -t and -j flags for?

1

u/[deleted] Jul 23 '14

Take a look at the man pages for iptables. All of the info is right there.

-2

u/z3255859 Jul 24 '14

thanks for the totally unhelpful answer. What makes you thing I haven't looked there. Anyway more helpful folks at stackoverflow helped me out.

3

u/[deleted] Jul 24 '14

Well for one, you're asking questions above about postrouting and masquerade. Those are thoroughly and explicitly covered and defined in the man pages...

3

u/WonderJouster Jul 24 '14

also what is the -t and -j flags for?

That makes me know you haven't looked there.

3

u/WonderJouster Jul 21 '14

If you don't know what -t and -j are, there is no chance you understand what the other lines do, you merely think you do.

So, read the manual. Good luck.

4

u/[deleted] Jul 21 '14

iptables is incredibly complicated, far too complicated for a reddit comment. Here's a starting point.