OpenVPNで自宅経由でネットワークに接続できるようにする。(サーバ構築編)

お久しぶりです。
割と簡単にできると思っていたVPNですが、なかなかてこずってしまいました・・・

結局、自分で作ったサーバでは意図した動きにできなかったので、昔買って設定で躓いて寝かせていたVPNルーターを使って構築することにしました。

既製品を使ったVPNはまだできてはいないんですが、試行錯誤していた時にルーティング方式のVPNの構築は出来ていたので、今回はそちらを紹介しようと思います。

以下、手順です。

1.epelリポジトリインストール

コマンド:yum install epel-release

2.openvpnと関連パッケージインストール

コマンド:yum install openvpn easy-rsa bridge-utils

3.認証局の作成

3-1.設置ディレクトリの作成
コマンド:mkdir /etc/openvpn/easy-rsa

3-2.ファイルをコピー
コマンド:cp /usr/share/easy-rsa/3.0.3/* /etc/openvpn/easy-rsa/ -R

3-3.ディレクトリを移動
コマンド:cd /etc/openvpn/easy-rsa

3-4.初期化
コマンド:./easyrsa init-pki

結果
init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /etc/openvpn/easy-rsa/pki

3-5.認証局の作成
コマンド:./easyrsa build-ca

結果
Generating a 2048 bit RSA private key
………………………+++
……….+++
writing new private key to ‘/etc/openvpn/easy-rsa/pki/private/ca.key.DHV75XyVLC’
Enter PEM pass phrase:[akihiro-0210]<任意のパスフレーズ>

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.

Common Name (eg: your user, host, or server name) [Easy-RSA CA]:<任意の名前を入力>

CA creation complete and you may now import and sign cert requests.
Your new CA certificate file for publishing is at:
/etc/openvpn/easy-rsa/pki/ca.crt

3-6.DHパラメータの作成
コマンド:./easyrsa gen-dh

DH parameters of size 2048 created at /etc/openvpn/easy-rsa/pki/dh.pem

3-7.サーバ用の証明書と秘密鍵の作成
コマンド:./easyrsa build-server-full server_r nopass

結果
Generating a 2048 bit RSA private key
……………………………………………..+++
writing new private key to ‘/etc/openvpn/easy-rsa/pki/private/server_r.key.aNjZ0KK0TS’

Using configuration from ./openssl-1.0.cnf
Enter pass phrase for /etc/openvpn/easy-rsa/pki/private/ca.key:<手順3-5で入力したパスフレーズを入力>
Check that the request matches the signature
Signature ok
The Subject’s Distinguished Name is as follows
commonName :ASN.1 12:’server_r’
Certificate is to be certified until Jul 7 13:48:31 2029 GMT (3650 days)

Write out database with 1 new entries
Data Base Updated

3-8.クライアント用の証明書と秘密鍵の作成
コマンド:./easyrsa build-client-full client1 nopass

結果
Generating a 2048 bit RSA private key
……..+++
………..+++
writing new private key to ‘/etc/openvpn/easy-rsa/pki/private/server_r.key.aNjZ0KK0TS’
———-

Using configuration from ./openssl-1.0.cnf
Enter pass phrase for /etc/openvpn/easy-rsa/pki/private/ca.key:<手順3-5で入力したパスフレーズを入力>
Check that the request matches the signature
Signature ok
The Subject’s Distinguished Name is as follows
commonName :ASN.1 12:’client1′
Certificate is to be certified until Jul 7 13:53:39 2029 GMT (3650 days)

Write out database with 1 new entries
Data Base Updated

3-9.TLS認証キーの作成
コマンド:openvpn –genkey –secret /etc/openvpn/ta.key

4.設定ファイルの作成
ファイル:/etc/openvpn/server.conf

以下の内容で作成

port 1194
proto udp
dev tun
ca /etc/openvpn/easy-rsa/pki/ca.crt
cert /etc/openvpn/easy-rsa/pki/issued/server_r.crt
key /etc/openvpn/easy-rsa/pki/private/server_r.key
dh /etc/openvpn/easy-rsa/pki/dh.pem
server 10.8.0.0 255.255.255.0
push “route 10.2.22.0 255.255.255.0″ ← 接続したいローカルネットワーク
push “redirect-gateway def1″ ← 全トラフィックをVPN経由にする
push “dhcp-option DNS 8.8.8.8″ ← VPNクライアントのDNS
client-to-client
keepalive 10 120
tls-auth /etc/openvpn/ta.key 0
comp-lzo
user nobody
group nobody
persist-key
persist-tun
status /var/log/openvpn-status.log
log /var/log/openvpn.log
verb 3

5.サービス起動と自動起動設定

5-1.サービス起動
コマンド:systemctl start openvpn@server

5-2.サービス自動起動設定
コマンド:systemctl start openvpn@server

6.Firewallの設定

6-1.設定
コマンド:firewall-cmd –add-port=1194/udp –permanent
コマンド:firewall-cmd –add-masquerade –permanent

6-2.適用
コマンド:firewall-cmd –reload

7.ルーティングの有効化
「/etc/sysctl.conf」に「net.ipv4.ip_forward = 1」を追記し適用のために再起動

今日はとりあえずここまで
次回、クライアント側(iphone)の設定を紹介予定です。

では