iptables forward all traffic to interface

I have two interfaces eth1 and eth0. I want all traffic on eth0to be forwarded to eth1. I created an iptable rule like this:

iptables -A FORWARD -s 0/0 -i eth0 -p tcp -o eth1 -j ACCEPT

But this doesn’t work. Is this the correct way of doing this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

If you haven’t already enabled forwarding in the kernel, do so.

  • Open /etc/sysctl.conf and uncomment net.ipv4.ip_forward = 1
  • Then execute $ sudo sysctl -p

Add the following rules to iptables

sudo iptables -t nat -A POSTROUTING --out-interface eth1 -j MASQUERADE  
sudo iptables -A FORWARD --in-interface eth0 -j ACCEPT

All of the forwarded traffic will traverse the FORWARD chain. To filter packets you’ll now have to create rules on that chain specifying which interface is incoming/outgoing instead of using the INPUT/OUTPUT chains.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x