Two NIC's, two gateways....

Discussion in 'Technical' started by quentin, May 9, 2008.

  1. quentin

    quentin New Member

    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
     
  2. topdog

    topdog Active Member

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

    quentin New Member

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

    Thank you

    Best regards,

    Quentin
     
  4. topdog

    topdog Active Member

    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

    Code:
    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
    Code:
    ip ru add fwmark 2 lookup 4
    Then we need to mark the ssh packets
    Code:
    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
    Code:
    ip ro fl ca
     
  5. quentin

    quentin New Member

    Persistent

    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
     
  6. topdog

    topdog Active Member

    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.
     
  7. quentin

    quentin New Member

    Tables

    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
     
  8. topdog

    topdog Active Member

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

    Code:
    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
    Code:
    man ip
    And read these sites.
    http://lartc.org/howto/
    http://www.linux-foundation.org/en/Net:Iproute2
     

Share This Page