2

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 May 6 '15 at 11:50
9

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

ip route add 10.11.12.0/24 via 10.7.4.1 src 10.7.4.200

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 0.0.0.0/0.0.0.3 \
  -j MARK --set-mark 1
iptables -A PREROUTING  -t mangle -m mark --mark 0x0 -d 0.0.0.1/0.0.0.3 \
  -j MARK --set-mark 2
iptables -A PREROUTING  -t mangle -m mark --mark 0x0 -d 0.0.0.2/0.0.0.3 \
  -j MARK --set-mark 3
iptables -A PREROUTING  -t mangle -m mark --mark 0x0 -d 0.0.0.3/0.0.0.3 \
  -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 10.7.4.1 src 10.7.4.200 table 1
ip route add default via 10.7.4.1 src 10.7.4.201 table 2
ip route add default via 10.7.4.1 src 10.7.4.202 table 3
ip route add default via 10.7.4.1 src 10.7.4.203 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 May 7 '15 at 2:11
4

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 10.7.4.201 ... 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
         address.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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