Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have two gateway to access internet, somehow I want to load balancing it, so far its working, but some connection or service need persistent gateway IP to be used, so the client should never change its gateway once it have connected to dest., my current implementation seems to be round-robin or whatever it is.

this is my iproute

    nexthop via dev eth0 weight 1
    nexthop via dev eth0 weight 1

now i want to fix it somehow the gateway the client will use is predetermined, for example by using source port, if the source port is even number we use gw.1 and odd number go through gw.1, can we do that using ip route?

*note that I only have one outbound interface : eth0 here.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Use policy routing with marking packets. I'm not sure what format of configuration it is but you should check it in your distro.

In commandline it should look like (not tested but should work)

iptables -A PREROUTING -t mangle -p tcp --dport 22 --set-mark 0x1 -j CONNMARK
echo "200 ssh" >> /etc/iproute2/rt_tables
ip rule add fwmark 1 table ssh
ip route add default dev eth0 via table ssh

Edit: lines

echo "200 ssh" >> /etc/iproute2/rt_tables

Names routing table 200 by name "ssh". It is preserved between boots.

share|improve this answer
can you explain what does this line do : cat "200 ssh" >> /etc/iproute2/rt_tables ? –  uray Aug 25 '10 at 15:03
Explained in edit. /etc/iproute2/rt_tables is a file which names routing tables by user-readable names. –  Maciej Piechotka Aug 25 '10 at 16:56
In case someone is reading this 4.5 years later, here's my experience on Ubuntu 14.04: I had to do iptables -t mangle -A OUTPUT -p tcp --sport 22 -j MARK --set-mark 1 instead fo the CONNMARK thingy, plus this: sysctl net.ipv4.conf.eth1.rp_filter=2 –  tuomassalo May 6 at 13:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.