View Full Version : Two NIC's, two gateways....
quentin
9th May 2008, 18:12
Hi,
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,
Quentin
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,
Quentin
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 192.168.1.1 -> gw 192.168.1.2
eth0 192.168.2.1 -> gw 192.168.2.2
Okay now in your main routing table your default gw will be 192.168.1.2
okay now create the second routing table
ip ro add 192.168.2.0/24 dev eth1 proto kernel scope link src 192.168.1.1 table 4
ip ro add default 192.168.2.2 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.
Quentin
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,
Quentin
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.
http://lartc.org/howto/
http://www.linux-foundation.org/en/Net:Iproute2
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.