I have a CentOS 7 box where there is tun0 interface. I want to forward traffic to a specific IP, originating from within the box, to go over the tun0.

I tried doing this

route add -host dev tun0

also tried adding

iptables -t nat -I POSTROUTING 1 -o tun0 -j MASQUERADE

Nothing seems to work.


What you're wanting is called policy routing (or rule-based routing), where the routing process looks at something other than the destination (in addition to the destination) for its decision-making. This can be done with ip using the rule command:

# echo '1 tunroute' >> /etc/iproute2/rt_tables
# ip rule add from table tunroute
# ip route add default via {tun0-address} dev tun0 table tunroute

You only need to run the first command once, as it establishes an alias; technically, you don't need it if you only want to use table numbers.

| improve this answer | |
  • Thanks for the suggestion. I was not aware of this. However, it is still not working or the traffic is not reaching the destination. When I do the tcpdump on the tun0 interface, I see the message being send to the interface, but no response. Not sure what is the issue – Sajan Jun 28 '18 at 21:30
  • 2
    Does the other side have its routes set up correctly? As well, are there any firewall rules preventing input traffic? – ErikF Jun 28 '18 at 21:34
  • 1
    Note that {tun0-address} is the "remote address" for tun0, and not its own address. – Ralph Rönnquist Jun 29 '18 at 6:46

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.