How to Configure Two Gateways



How to Configure Two Gateways

Some networks may have multiple router/firewalls providing connectivity. Here's a typical scenario:

  • You have one router providing access to the Internet that you'd like to have as your default gateway (see the default gateway example earlier).

  • You also have another router providing access to your corporate network using addresses in the range 10.0.0.0 to 10.255.255.255. Let's assume that this router has an IP address of 192.168.1.254

The Linux box used in this example uses interface wlan0 for its Internet connectivity. You may be most likely using interface eth0, please adjust your steps accordingly.

There are a number of ways to add this new route.

Adding Routes from the Command Line

The route add command can be used to add new routes to your server. It has the advantage of being universal to all versions of Linux and is well documented in the man pages. In our example the reference to the 10.0.0.0 network has to be preceded with a -net switch, and the subnet mask and gateway values also have to be preceded by the netmask and gw switches, respectively:

     [root@bigboy tmp]# route add -net 10.0.0.0 netmask 255.0.0.0 \
       gw 192.168.1.254 wlan0

If you wanted to add a route to an individual server, the -host switch would be used with no netmask value. (The route command automatically knows the mask should be 255.255.255.255.) Here is an example for a route to host 10.0.0.1:

     [root@bigboy tmp]# route add -host 10.0.0.1 gw 192.168.1.254 wlan0

A universal way of making this change persistent after a reboot would be to place this route add command in the file /etc/rc.d/rc.local, which is always run at the end of the booting process.

Adding Routes with /etc/sysconfig/network-scripts/ Files

In Fedora Linux, permanent static routes are added on a per interface basis in files located in the /etc/sysconfig/network-scripts directory. The filename format is route-interface-name, so the filename for interface wlan0 would be route-wlan0.

The format of the file is quite intuitive with the target network coming in the first column followed by the word via and then the gateway's IP address. In our routing example, to set up a route to network 10.0.0.0 with a subnet mask of 255.0.0.0 (a mask with the first 8 bits set to 1) via the 192.168.1.254 gateway, we would have to configure file /etc/sysconfig/network-scripts/route-wlan0 to look like this:

     #
     # File /etc/sysconfig/network-scripts/route-wlan0
     #
     10.0.0.0/8 via 192.168.1.254

Note

The /etc/sysconfig/network-scripts/route-* filename is very important. Adding the wrong interface extension at the end will result in the routes not being added after the next reboot. There will also be no reported errors on the screen or any of the log files in the /var/log/ directory.


You can test the new file by running the /etc/sysconfig/network-scripts/ifup-routes command with the interface name as the sole argument. In the next example we check the routing table to see no routes to the 10.0.0.0 network and execute the ifup-routes command, which then adds the route:

     [root@bigboy tmp]# netstat -nr
     Kernel IP routing table
     Destination Gateway        Genmask       Flags MSS Window irtt Iface
     192.168.1.0 0.0.0.0        255.255.255.0 U     0   0      0    wlan0
     169.254.0.0 0.0.0.0        255.255.0.0   U     0   0      0    wlan0
     0.0.0.0     192.168.1.1    0.0.0.0       UG    0   0      0    wlan0
     [root@bigboy tmp]#

     [root@bigboy tmp]# ./ifup-routes wlan0

     [root@bigboy tmp]# netstat -nr
     Kernel IP routing table
     Destination Gateway        Genmask       Flags MSS Window irtt Iface
     192.168.1.0 0.0.0.0        255.255.255.0 U     0   0      0    wlan0
     169.254.0.0 0.0.0.0        255.255.0.0   U     0   0      0    wlan0
     10.0.0.0    192.168.1.254  255.0.0.0     UG    0   0      0    wlan0
     0.0.0.0     192.168.1.1    0.0.0.0       UG    0   0      0    wlan0
     [root@bigboy tmp]#