Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have been using ipfilter in the past. Here is what I used there:

map-block tun0 192.168.1.0/24 -> 20.20.20.0/24

Some remote applications require that multiple connections all come from the same IP address. So, I want to tell iptables to have a static mapping between IP address and use the same IP address for a host (i.e. use some magic to choose a port). How do I do this?

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

Use the iptables SNAT target with the persistent option to keep the IP chosen for a specific client the same:

iptables --table nat --append POSTROUTING --out-interface tun0 --source 192.168.1.0/24 --jump SNAT --to-source 20.20.20.0/24 --persistent

share|improve this answer
add comment

You can do similar with netfilter:

iptables -t nat -A POSTROUTING -o tun0 -s 192.168.1.0/24 -j SNAT --to 20.20.20.0/24

You can read the iptables NAT howto here.

share|improve this answer
add comment

See iptables -j SNAT --to-source [ipaddr[-ipaddr]] --persistent

I don't know though "how static" this is. Maybe the mapping is lost on reboot. If that is a problem then you might configure a static mapping by an explicit rule for each address.

IIRC there is a tool (unfortunately I cannot remember the name) which can map source or destination addresses to chains so that you don't need a chain with 254 entries:

iptables ... -s 192.168.1.1 -j ...
iptables ... -s 192.168.1.2 -j ...
iptables ... -s 192.168.1.3 -j ...
share|improve this answer
add comment

Your Answer

 
discard

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.