Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13
Hi, I want to setup two IP addresses in a single NIC with two different gateways for the two IPs. Is this possible (with different gateways) ? Im using CentOS ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie amithad's Avatar
    Join Date
    Sep 2006
    Location
    Sri Lanka
    Posts
    160

    Smile Two IP Addresses with Two Gateways in Single NIC


    Hi,

    I want to setup two IP addresses in a single NIC with two different gateways for the two IPs. Is this possible (with different gateways) ?

    Im using CentOS release 6.3 (Final).

    Thanks

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    hmmm..i think that would work. at least i can run the commands to do it w/o them barfing, e.g.:

    # here's how i added a second ip addr for eth0
    Code:
    ip addr add 10.10.10.100/24 dev eth0
    # and here' how i added its gateway
    Code:
    route add default gw 10.10.10.1 dev eth0
    # now here's my ip addresses:
    Code:
    $ ip addr show dev eth0
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
        inet 192.168.2.15/24 brd 192.168.2.255 scope global eth0
        inet 10.10.10.100/24 scope global eth0
    #and my routing table:
    Code:
    $ route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         10.10.10.1      0.0.0.0         UG    0      0        0 eth0
    0.0.0.0         192.168.2.1     0.0.0.0         UG    0      0        0 eth0
    192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
    10.10.10.0      0.0.0.0         255.255.255.0   U     0      0        0 eth0
    not sure what would actually happen, mind you...

  3. #3
    Just Joined!
    Join Date
    Jun 2004
    Location
    North Hollywood, CA
    Posts
    22
    You could also setup a bridge and use virtual network cards with the bridge routing as many networks as you want. Look here: Features/Network Interface Management - FedoraProject for instructions.

  4. #4
    Just Joined!
    Join Date
    Mar 2003
    Location
    CT
    Posts
    11
    You'll want to set a metric, fro the two default routes, and/or use iproute2, or some policy based routing to determine which gateway the traffic should go out and also which gateway return traffic should go through.

  5. #5
    Linux Newbie nplusplus's Avatar
    Join Date
    Apr 2010
    Location
    Charlotte, NC, USA
    Posts
    106
    Hi, Amithad,

    As routing is done based on destination, with equal cost default routes, you will likely end up with an approximate round-robin of your traffic out of each gateway. If that is what you want, then you may be good to go with Atreyu's guidance. However, if you are looking to send some traffic out of one gateway and other traffic out of another gateway, then you are going to need to setup some sort of policy based routing that routes based on attributes you define. Maybe you could do that with IPTables, maybe with some Linux routing package (maybe Zebra or Quagga could do that, dunno, I haven't worked with either).

    But, notice how I said you may be good with Atreyu's guidance? In my experience, with IPv4, a system considers additional IP addresses on an interface to be "secondary." I am not sure if that is correct with Linux in general, perhaps with only certain kernel/packages/drivers, or maybe I'm just wrong. If I am correct, then you might find the host doesn't know how to find Atreyu's 10.10.10.1 gateway because it always selects the 192.168.2.0/24 network as its primary. If anyone definitively knows the answer to this, it would be really nice to know.

    Thank you,
    N

  6. #6
    Linux Newbie amithad's Avatar
    Join Date
    Sep 2006
    Location
    Sri Lanka
    Posts
    160
    Dear All,

    Thanks for your comments!

    dear Atreyu, did what you suggested. But when i execute the ping command to a different subnet where the new defaut gateway resides it does not working??

    Thanks

  7. #7
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    yeah, not surprised. you probably need to use iptables to tell the kernel to redirect packets based upon their destination. you might even be able to do it with a properly crafted route command, too.

  8. #8
    Just Joined!
    Join Date
    Feb 2009
    Posts
    14

    default GW and routing

    Hi,

    Default gateway can be only one as its parameter 'telling' the where to 'look' for addresses not in routing table.
    therefore putting 2 gateways is 'possible' technically but cannot be used.

    what i always do, is put additional routes to networks not known by my router (default gateway).

    for example:
    ip route add 192.168.1.0/24 dev eth1

    this will add route to 192.168.1.0/24 network that your router is not connected to.

    usually it happens when you have 2 or more network cards, and your second network card is connected to different switch.
    or if you dont have a router (very rare), or if your router has some problem and you want to quickly fix/troubleshoot the issue by adding the route manualy.

    the best is of course let your router manage all the routing tables dynamicly.

    routers do it best by using routing protocols such as RIP or OSPF.

    also, consider this:

    if you have 2 network cards with following IPs:
    NIC1 192.168.1.2/24 - DHCP
    NIC2 10.10.10.2/24 - static IP

    they routing table will have one default gateway - you will have to set it DHCP (example: 192.168.1.254)

    and second NIC, set statically to IP 10.10.10.2.

    this means that if for example you will try to reach any host in
    10.10.10.x/24 network, they computer will automatically send
    the traffic thru NIC2, while if you would like to reach
    for example google website (which is in the internet),
    it will automatically go thru default gateway.
    and thats becouse computer always looks for local 'attached'
    network connections first.
    therefore nowadays adding static routes is very rare and usualy used only in special circumstances such as troubleshooting or some really custom config..

  9. #9
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    yes, bflance, you are right on track w/what i was thinking about. i remember when i last did this successfully i used some form of "route add -net x.x.x.x dev eth1" to get to my 2nd network.

  10. #10
    Linux Newbie amithad's Avatar
    Join Date
    Sep 2006
    Location
    Sri Lanka
    Posts
    160
    Dear All,

    Without doing the task by one network card I installed another network card to my Linux box. My setup is as follows

    Code:
                       ________ 192.168.101.77(eth1)______________                  _______ 192.168.101.1<---------------------------> Internet
    Linux Box (Proxy) |________ 192.168.101.31(eth0) _____________-----------------|_______
                                                                                            192.168.101.30(VPN)<--------------------> 192.168.103.69
    My Linux box is acting as a proxy and users of the 192.168.103.0 subnet should be able to access the proxy server via the VPN.

    I entered the following commands in my Linux box but still I cant access the 192.168.103.0 subnet from it.

    Code:
    echo "1 proxy_gw" >> /etc/iproute2/rt_tables
    ip route add 192.168.101.0/24 dev eth1 src 192.168.101.77 table proxy_gw
    ip route add default via 192.168.101.30 dev eth1 table proxy_gw
    ip rule add from 192.168.101.77/24 table proxy_gw
    After adding the above commands tried to ping 192.168.103.69 host but it failed
    Attached Images Attached Images
    Last edited by amithad; 05-15-2013 at 05:57 AM.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •