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'm using OpenVPN on a VPS which I want to forward incoming port 7999 from the external IP 198.23.248.150 to the internal IP 10.1.10.2 using iptables. I cannot seem to get the syntax correct and I have made sure forwarding is enabled:

root@insanity:~# cat /proc/sys/net/ipv4/conf/venet0/forwarding             
1

So if anyone could tell me the iptables command I'm looking to use I'd appreciate it so much. I'm losing my mind trying to figure it out.

share|improve this question

1 Answer 1

You just want to:

iptables -t nat -A PREROUTING -d 198.23.248.150 -p tcp --dport 7999 -j DNAT -to-destination 10.1.10.2

and

echo 1 > /proc/sys/net/ipv4/ip_forward

Also make sure that you've enabled forwarding persistent across reboots.

# Controls IP packet forwarding net.ipv4.ip_forward = 1

in /etc/sysctl.conf

And don't forget to check routing on VPS and on the remote box which has internal IP.

share|improve this answer
    
I'm not sure how you mean check routing on VPS and on remote box which has internal IP. But after doing the commands you said, I can telnet to 10.1.10.2 port 7999 and get an established connection, but I cannot telnet to 198.23.248.150 port 7999 and get a connection. –  Pei Mar 13 '13 at 0:09
    
How can you connect to internal IP from Internet?? –  slimsuperhero Mar 13 '13 at 5:07
    
It doesn't work: root@insanity:~# telnet 10.1.10.2 7987 Trying 10.1.10.2... Connected to 10.1.10.2. Escape character is '^]'. root@insanity:~# iptables -t nat -A PREROUTING -d 198.23.248.150 \ -p tcp --dport 7987 -j DNAT --to-destination 10.1.10.2 root@insanity:~# telnet 198.23.248.150 7987 Trying 198.23.248.150... telnet: Unable to connect to remote host: Connection refused –  user174217 May 17 '13 at 15:20
    
To have connections to the external IP from the machine itself work the DNAT rules also needs to be in the OUTPUT chain. –  mgorven May 17 '13 at 18:41

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.