23.5. 基本的な設定

サンプルファイルを使用した基本的な VPN サーバーの設定手順について解説します。OpenVPN には ルーティング VPN と ブリッジ VPN という 2 つの方式がありますが、本ガイドでは多く利用されるルーティング VPN について扱います。

23.5.1. サンプルファイルのコピー

まずは、サンプル設定ファイルを次のようにコピーします。client.conf はクライアント用の設定ファイルですので適切な編集を行い該当するクライアントへ配置します。

# cp /usr/share/doc/packages/openvpn/sample-config-files/server.conf /etc/openvpn/ 
# cp /usr/share/doc/packages/openvpn/sample-config-files/client.conf /etc/openvpn/

生成した鍵を格納するディレクトリ(例では /etc/openvpn/keys )の作成とスクリプトファイルのコピーを行います。

# cd /etc/openvpn/
# mkdir keys
# cp /usr/share/doc/packages/openvpn/easy-rsa/build-key /etc/openvpn/
# cp /usr/share/doc/packages/openvpn/easy-rsa/build-ca /etc/openvpn/  
# cp /usr/share/doc/packages/openvpn/easy-rsa/build-key-server /etc/openvpn/
# cp /usr/share/doc/packages/openvpn/easy-rsa/build-dh /etc/openvpn/
# cp /usr/share/doc/packages/openvpn/easy-rsa/vars /etc/openvpn/
# cp /usr/share/doc/packages/openvpn/easy-rsa/clean-all /etc/openvpn/
# cp /usr/share/doc/packages/openvpn/easy-rsa/openssl.cnf /etc/openvpn/  

ティップ

openssl.cnf はスクリプトを実行する際に必要な設定ファイルです。各スクリプトと同じディレクトリ以下にコピーしておいてください。

23.5.2. ルート証明書と秘密鍵の作成

ここでは、項23.5.1 でコピーをした各ファイルを使用してルート証明書を作成する手順を解説します。ルート証明書は、以降で作成するサーバーおよびクライアントの証明書(公開鍵)と秘密鍵を証明するための自己証明機関の役割をします。

まずは、/etc/openvpn/ ディレクトリにコピーした環境設定ファイル vars の次の部分を編集します。ここで指定した値は、各スクリプトを実行した際のデフォルト値になりますので、実際の環境に合わせて入力してください。

export KEY_COUNTRY=JP
export KEY_PROVINCE=Tokyo
export KEY_CITY=Shibuya
export KEY_ORG="MyCompany"
export KEY_EMAIL="taro@example.com"

次のように実行し環境設定と公開鍵の初期化を行います。

# . ./vars
NOTE: when you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/keys
# ./clean-all

ティップ

clean-all を実行すると /etc/openvpn/keys 以下をすべて削除します。既に生成済みの鍵を格納している場合には注意してください。

続いてルート証明書と秘密鍵を生成するために以下のように実行します。スクリプトから openssl コマンドが実行されます。vars に設定した値が表示されますので[Enter]キーで進みます。Common Name(共通名)のみはデフォルト値が設定されないため、ここで必ず入力してください。以下例では、example.com と入力しています。

# ./build-ca
Generating a 1024 bit RSA private key
...++++++
..++++++
writing new private key to 'ca.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [JP]: 
State or Province Name (full name) [Tokyo]: 
Locality Name (eg, city) [Shibuya]: 
Organization Name (eg, company) [MyCompany]: 
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:example.com
Email Address [taro@example.com]:
#

ルート証明書と鍵は ca.crt と ca.key というファイル名で keys ディレクトリ以下に生成されています。

23.5.3. サーバー証明書と秘密鍵生成

続いて、項23.5.1 でコピーをした build-key-server を使用してサーバー証明書と秘密鍵を作成する手順を解説します。

次のように実行するとルート証明書の場合と同様に vars に設定した値が表示されますので[Enter]キーで進みます。Common Name(共通名)のみはデフォルト値が設定されないため、ここで必ず入力してください。以下例では、server と入力しています。"Sign the certificate? [y/n]:" と "1 out of 1 certificate requests certified, commit? [y/n]:" という 2 つの質問は y で進んでください。

# ./build-key-server server
Generating a 1024 bit RSA private key
......++++++
.....................................++++++
writing new private key to 'server.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [JP]:
State or Province Name (full name) [Tokyo]:
Locality Name (eg, city) [Shibuya]:
Organization Name (eg, company) [MyCompany]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:server
Email Address [taro@example.com]:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /etc/openvpn/openssl.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName           :PRINTABLE:'JP'
stateOrProvinceName   :PRINTABLE:'Tokyo'
localityName          :PRINTABLE:'Shibuya'
organizationName      :PRINTABLE:'MyCompany'
commonName            :PRINTABLE:'server'
emailAddress          :IA5STRING:'taro@example.com'
Certificate is to be certified until Oct 27 02:10:04 2017 GMT (3650 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

サーバー証明書と秘密鍵、証明書要求は server.crt と server.key 、server.csr というファイル名で keys ディレクトリ以下に生成されています。

23.5.4. クライアント証明書と秘密鍵生成

続いて、項23.5.1 でコピーをした build-key を使用してクライアント証明書および秘密鍵を作成する手順を解説します。

次のように実行するとサーバーへの接続を許可するクライアント用の証明書と鍵を生成します。次は client1 と client2 の 2 台のクライアント用の鍵生成の例です。基本的な設定方法はサーバーの場合と同様です。commonName は必ずユニークなものを指定してください。

# ./build-key client1
Generating a 1024 bit RSA private key
.........................++++++
....................++++++
writing new private key to 'client1.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [JP]:
State or Province Name (full name) [Tokyo]:
Locality Name (eg, city) [Shibuya]:
Organization Name (eg, company) [MyCompany]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:client1
Email Address [taro@example.com]:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /etc/openvpn/openssl.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName           :PRINTABLE:'JP'
stateOrProvinceName   :PRINTABLE:'Tokyo'
localityName          :PRINTABLE:'Shibuya'
organizationName      :PRINTABLE:'MyCompany'
commonName            :PRINTABLE:'client1'
emailAddress          :IA5STRING:'taro@example.com'
Certificate is to be certified until Oct 27 02:24:13 2017 GMT (3650 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

同様に client2 用の証明書と鍵を生成します。

# ./build-key client2

クライアント証明書と秘密鍵、証明書要求は client1.crt と client1.key 、client1.csr といったファイル名で keys ディレクトリ以下に生成されています。

注意

commonName には、他のクライアントと同じ値を指定することはできません。必ずユニークな値を入力してください。

ティップ

次回からクライアントを追加する場合には以下のように実行します。

# cd /etc/openvpn
# . ./vars
# ./build-key clientn

23.5.5. Diffie Hellman (DH) 鍵共有パラメータ生成

続いて、項23.5.1 でコピーをした build-dh を使用して DH 鍵共有パラメータを作成する手順を解説します。

次のように実行します。

# ./build-dh
Generating DH parameters, 1024 bit long safe prime, generator 2
This is going to take a long time
.......................................................................................................+..............................+........................................+.............................................................+................................................................................................................+...........................................................................+....................+......+...............................................................................................................................................................................................................+......................................................+..............................++*++*++*

dh 鍵共有パラメータは、dh1024.pem というファイル名で keys ディレクトリ以下に生成されています。

23.5.6. 生成ファイルの配置

ここまでの手順で生成した証明書と鍵ファイルは以下の通りです。それぞれサーバー、クライアントに適切に配置します。非公開欄に○の記載されているファイルについては公開してはいけません。安全な方法でファイルをやりとりするように注意してください。

表 23-4. vars の設定項目

ファイル名配置説明非公開
ca.crtサーバー、全クライアントルート証明書(項23.5.3 参照) 
ca.key署名を行うホストルート秘密鍵ファイル(項23.5.2 参照)
server.crtサーバーサーバー証明書(項23.5.3 参照) 
server.keyサーバーサーバー秘密鍵(項23.5.3 参照)
client1.crtclient1 クライアントclient1 の証明書(項23.5.4 参照) 
client1.keyclient1 クライアントclient1 の秘密鍵項23.5.4 参照)
client2.crtclient2 クライアントclient2 の証明書項23.5.4 参照) 
client2.keyclient2 クライアントclient2 の秘密鍵(項23.5.2 参照)
dh1024.pemDH 鍵共有パラメータサーバー(項23.5.2 参照) 

23.5.7. サーバー設定ファイル(server.conf)

ここでは、サーバー側の設定ファイル /etc/openvpn/server.conf の基本的な設定を行います。項23.5.1 の例のようにサンプルファイルを /etc/openvpn/ 以下に配置した場合の例で解説します。

主な設定項目は以下の通りです。また、# および ; で始まる行はコメントです。

port 1194

ポート番号の指定です。ファイアウォールのある環境ではパケットの通過が可能なように設定が必要です。

proto udp

プロトコルの指定です。

dev tun

仮想 tun ネットワークインターフェイスをルーティング VPN として作成します。ブリッジ VPN として作成する場合は、tap と変更する必要があります。

ca ca.crt

ルート証明書のパス指定です。実際の格納先パス名に変更してください。

cert server.crt

サーバー証明書のパス指定です。実際の格納先パス名に変更してください。

key server.key

サーバーの秘密鍵のパス指定です。実際の格納先パス名に変更してください。

dh dh1024.pem

DH 鍵共有パラメータのパス指定です。実際の格納先パス名に変更してください。

server 10.8.0.0 255.255.255.0

VPN クライアントに配布する IP アドレスの指定です。例の場合は、10.8.0.1 をサーバー自身が使用します。ブリッジ VPN の場合は、server-bridge を指定する必要があります。

ここまでの例のとおりに実行している場合、サンプルファイルを利用するには以下の部分を編集するだけです。

ca keys/ca.crt
cert keys/server.crt
key keys/server.key  # This file should be kept secret
dh keys/dh1024.pem

設定が完了したらサービスを起動します。

# /etc/init.d/openvpn start
Starting openvpn:                                                   OK