Routing network traffic through a particular NIC

So you have this box with two NICs. One (say, eth0) is connected to your local network (LAN), which has access to Internet. The other one, in this case wlan0, is connected directly to another router, which also has access to Internet.

Access to Internet via eth0 is slower, because the Internet connection is shared with other people on LAN. Access the other NIC (wlan0) would be faster, but you either do not know which of the interfaces is used for browsing Internet, or you in fact do know, that is is done through the first, the slowest, interface eth0.

You want to have connection to your peers on LAN and the intranet servers via the first NIC, eth0, but all connections to Internet shall go through the second NIC, wlan0.

Now. First check what is the current setup:

route

This will give you something like this:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.0.1     0.0.0.0         UG    0      0        0 eth0
link-local      *               255.255.0.0     U     1000   0        0 eth0
192.168.0.0     *               255.255.255.0   U     1      0        0 eth0
192.168.1.0     *               255.255.255.0   U     2      0        0 wlan0

In this case it says that the default gateway is eth0, which is not good. We want all the routing to destinations not explicitly indicated, which is this case are two segments of LAN, to go through the wlan0.

You may also go to a place like http://whatismyipaddress.com/ and write down your IP address as it appears to the other people on Internet.

Now issue commmand:

sudo route add default gw 192.168.1.1

And check the effect:

route

Now it says:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.1.1     0.0.0.0         UG    0      0        0 wlan0
default         192.168.0.1     0.0.0.0         UG    0      0        0 eth0
link-local      *               255.255.0.0     U     1000   0        0 eth0
192.168.0.0     *               255.255.255.0   U     1      0        0 eth0
192.168.1.0     *               255.255.255.0   U     2      0        0 wlan0

Voila! Now all the default traffic goes out through the wlan0.

Verify it by going again to, say, http://whatismyipaddress.com/ to see that your IP address now is different.

Enjoy!

P.S.

While this works in practice, I do not know what effect having two default gateways may have.

You may as well delete the unnecessary old default gateway like this:

sudo route del default gw 192.168.0.1

Although what effect that will have I am not sure either…

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s