Thursday, March 6, 2014

How to add Two Gateway on same Linux machine

Q:I have one test server that has Two Interfaces. To one interface i have assigned ip 172.16.122.1 and GW 172.16.xxx.xxx
Now i connected cable to second interface and gave it ip 10.2.2.98.
Now they both on different subnets.
So how do I add the second gateway in /etc/sysconfig/network file. And when i will try to ping then through which interface will it ping the remote system.

A:Technically you can have multiple default gateways but that is the realm of core internet servers, network load balancers, and so on for the most part.

Much more common is a requirement for multiple routes, each with its own router.
Example: You have a computer sitting between two networks, like this: 192.168.1.5/24 and 192.168.2.5/24
You use 192.168.1.1 as Default Gateway - that is the default route for anything not locally attached. Locally attached is explicitly defined as anything which is in any of the subnets on which you have an interface in the UP/RUNNING state and for which there is no explicit static route defined.
In this configuration you already have three routes: A route for everything locally on 192.16.8.1.0/24, with the gateway being your own IP address on that subnet. You have a similar route for the systems on 192.168.2.0/24... And finally you have a default route, that being for "0.0.0.0" pointing to the default gateway. (There are also some other "psuedo" routes on some operating systems, eg for accessing the loopback device, or for broadcast traffic)
Now say there is a router at 192.168.2.2 which provides access to a number of networks. Usually these are "internal" networks. You can now add "static routes" to these networks. Example for a network with subnet 192.168.5.0/24
Linux Example:

route add -net 192.168.5.0 netmask 255.255.255.0 gw 192.168.2.2
 
You can add multiple "static" routes through one router, or routes can use as many routers/gateways as needed.
You can view the table of known routes with the command

netstat -rn
 
The Linux specific command will show the same information in a slightly different format.

ip route show
 
The router(s) specified must "know" how to reach the target network, otherwise it will go to its own default gateway and forward your packets that way.
To make this configuration changes persistent you need to add it to the system configuration files. Static routes are relevant to one specific interface so they do not go into /etc/sysconfig/network. In stead these go into files located in /etc/sysconfig/network-scripts/

For example: /etc/sysconfig/network-scripts/route-eth0
You add "via" entries into this file for routes. These lines read like "Access network X via gateway Y on Network interface device Z.
Example

default 192.168.1.1 dev eth0
192.168.5.0/24 via 192.168.2.2 dev eth0
192.168.6.0/24 via 192.168.2.2 dev eth0
 
And so on.

No comments:

How to use DiskSpd to simulate Veeam Backup & Replication disk actions

This HOW-TO contains information on how to use Microsoft© DiskSpd to simulate Veeam Backup & Replication disk actions to measure disk pe...