Linux – Policy-Based Routing Enables the Use of Multiple IP Default Gateways

Props to this site for the helpful info.

1. Configure your first static IP Address and Gateway information as normal inside of /etc/network/interfaces. If you have multiple static IP’s in the same subnet using the same gateway you should be fine to add them as sub-interfaces with no problems.

auto eth0
iface eth0 inet static
        address 1.1.1.1
        netmask 255.255.255.0
        network 1.1.1.0
        broadcast 1.1.1.255
        dns-nameservers 2.2.2.2 3.3.3.3
        dns-search yourdomain.com

        gateway 1.1.1.2

2. Configure your additional static IP Addresses but leave off the Gateway information inside of /etc/network/interfaces – Linux will squawk at this

auto eth1
iface eth1 inet static
        address 4.4.4.4
        netmask 255.255.255.0
        network 4.4.4.0
        broadcast 4.4.4.255
auto eth1:1
iface eth1:1 inet static
        address 5.5.5.5
        netmask 255.255.255.0
        network 5.5.5.0
        broadcast 5.5.5.255

3. Restart Networking
4. Create a routing policy number and name for each additional static IP/Gateway combo. I will create two for this example.

echo "1 ether1" >> /etc/iproute2/rt_tables
echo "2 ether1_1" >> /etc/iproute2/rt_tables

5. Add IP Routes for each of your Policies. Insert your Default Gateway after “default via”. You will want to add the entries in step numbers 5 and 6 to /etc/rc.local to retain the routes after a reboot.

ip route add 4.4.4.4/24 dev eth1 src 4.4.4.4 table ether1
ip route add default via 4.4.4.254 dev eth1 table ether1
ip route add 5.5.5.5/24 dev eth1:1 src 5.5.5.5 table ether1_1
ip route add default via 5.5.5.254 dev eth1:1 table ether1_1

6. Create To and From IP Rules for your entries

ip rule add from 4.4.4.4/32 table ether1
ip rule add to 4.4.4.4/32 table ether1
ip rule add from 5.5.5.5/32 table ether1
ip rule add to 5.5.5.5/32 table ether1

7. Useful commands for checking your routes and policies are:

netstat -anr
ip rule show
ip route show table ether1

Leave a Reply