先日、 yum で一撃インストールした OpenVPN を設定していきます。サーバ側でやらなければならないことは大きく分けて3つあります。
1. CA の構築とキーと証明書の発行
2. OpenVPNサーバの設定
3. サーバのネットワークの設定
以下、設定メモ。
† CA の構築とキーと証明書の発行
OpenVPN は SSL を利用したVPNなので、接続の管理・暗号化のためのキーが必要になります。
先にサーバの設定からやってもいいのですが、設定ファイル内に証明書とキーの情報を書く必要があるので、先にこちらを済ませてしまいます。証明書については当然、外部のCAから証明書を買ってくるという選択肢もありますが、費用の問題から個人利用の場合には自分でCA(証明機関)を作成し、キーと証明書を発行するということになろうかと思います。 OpenVPN ではこのような使い方を想定して、 eazy-rsa というツールがあらかじめパッケージに含まれているので、これを使います。
下記のコマンドで /etc/openvpn/easy-rsa にeazy-rsaをインストールします。
# cd /usr/share/doc/openvpn-2.2.0/easy-rsa/2.0
# make install DESTDIR=/etc/openvpn/easy-rsa
下記のコマンドでCAのキーと証明書を作成します。
build-*のコマンドの起動した際に聞かれる入力項目や vars の内容は必要に応じてを変更する必要がありますが、プライベートユースであれば変更しなくて(端にエンターキーを押すだけ)も問題ないはずです。内容についてはサーバのSSLを買うために .csr ファイルを作成した経験がある人にはおなじみの内容なので、分かる人は変更しておいた方が後々管理が楽です。
# cd /etc/openvpn/easy-rsa
# (vars を必要に応じて変更)
# . ./vars
# ./clean-all
# ./build-ca
完了すると keys ディレクトリ内に ca.key と ca.crt が生成されているはずです。
続いて、クライアントとサーバーのキーと証明書を生成します。今回はサーバ名は thove, クライアント名は ferry としています。注意点としては、サーバのキーと証明書は build-key-server で、クライアントのキーと証明書は build-key コマンドで作る必要があることでしょうか。これを間違えると接続する段階でエラーになってハマるので気を付けた方がいいです。ここでもいろいろ聞かれますが、分からなければ単にエンターを押していくだけでもいいと思います。
# ./build-dh
# ./build-key-server thove
# ./build-key ferry
完了すると keys ディレクトリ内に dh1024.pem, thove.key , thove.crt, ferry.key, ferry.crt が生成されているはずです。サーバ側ではdh1024.pem, thove.key , thove.crt, ca.crt をのみを使うので、これらのファイルを下記の手順で /etc/openvpn 内にコピーします。残りの ferry.key, ferry.crt についてはクライアントの設定の時に使うので、ローカルのPCにダウンロードしておきます。
# cp -f /etc/openvpn/easy-rsa/keys/ca.crt /etc/openvpn
# cp -f /etc/openvpn/easy-rsa/keys/thove.crt /etc/openvpn
# cp -f /etc/openvpn/easy-rsa/keys/thove.key /etc/openvpn
# chmod 600 /etc/openvpn/easy-rsa/keys/thove.key
# cp -f /etc/openvpn/easy-rsa/keys/dh1024.pem /etc/openvpn
# chmod 600 /etc/openvpn/easy-rsa/keys/dh1024.pem
これでキーの準備は完了です。
† サーバの設定
作成したキーを使ってサーバ側の設定をし、サーバを起動してやります。
起動スクリプト (/etc/init.d/openvpn) は /etc/openvpn に .conf ファイルを見つけるとそれに従ってサーバを起動してくれるようになっていますので、具体的には .conf ファイルを作成するだけの作業になります。.conf ファイルは /usr/share/doc/openvpn-2.2.0/sample-config-files に入っているサンプルを流用するのが一番お手軽なので、これをコピーして使います。
# cd /etc/openvpn
# cp -a /usr/share/doc/openvpn-2.2.0/sample-config-files/server.conf .
# (server.confを編集。下記参照。)
# /etc/init.d/openvpn start
今回は下記の前提の基に編集していますので、適宜読み替える必要があります。
・プロトコルはTCP (ネットワーク的にUDPが通らないため)
・サーバのローカルアドレスは 192.168.17.224/24
/etc/openvpn/server.conf
--- server.conf.org 2010-10-21 18:18:17.000000000 +0900
+++ server.conf 2011-11-11 00:16:31.004979615 +0900
@@ -32,8 +32,8 @@
port 1194
# TCP or UDP server?
-;proto tcp
-proto udp
+proto tcp
+;proto udp
# "dev tun" will create a routed IP tunnel,
# "dev tap" will create an ethernet tunnel.
@@ -76,8 +76,8 @@
# OpenVPN can also use a PKCS #12 formatted key file
# (see "pkcs12" directive in man page).
ca ca.crt
-cert server.crt
-key server.key # This file should be kept secret
+cert thove.crt
+key thove.key # This file should be kept secret
# Diffie hellman parameters.
# Generate your own with:
@@ -100,7 +100,7 @@
# is restarted, reconnecting clients can be assigned
# the same virtual IP address from the pool that was
# previously assigned.
-ifconfig-pool-persist ipp.txt
+;ifconfig-pool-persist ipp.txt
# Configure server mode for ethernet bridging.
# You must first use your OS's bridging capability
@@ -135,6 +135,7 @@
# back to the OpenVPN server.
;push "route 192.168.10.0 255.255.255.0"
;push "route 192.168.20.0 255.255.255.0"
+push "route 192.168.17.0 255.255.255.0"
# To assign specific IP addresses to specific
# clients or if a connecting client has a private
@@ -201,7 +202,7 @@
# To force clients to only see the server, you
# will also need to appropriately firewall the
# server's TUN/TAP interface.
-;client-to-client
+client-to-client
# Uncomment this directive if multiple clients
# might connect with the same certificate/key
@@ -214,7 +215,7 @@
# CERTIFICATE/KEY PAIRS FOR EACH CLIENT,
# EACH HAVING ITS OWN UNIQUE "COMMON NAME",
# UNCOMMENT THIS LINE OUT.
-;duplicate-cn
+duplicate-cn
# The keepalive directive causes ping-like
# messages to be sent back and forth over
@@ -259,8 +260,8 @@
#
# You can uncomment this out on
# non-Windows systems.
-;user nobody
-;group nobody
+user nobody
+group nobody
# The persist options will try to avoid
# accessing certain resources on restart
問題がなければこんな感じで起動しているのが確認できます。
# ps auxfww | grep openvpn | grep -v grep
nobody 18256 0.0 0.0 67424 1296 ? Ss 11:03 0:00 /usr/sbin/openvpn --daemon --writepid /var/run/openvpn/server.pid --config server.conf --cd /etc/openvpn --script-security 2
† ネットワーク設定の変更
OpenVPN では雛形の設定をそのまま使うとクライアントには、DHCPで 10.8.0.0/24 のアドレスが振られてきます。このアドレスを使えばOpenVPNがインストールされているサーバ自身のサービスにはアクセスできるようになります。が、VPNの目的はサーバがつながっているネットワーク内のサービスを利用するという場合がほとんどかと思います。ということで、NATの設定を行ってサーバ側のネットワークに出て行くことができるようにします。
まず ip_forward の設定をして、有効化しておきます。
# (/etc/sysctl.confの編集。下記参照。)
# /etc/init.d/network restart
/etc/sysctl.conf
--- sysctl.conf.org 2011-05-21 05:59:50.000000000 +0900
+++ sysctl.conf 2011-11-11 11:08:21.526825611 +0900
@@ -4,7 +4,7 @@
# sysctl.conf(5) for more details.
# Controls IP packet forwarding
-net.ipv4.ip_forward = 0
+net.ipv4.ip_forward = 1
# Controls source route verification
net.ipv4.conf.default.rp_filter = 1
最後に iptables を使って NAT の設定を行い、その設定を保存します。
server.conf でアドレスを変更している場合にはIPの部分を読み替える必要があります。
# iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
# /etc/init.d/iptables save
これでサーバ側の設定は完了です。