I have a computer with two network devices (eth0 and wlan0), both connected to the internet (two different connections/isp). I'm trying to share the connection of wlan0 to another computer connected via ethernet to eth0.

What I'm doing is:

# sysctl net.ipv4.ip_forward=1
# iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE

From the client computer I can then connect to this one, but the internet connection that gets shared is the one on eth0 and not the one on wlan0.

If I disable the internet connection on eth0 (by setting no gateway), then the connection to be shared is the one on wlan0. However, I'd like to have both internet connections enabled and specify to iptables which one to share. Is this possible?

What am I missing? Do I need some forwarding rule?

  • what is output of sysctl -a | grep net.ipv4.ip_forward, is FORWARD Chain is allowed ? could you please add your iptables rules. and what is client side gateway ? Jan 12, 2014 at 19:35
  • the output is net.ipv4.ip_forward = 1. Forward chain is allowed and there are no other rules beside the one I posted above. The client side gateway is the local address of the computer that should share the connection. Note that sharing the connection is working, it's just not sharing the one I want.
    – lgpasquale
    Jan 12, 2014 at 21:25

1 Answer 1


Seem like your default gw is on eth0 and client is redirected to it (via a icmp redirect).

To fix your setup you need to add a routing rule stating that all packets incoming from client_ip should be routed to wlanO_gw.

Try adding a new routing table:

  • Edit /etc/iproute2/rt_tables and add a line for a new table, for example 252 masq where 252 is the table id and masq is the new table name.

  • Add a rule to route ip_client packets with table masq

    ip rule add from ip_client/32 table masq

  • add a default gw to the masq table

    ip ro add default via wlan0_gw table masq

  • This is almost exactly what I was looking for. This works perfectly if I use the exact commands you posted, however if I try changing the first one to ip rule add from ip table masq (instead of in order to allow routing of packets from all the addresses of the subnet connected to eth0, it stops working. Is there a way to do that?
    – lgpasquale
    Jan 12, 2014 at 22:17
  • 1
    The rule match your local eth0 ip. Try adding iif eth0, ie ip rule add from iif eth0 table masq Jan 12, 2014 at 23:35
  • Perfect, that did the trick!
    – lgpasquale
    Jan 13, 2014 at 20:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .