We could set multiple IP addressses on single interface, for example using NetworkManager:

multiple IP

How to make any connection to outside of this PC to use different IP?

for example if I have 8 IP addresses (10.7.4.x, 10.7.4.x+1, 10.7.4.x+2, ...), I want to connect each destination address using different IP, either using random IP or sequential mod (when destination IP mod 8 = 0, then use x, when destination mod 8 = 1, then use x+1, and so on)

  • 1
    Not related to your question, but I notice that the UI is a bit misleading in that it gives the impression you can specify a separate gateway for each IP address. I see that you have specified the same gateway on all the lines, and you should probably keep it that way.
    – kasperd
    Commented May 6, 2015 at 11:50

2 Answers 2


In Linux the selection of source addresses for outgoing connections can be controlled by the routing table:

ip route add via src

This is enough if you just need to use different source addresses for some fixed IP ranges. However, by combining the power of Linux netfilter (iptables) and policy routing (ip rule) you can get dynamic selection of the source address.

The basic procedure is as follows:

  1. Set the appropriate marks on the packets in the PREROUTING chain of the mangle table.
  2. Use different IP routing tables for packets with different marks (ip rule add fwmark X table Y).
  3. In each routing table use the required src address for packets.

The netfilter setup for marking packets according to the destination IP for the "mod 4" setup may look like this:

iptables -A PREROUTING  -t mangle -j CONNMARK --restore-mark
iptables -A PREROUTING  -t mangle -m mark --mark 0x0 -d \
  -j MARK --set-mark 1
iptables -A PREROUTING  -t mangle -m mark --mark 0x0 -d \
  -j MARK --set-mark 2
iptables -A PREROUTING  -t mangle -m mark --mark 0x0 -d \
  -j MARK --set-mark 3
iptables -A PREROUTING  -t mangle -m mark --mark 0x0 -d \
  -j MARK --set-mark 4
iptables -A POSTROUTING -t mangle -j CONNMARK --save-mark

(For this particular case you can omit two CONNMARK commands, because the other marking commands will give the same result for all packets in the same connection; however, for more complex cases, like the round-robin usage of source addresses, these commands are required to ensure that all packets in the connection will use the same route.)

The IP routing setup may then look like this:

ip route add default via src table 1
ip route add default via src table 2
ip route add default via src table 3
ip route add default via src table 4

ip rule add fwmark 1 pref 1 table 1
ip rule add fwmark 2 pref 2 table 2
ip rule add fwmark 3 pref 3 table 3
ip rule add fwmark 4 pref 4 table 4
  • should I flush my default rules first? ip route flush all, but when I do, when I call ip route again, the result was: RTNETLINK answers: no such process, I believe there are something that need to be done before adding the route, my log: pastie.org/10174898
    – Kokizzu
    Commented May 7, 2015 at 2:11

The application or tool with which you are making the outgoing connections must understand that there are multiple possible source IP addresses, and bind explicitly to whatever address is required.

E.g. ssh has this capability via the -b option; so if you do ssh -b ... it will use that address as the source address.

Automatically using the possible source addresss in e.g. a round-robin manner is not possible.

what is -b flag ?

-b bind_address
         Use bind_address on the local machine as the source address of
         the connection.  Only useful on systems with more than one

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .