さくらVPSで(クライアント→VPN→外部のための)VPNトンネルつくったよ

ちょっと必要になったので。CentOS 6.4 x86_64です。

以下、ぜんぶrootで作業してるのでてきとうに

OpenVPNインストール
yum install lzo-devel
yum install openvpn
easy-rsaの使用

てきとうなディレクトリで

wget http://swupdate.openvpn.org/community/releases/openvpn-2.2.2.tar.gz #最新だとeasy-rsaがなさそうだったので
tar xvzf openvpn-2.2.2.tar.gz
cd openvpn-2.2.2
cp sample-config-files/server.conf /etc/openvpn/
cp -fr easy-rsa/2.0 /etc/openvpn/easy-rsa
鍵の作成・インストール。
cd /etc/openvpn/easy-rsa
cp openssl-1.0.0.cnf openssl.cnf # 0.9.6、0.9.8も用意されているので適切なバージョンのものを
./vars
./clean-all
./build-ca # 認証局を構築。パラメータはそれぞれ適切に
./build-key-server server # サーバ鍵を作成
./build-key client # クライアント用の鍵を作成
./build-key clientx # 必要な数分作成
./build-dh
cd keys
cp ca.crt dh1024.pem server.crt server.key ../..
サーバ設定
grep -E '^[a-z]' server.conf
port 1194
proto udp
dev tun
ca /etc/openvpn/ca.crt
cert /etc/openvpn/server.crt
key /etc/openvpn/server.key  # This file should be kept secret
dh /etc/openvpn/dh1024.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "redirect-gateway def1" # クライアント側のデフォルトゲートウェイ変更指示
push "dhcp-option DNS x.x.x.x" # 必要な場合。x.x.x.xはてきせつな値を
keepalive 10 120
comp-lzo
persist-key
persist-tun
status openvpn-status.log
verb 3
起動してみる
openvpn /etc/openvpn/server.conf
^C

なんとなく動いてそうなのでIPv4のフォワードの設定と、iptablesの設定

vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
sysctl -p
iptables -A INPUT -p udp --dport 1194 -j ACCEPT
iptables -A INPUT -i tun+ -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -i tun+ -j ACCEPT
iptables -A FORWARD -m state --state NEW -o eth0 -j ACCEPT
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state NEW -o eth0 -j ACCEPT
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
service openvpn start

サーバの設定終わり。クライアントではサーバで作成したCA証明書(ca.crt。共通)、クライアント証明書・鍵(client.crt、client.key。クライアントごとに別)をsftpとかでもってきて使えばいいです。Windowsだと、自動接続とかが必要ないならvpnux Connector Liteとかが楽なようすでした。