I am doing a network experiment about ip packet forwarding, but I don't know why it does work.

I have a linux machine with two network interfaces, eth0 and eth1 both with static IP address (eth0:, eth1:

My goal is simple, I just want to forward ip packets from eth1 with destination in subnet to eth0, and forward ip packets from eth0 with destination in subnet to eth1.

I turned on ip forwarding with:

sysctl -w net.ipv4.ip_forward=1

my routing table is like this:

# route -n
Kernel IP routing table
Destination     Gateway     Genmask        Flags Metric Ref   Use  Iface  U     0      0       0  eth0  U     0      0       0  eth1

But, when I try to ping from to, it does not work.


You need to add a forwarding rule using iptables command, something like this:

modprobe iptable_nat
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -A FORWARD -i eth1 -j ACCEPT

see man iptables for more details, or search internet for howto articles, for example How to set up a NAT router on a Linux-based computer

Here is Linux IP Masquerade HOWTO which discusses the topic in details.

You should also ensure that you have no other rules (e.g. in the FORWARD chain) that are overriding the above ACCEPT rule. If there are, you probably want to delete them.

| improve this answer | |
  • I don't know why, but it still don't work. I have added both iptables -A FORWARD -i eth0 -o eth1 -j ACCEPT and iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT to my tables. And my INPUT, OUTPUT, and FORWARD policy are all ACCEPT. – Yishu Fang Dec 10 '12 at 4:47
  • There are many things which can be configured wrong. For example, you host's IP should be specified as "gateway IP" on the hosts in the "internal" network, so they know that if an IP is not in the range of their "local" network the packets need to be sent to your gateway machine. This is similar to the usual setup where ADSL router is registered as gateway for hosts in the LAN – Sergey Dec 10 '12 at 9:29
  • When I use traceroute on my host, it shows that the packet goes to the "router" (, but it don't go any further. – Yishu Fang Dec 10 '12 at 10:56
  • 1
    @UniMouS: Seems that I've forgotten about MASQUERADE thing - I've edited my answer and added new links to articles. – Sergey Dec 10 '12 at 20:52

You need to add a route to both and

If your forwarding server has IPs and you would add in client

ip route via 192.1268.100.1

and in client

ip route via

(This works with just forwarding enabled, no iptables).

| improve this answer | |

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.