LoginSignup
35
39

More than 5 years have passed since last update.

OpenVPNを使ってVPNサーバを構築する

Last updated at Posted at 2015-06-21

背景

最近、スタバなどの公衆無線LANを利用することが増えた。
公衆無線LANは大変便利なのだが、httpsでないサイトへのログインなど、暗号化されていない通信をするのはちと怖い。
そこで全てのパケットを暗号化して、自宅のVPNサーバ経由で通信することで、安心してノマドごっこをできるようにする。

環境

network.png

自宅はNAPTを使ったネットワークになっている。

構築手順

サーバ側の環境構築

http://centossrv.com/openvpn.shtmlのとおりに構築する。OpenVPN 2.3.6 を使用。
/etc/openvpn/server.conf は以下の通り。

port 1194
proto udp
dev tun
ca ca.crt
cert server.crt
key server.key
dh dh.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "redirect-gateway def1"
push "dhcp-option DNS 192.168.11.1"  # 自宅のDNSサーバのIP
keepalive 10 120
tls-auth ta.key 0
user nobody
group nobody
persist-key
persist-tun
status openvpn-status.log
log-append  /var/log/openvpn.log
verb 3
management localhost 7505
crl-verify crl.pem

VPNネットワークは 10.8.0.0/24 とした。
push "redirect-gateway def1" とすることで、クライアントからの全ての通信をVPNサーバ経由で行うようにする。

クライアント側の環境構築

下記の通り client.conf を作成する。

client
remote hogehoge.co.jp
ca ca.crt
cert client.crt
key client.key
tun-mtu  1350
dev tun
proto udp
remote-cert-tls server
tls-auth ta.key 1
nobind
auth-nocache
script-security 2
persist-key
persist-tun
user nobody
group nogroup

tun-mtu の値を 1350 にするのがポイント。
MTUの値を大きくすると途中の経路でパケット分割が起きるからか、上手く動作しなくなる(この辺よくわかっていない…)

クライアント側では network-manager-openvpn-gnome を使う。必要なパッケージをインストール。

$ sudo apt-get install network-manager-openvpn network-manager-openvpn-gnome 

ネットワークアイコンから VPN接続 -> VPNを設定 -> 追加 -> 保存したVPN設定をインポートする を選び client.conf を選択。
これでクライアント側の設定は完了。

ネットワークの設定

ルータに以下の設定をする。

  • 外部から来る hogehoge.co.jp:1194 宛のパケットを 192.168.11.10:1194 へ転送する(NAPT)。
  • 10.8.0.0/24 宛のパケットを 192.168.11.10 へ転送する。

動作確認

公衆無線LANに接続する。
その後、ネットワークアイコンから VPN接続 -> (クライアントのVPN設定の名前) を選択し、VPNに接続する。

外部への通信が自宅経由で行われていることを確認する。

$ traceroute www.google.co.jp
 1  10.8.0.1 (10.8.0.1)  48.214 ms  48.138 ms  48.148 ms
 2  xxxxxxxx (192.168.11.1)  50.558 ms  50.846 ms  50.707 ms
(以下略)
35
39
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
35
39