Generic load balancer for multiple WAN links

So you have two or more ADSL lines and want to use them all?
… or maybe you’re stealing your neighbor’s wifi and you have more than one network available?
… or you have cloned your SIM card and want to use multiple 3G connections simultaneously?

You can easily setup your Linux box to route multiple connections using iproute2, no matter how many WAN links you have! I have made a script to automate the process, it also comes with an optional failover watchdog which will monitor all the WAN links and automatically disable those which fail, re-enabling them when the connection is back.

Configuration is simple -one column for each WAN link-, you just need to have a separate interface for each link. If you want to use only one physical interface I recommend using a different VLAN on each WAN link. Edit the script to configure it as follows:

# Specify each WAN link in a separate column, example:
# In this example we have 3 wan links (vlanXXX interfaces) attached to a single
# physical interface because we use a vlan-enabled switch between the balancer
# machine and the ADSL routers we want to balance. The weight parameter should
# be kept to a low integer, in this case the ADSL line connected to vlan101 and
# vlan102 is 4Mbps and the ADSL line connected to vlan100 is 8Mbps (twice fast)
# so the WEIGHT value in vlan100 is 2 because it is two times faster.
# WANIFACE=\"    vlan101         vlan100         vlan102\"
# WEIGHTS=\"     1               2               1\"
# quick formula to calculate the weight: (LINKSPEED/MINSPEED)*NUM_LINKS
# If you don't want to use vlans, you should then use a separate physical
# interface for each link. IP aliasing on the same interface is not supported.

The script will set up the default route to be a multipath route, this will balance routes over the multiple WAN links and cache them depending on the destination address (so often used sites will always be sent over the same link). The weight parameters can be adjusted for each link in case you don’t have the same speed on each.


Enjoy! 😉

This entry was posted in linux and tagged , , , , , , , , . Bookmark the permalink.

13 Responses to Generic load balancer for multiple WAN links

  1. pof says:

    After using this for a while, I have found a small but annoying bug when using a multipath default route for load balancing over multiple links. Apparently, after each route cache flush traffic from existing connections is routed through a different interface (I noticed it because this causes some streaming music connections to cut after 10 minutes playing).

    As a workaround, you can set the sysctl value ‘net.ipv4.route.secret_interval=3600‘ (defaults to 600), and ‘net.ipv4.route.gc_elasticity=80‘ (defaults to 8). With these values, the error will only happen once an hour instead of once every 10 minutes, and the garbage collector will not prune routes which are long time on the cache, so it will be more difficult to trigger the problem.

  2. Jason says:

    I tried your script and I am having trouble getting it to work. I have two networks configured but it only appears to be working on one device. For example I can ping a site fine, try again and get nothing, ping another fine, then nothing.

    Both of the networks have the same gateway-, which I have a feeling may be causing the problem? It looks like when the routing tables are being created, the default gateway is specified by IP only. Should the device be specified here as well?? Any feedback would be great. Thanks. -Jason

    • pof says:

      You are right, the gateway can’t be the same. You must use different network addresses on each interface: if using in one network, you can use in the other, or if you prefer, you can do subnetting with 2 hosts using /30 netmasks for peer-to-peer networking.

      • Kwe says:

        How about if I have different gateways on the same network?

        • pof says:

          you need to have separate interfaces for each link, so won’t work if the gateways are on the same network (seen through the same interface). Either use VLANs on the same NIC, or use separate ethernet adapters one for each link.

  3. kosbir says:

    I ‘m a rookie in Linux networking and I would like to use this script to load-balance between an ADSL connection and a 3G/UMTS one. Which information from ifconfig regarding the two interfaces (eth0, ppp0) am I supposed to fill in order to get the script working? Any help highly appreciated!

    • pof says:

      read the script comments, it’s all in there, basically you have to fill in the interface name, gateway IP address, network address with netmask (in CIDR notation), and the weight you want to assign to each link.

  4. rajasekaran says:

    i have two internet connection one is 8Mbps another one 2Mbps and Centos6 and squid transparent proxy . how to through two internet connection at one lan port using Load balancing

  5. John Pierce says:

    This is the best looking script that I have fond. Question. Is there a way to create seperate vlans internally so you could avoid static wan interfaces.

  6. Error says:


    I’ve got one problem with generic balanser – ftp connections.
    My company has 2 WAN links but only one is a Symetric link. I would like to pass all my outgoing and incomming connections to external ftp serwers by eth2 interface where i plug my symetric link. How I Can do this? I have to write iptables rules?

    Additional, I woudl like to use generic balanser to route all ftp connections when eth2 link will fail.

  7. I have recently upgraded to Ubuntu 14.04 which has latest kernel 3.13.0-24.

    This script does not work properly. It opens some URLs and does not open some of them. Upon investigation I’ve found that the command
    “ip route show cache” always show empty result.

    Google has kind of confirmation that new kernel has some route cache problem.

    Please advise.


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>