I have openvpn working fine and it loads at startup as expected. However, without running the following commands on the server I cannot use it to forward the traffic from my client:

sudo iptables -t nat -A POSTROUTING -s -o eth0 -j MASQUERADE
echo '1' > /proc/sys/net/ipv4/ip_forward

The su is because sudo isn't sufficient (i.e. sudo echo '1' > /proc/sys/net/ipv4/ip_forward will fail so I must use su, log in as root and then run the command. If you know a better way to do this I'm all ears).

I want this to all be done automatically on boot (so after a reboot I don't have to log in via SSH, run the commands and log out again... I want to just launch my client and go) but do not know how to do this. I'm using Ubuntu 10.10 Server.

*FYI this is so I can use this (US based... in my living room) VPN from within China if (when) they block my normal aid VPN (which they do all too frequently).


You have some options (Actually, you have lots and lots of options, but here are a few):

1) Put those commands into /etc/rc.local (before the "exit 0" line). They will get run on boot.

2) Write an "openvpn-firewall" init script, have it run those commands, and make sure it runs prior to openvpn starting.

3) Use iptables-save and iptables-restore to manage your iptables commands, and put your ip_forward into /etc/sysctl.

a) iptables. Follow the instructions at the Ubuntu IPTables Wiki Page, particularly the "configuration on startup" section.

b) ip_forward. Edit /etc/sysctl.conf and find the line that looks like


And uncomment it so that it looks like this:


On reboot, this will run automatically.

| improve this answer | |

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.