Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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 ? – Rahul Patil Jan 12 '14 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 '14 at 21:25
up vote 2 down vote accepted

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

share|improve this answer
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 '14 at 22:17
The rule match your local eth0 ip. Try adding iif eth0, ie ip rule add from iif eth0 table masq – Jose Luis Martin Jan 12 '14 at 23:35
Perfect, that did the trick! – lgpasquale Jan 13 '14 at 20:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.