Sign up ×
Server Fault is a question and answer site for 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 to the internal IP 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             

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 -p tcp --dport 7999 -j DNAT -to-destination


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 port 7999 and get an established connection, but I cannot telnet to 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 7987 Trying Connected to Escape character is '^]'. root@insanity:~# iptables -t nat -A PREROUTING -d \ -p tcp --dport 7987 -j DNAT --to-destination root@insanity:~# telnet 7987 Trying 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


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.