HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials

HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials (
-   Technical (
-   -   Two NIC's, two gateways.... (

quentin 9th May 2008 18:12

Two NIC's, two gateways....

Wonder if someone could help out with this one...

I've one server with two network-adapters. Both are connected to the internet, but are on completely different ranges and use, ofcourse, both a different gateway.

What I want to achieve is that all traffic, EXCEPT SSH (port 22) will go through NIC1. You can guess what NIC2 should do, because that one should only allow bidirectional SSH traffic. My problem is the gateway, I can't figure it out how to route it the right way.

Can someone help me out with this one?

Best regards,


topdog 9th May 2008 18:18

you need to setup a second routing table for the second nic then us a rule to route the ssh.

quentin 9th May 2008 18:20

Can you be a bit more specific? An example maybe?

Thank you

Best regards,


topdog 9th May 2008 18:26

For purposes of illustrating how this would work i will assume the following you need to substitute for your network

eth0 -> gw
eth0 -> gw

Okay now in your main routing table your default gw will be

okay now create the second routing table


ip ro add dev eth1  proto kernel  scope link  src table 4
ip ro add default table 4

The lets put a rule to make ssh traffic go via table 2

ip ru add fwmark 2 lookup 4
Then we need to mark the ssh packets

iptables -t mangle -I OUTPUT -p tcp --dport 22 -j MARK --set-mark 2
iptables -t mangle -I OUTPUT -p tcp --sport 22 -j MARK --set-mark 2

Then we flush the routing cache

ip ro fl ca

quentin 13th May 2008 11:31

Thank you for your reply!

But I've some additional questions:

- Are these settings persistent?
- If yes, is it somewhere stored in a configfile?

Thanks in advance.


topdog 13th May 2008 11:33

Those setting are not persistent as far as i know the only distro with support for that is Mandriva for other distros you will need to activate those setting via /etc/rc.local or for debian you could do it in using the /etc/network/interfaces file.

quentin 13th May 2008 11:39

Thank you.

Final question: Could you tell me a bit more about the different tables you described? I'm a bit confused how it works.

Best regards,


topdog 13th May 2008 11:46

By default you have one routing table which you can see by running the command


ip ro sh
Now because you want to do policy based routing you need a second table which i have named table 4 it can be any other number or you can make it a label by adding the mapping to /etc/iproute2/rt_tables.

We have the relevant routes to handle the traffic in this second table.

Then to force the traffic to use the table we use the fwmark rule with works hand in hand with iptables marking in the mangle table. There other ways to specify rules.

To get all the details on how advanced routing on linux works look at

man ip
And read these sites.

All times are GMT +2. The time now is 03:51.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.