CentOS 5
OpenVPNのインストールと設定
  OpenVPNを導入して、仮想プライベートネットワークを構築します。外部からLANへ安全に接続することが可能です。

ここでは以下のような構成を前提として構築していきます。クライアントにVPNサーバーと同じネットワーク内のIPアドレスをVPNサーバー から割り当て、インターネット上を仮想プライベートネットワークを介して、通信を可能にするというものです。 (サーバー側のグローバルIPアドレスには説明の都合上、プライベートのものを使用していますが、適宜自分の環境に置き換えてください。)

(1) VPNサーバー
[172.16.2.1]
- グローバルIPアドレス

[192.168.0.17]
- eth0(元々のIPアドレス)

[192.168.0.4]
- br0 - ブリッジ用として新たに設定

(2) VPNクライアント(Windows)
[10.0.0.2]
- 元々のIPアドレス

[192.168.0.??]
- VPNサーバーより自動割り当て



なお、インターネットを介した通信のため、ルーターにNAT/ポートフォワードの設定をする必要があります。 VPNサーバー側の待ち受けポートとプロトコルはデフォルトのものを使用するため、UDP の 1194番です。 上記の例でいうと、インターネット側から 172.16.2.1 の 1194番宛てにきた UDP を 192.168.0.4 の 1194番へ転送する 設定をする必要があります。

[1] OpenVPNのインストールと設定ファイルの編集です。
# DAGからインストール

[root@ns ~]#
yum --enablerepo=dag -y install openvpn bridge-utils

[root@ns ~]#
cp /usr/share/doc/openvpn-2.0.9/sample-config-files/server.conf /etc/openvpn/

[root@ns ~]#
vi /etc/openvpn/server.conf


# 53行目:変更

dev
tap0


# 78行目:以下3行変更

ca
/etc/openvpn/easy-rsa/keys/ca.crt

cert
/etc/openvpn/easy-rsa/keys/server.crt

key
/etc/openvpn/easy-rsa/keys/server.key


# 87行目:変更

dh
/etc/openvpn/easy-rsa/keys/dh1024.pem


# 96行目:コメント化

#
server 10.8.0.0 255.255.255.0

# 103行目:コメント化

#
ifconfig-pool-persist ipp.txt

# 115行目:変更 ( [VPNサーバのIP] [マスク] [クライアントに割り当てるIPの範囲] )

server-bridge
192.168.0.4 255.255.255.0 192.168.0.50 192.168.0.100


# 125行目:変更 ( [VPNサーバのネットワーク] [マスク] )

push "route
192.168.0.0 255.255.255.0
"

# 267行目:変更

status
/var/log/openvpn-status.log


# 276行目:以下2行変更

log
/var/log/openvpn.log

log-append
/var/log/openvpn.log
[2] CA証明書とCA秘密鍵を作成します。
[root@ns ~]#
cp -r /usr/share/doc/openvpn-2.0.9/easy-rsa /etc/openvpn/

[root@ns ~]#
cd /etc/openvpn/easy-rsa/

[root@ns easy-rsa]#
mkdir keys

[root@ns easy-rsa]#
vi vars


# 45行目:以下5行、自分の環境に合わせて変更

export KEY_COUNTRY=
JP

export KEY_PROVINCE=
Hiroshima

export KEY_CITY=
Hiroshima

export KEY_ORG="
server-linux.info
"
export KEY_EMAIL="
root@server-linux.info
"

[root@ns easy-rsa]#
chmod 700 vars

[root@ns easy-rsa]#
. ./vars

NOTE: when you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/keys
[root@ns easy-rsa]#
chmod 700 clean-all

[root@ns easy-rsa]#
./clean-all

[root@ns easy-rsa]#
chmod 700 build-ca

[root@ns easy-rsa]#
./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]:
# Enter

State or Province Name (full name) [Hiroshima]:
# Enter

Locality Name (eg, city) [Hiroshima]:
# Enter

Organization Name (eg, company) [server-linux.info]:
# Enter

Organizational Unit Name (eg, section) []:
# Enter

Common Name (eg, your name or your server's hostname) []:
server-ca
 
# テキトーに入力

Email Address [root@server-linux.info]:
# Enter
[3] [2]の操作により、/etc/openvpn/easy-rsa/keys/ 配下に「
ca.crt
」というファイルが作成されているので、 それをクライアントPCにFTPなりSFTPなりでもってきておきます。

[4] サーバー証明書とサーバー秘密鍵を作成します。
[root@ns easy-rsa]#
chmod 700 build-key-server

[root@ns easy-rsa]#
./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]:
# Enter

State or Province Name (full name) [Hiroshima]:
# Enter

Locality Name (eg, city) [Hiroshima]:
# Enter

Organization Name (eg, company) [server-linux.info]:
# Enter

Organizational Unit Name (eg, section) []:
# Enter

Common Name (eg, your name or your server's hostname) []:
server
 
# テキトーに入力

Email Address [root@server-linux.info]:
# Enter


Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
# Enter

An optional company name []:
# Enter

Using configuration from /etc/openvpn/easy-rsa/openssl.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName
:PRINTABLE:'JP'

stateOrProvinceName
:PRINTABLE:'Hiroshima'

localityName
:PRINTABLE:'Hiroshima'

organizationName
:PRINTABLE:'server-linux.info'

commonName
:PRINTABLE:'server'

emailAddress
:IA5STRING:'root@server-linux.info'

Certificate is to be certified until Sep 4 14:11:20 2018 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
[5] Diffie Hellman ( DH ) パラメータを生成します。
[root@ns easy-rsa]#
chmod 700 build-dh

[root@ns easy-rsa]#
./build-dh

Generating DH parameters, 1024 bit long safe prime, generator 2
This is going to take a long time
[6] クライアント用の証明書と秘密鍵を作成します。
[root@ns easy-rsa]#
chmod 700 build-key-pass

[root@ns easy-rsa]#
./build-key-pass client

Generating a 1024 bit RSA private key
......++++++
.......................++++++
writing new private key to 'client.key'
Enter PEM pass phrase:
# パスフレーズ設定

Verifying - Enter PEM pass phrase:
# 確認再入力

-----
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]:
# Enter

State or Province Name (full name) [Hiroshima]:
# Enter

Locality Name (eg, city) [Hiroshima]:
# Enter

Organization Name (eg, company) [server-linux.info]:
# Enter

Organizational Unit Name (eg, section) []:
# Enter

Common Name (eg, your name or your server's hostname) []:
client
 
# テキトーに入力

Email Address [root@server-linux.info]:
# Enter


Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
# Enter

An optional company name []:
# Enter

Using configuration from /etc/openvpn/easy-rsa/openssl.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName
:PRINTABLE:'JP'

stateOrProvinceName
:PRINTABLE:'Hiroshima'

localityName
:PRINTABLE:'Hiroshima'

organizationName
:PRINTABLE:'server-linux.info'

commonName
:PRINTABLE:'client'

emailAddress
:IA5STRING:'root@server-linux.info'

Certificate is to be certified until Sep 4 14:20:52 2018 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
[7] [6]の操作により、/etc/openvpn/easy-rsa/keys/ 配下に「
client.crt
」と 「
client.key
」いう二つのファイルが作成されているので、 それをクライアントPCにFTPなりSFTPなりでもってきておきます。

[8] VPNサーバーのブリッジの設定と起動スクリプトの作成をし、OpenVPNを起動します。
[root@ns ~]#
cp /usr/share/doc/openvpn-2.0.9/sample-scripts/openvpn.init /etc/openvpn/

[root@ns ~]#
cp /usr/share/doc/openvpn-2.0.9/sample-scripts/bridge-stop /etc/openvpn/

[root@ns ~]#
cp /usr/share/doc/openvpn-2.0.9/sample-scripts/bridge-start /etc/openvpn/

[root@ns ~]#
chmod 755 /etc/openvpn/bridge-start

[root@ns ~]#
chmod 755 /etc/openvpn/bridge-stop

[root@ns ~]#
chmod 755 /etc/openvpn/openvpn.init

[root@ns ~]#
vi /etc/openvpn/bridge-start


# 18,19,20行目:変更 ( ブリッジに使用するIP:マスク:ブロードキャストアドレス )

eth_ip="
192.168.0.4
"
eth_netmask="
255.255.255.0
"
eth_broadcast="
192.168.0.255
"

[root@ns ~]#
vi /etc/sysctl.conf


# 7行目:変更 ( パケットの転送機能有効化 )

net.ipv4.ip_forward =
1


[root@ns ~]#
cp /etc/rc.d/init.d/network /etc/rc.d/init.d/vpn

[root@ns ~]#
vi /etc/rc.d/init.d/vpn


# 168行目あたりに以下2行追記

/etc/openvpn/bridge-start
/etc/openvpn/openvpn.init start


# 174行目あたりに以下2行追記

  stop)
/etc/openvpn/openvpn.init stop
/etc/openvpn/bridge-stop


[root@ns ~]#
/etc/rc.d/init.d/vpn start

Bringing up loopback interface:
[  OK  ]

Bringing up interface eth0:
[  OK  ]

Bringing up interface eth1:
[  OK  ]

Wed Sep 3 23:05:57 2008 TUN/TAP device tap0 opened
Wed Sep 3 23:05:57 2008 Persist state set to: ON
Starting openvpn:
[  OK  ]

[root@ns ~]#
chkconfig --add vpn

[root@ns ~]#
chkconfig vpn on