LoginSignup
5
5

More than 5 years have passed since last update.

Raspberry Pi 3をOpenVPNサーバにする(前提環境③)VPNサーバ

Last updated at Posted at 2017-05-14

Raspberry Pi環境の全体はリンクを参照

基本的には以下のサイトを参考にしながら、それぞれのいいとこどりで構築しました。

OpenVPNの設定(その4:トンネルモードサーバー設定)
Raspberry Pi 3にOpenVPNをインストールする(サーバー編)

必要なもの

Raspberry Pi 3とL-03C(前提環境①)

設定する前の注意点

クライアントはiPhoneを想定します。クライアントがiPhoneの場合は、トンネルモードのみになります。

OpenVPNのインストール

easy-rsaのファイル一式をOpenVPN用としてコピーしておきます。
$ sudo apt-get install openvpn openssl
$ sudo cp -r /usr/share/easy-rsa /etc/openvpn/easy-rsa

証明書作成のコマンドを実行した時の初期値となる内容を設定しておきます。ここで設定しておくと何回も同じ内容を入力しなくてもEnterでスルー出来る。

$ cd /etc/openvpn/easy-rsa
$ sudo vi vars

vars
export EASY_RSA="`pwd`"
・・・()・・・
#Increase this to 2048 if you
#are paranoid.  This will slow
#down TLS negotiation performance
#as well as the one-time DH parms
#generation process.
export KEY_SIZE=2048

export KEY_COUNTRY="JP"
export KEY_PROVINCE="Tokyo"
export KEY_CITY="Chiyoda-Ku"
export KEY_ORG="MyOffice"
export KEY_EMAIL="admin@example.com"
export KEY_OU="VPNServer"

//X509 Subject Field
export KEY_NAME="EasyRSA"

環境変数をシステムに反映する上で設定した内容が有効になるように反映させます。このとき、権限がないとPermission deniedとエラーが出るので、root権限で実行してください。
$ su -
$ cd /etc/openvpn/easy-rsa
$ source ./vars

「NOTE: If you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/keys」というメッセージが現れますが、無視します。

証明書と鍵作成先ディレクトリを初期化する証明書や鍵は/etc/openvpn/easy-rsa/keys/に作成されます。
$ ./clean-all
何もメッセージが表示されなければ成功です。
「Please source the vars script first(i.e. "source ./vars") Make sure you have edited it to reflect your configuration.」というメッセージが現れたら、上のroot権限に変更するあたりからやりなおしてください。

認証局証明書と秘密鍵を作成する上で設定した内容がデフォルトで表示されますのでEnterで進めます。
$ ./build-ca

VPNサーバー証明書と秘密鍵を作成するEnterで進めて以下の選択肢はyにする。
$ ./build-key-server server
・・・(以下のメッセージまですべてenter)・・・
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y

DHパラメータを作成します。作成には時間がかかります。
$ ./build-dh

OpenVPNが送受信するすべてのパケットに署名を付加するためのTLS認証鍵を作成する。
$ openvpn --genkey --secret /etc/openvpn/easy-rsa/keys/ta.key

VPNクライアント証明書と秘密鍵を作成する
Enterで進めて以下の選択肢だけyにする。
$ ./build-key client1
・・・(以下のメッセージまですべてenter)・・・
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y

Asteriskで内線電話をテストしたい場合は、ここでもうひとつクライアントを作成しておきます。
$ ./build-key client2
・・・(以下のメッセージまですべてenter)・・・
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y

OpenVPNの設定ファイルを編集する
サンプルの設定ファイルは.gzで圧縮されているので解凍して、OpenVPNのディレクトリにコピーする。以下、コメントを除いた設定の部分のみの内容。詳細は環境に合わせて設定を煮詰めるという事で。
$ sudo gzip -d /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz
$ sudo cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf /etc/openvpn/server.conf
$ sudo vi /etc/openvpn/server.conf

server.conf
・・・()・・・
port 1194
proto udp
dev tun
ca /etc/openvpn/easy-rsa/keys/ca.crt
cert /etc/openvpn/easy-rsa/keys/server.crt
key /etc/openvpn/easy-rsa/keys/server.key  # This file should be kept secret
dh /etc/openvpn/easy-rsa/keys/dh2048.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "route 192.168.0.0 255.255.255.0"
client-to-client
keepalive 10 120
tls-auth /etc/openvpn/easy-rsa/keys/ta.key 0 # This file is secret
cipher AES-256-CBC
comp-lzo
user nobody
group nogroup
persist-key
persist-tun
status /var/log/openvpn-status.log
log /var/log/openvpn.log
log-append /var/log/openvpn.log
verb 3

ufwのインストール

ufwをインストールします。
$ sudo apt-get install ufw
OpenVPNで使用するポートを許可する
$ sudo ufw allow 1194/udp

IPフォワーディングを有効にする
以下の行のコメントを外す。
$ sudo vi /etc/ufw/sysctl.conf

sysctl.conf
$ Uncomment this to allow this host to route packets between interfaces
net/ipv4/ip_forward=1

ufwのルールを編集する
$ sudo vi /etc/ufw/before.rules
以下の内容をファイルの先頭に追加する。

before.rules
# 
# rules.before
# 
*nat
:POSTROUTING ACCEPT [0:0]
-A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
COMMIT
・・・(略)・・・
# don't delete the 'COMMIT' line or these rules won't be processed
-A ufw-before-input -i tun+ -j ACCEPT
-A ufw-before-output -i tun+ -j ACCEPT
-A ufw-before-forward -s 10.8.0.0/24 -j ACCEPT
-A ufw-before-forward -d 10.8.0.0/24 -j ACCEPT
COMMIT

ufwを再起動する
$ sudo ufw disable
$ sudo ufw enable

ポートが許可されているか確認する。
$ sudo ufw status

Status: active

To Action From
1194/udp ALLOW Anywhere
1194/udp ALLOW Anywhere(v6)

再びOpenVPN

OpenVPNを再起動して[ ok ]になるか確認する。
$ sudo /etc/init.d/openvpn restart
[ ok ] Restarting openvpn (via systemctl): openvpn.service.

OpenVPNサーバーの状態がactiveになっているか確認する。
$ sudo /etc/init.d/openvpn status
● openvpn.service - OpenVPN service
Loaded : loaded (....)
Active : active (exited) ...
Process: ...
Main PID: ...
raspberrypi systemd[1] : Started OpenVPN service.
$ sudo systemctl enable openvpn
Executing /usr/sbin/update-rc.d openvpn defaults
Executing /usr/sbin/update-rc.d openvpn enable

VPNクライアント用の設定ファイルを作成する
以下の場所にあるサンプルファイルをコピーして、上で作った各種証明書ファイルと共にクライアントに持って行く。可能であればUSBメモリなど安全な方法で。もちろんそのUSBメモリの取り扱いには注意が必要。使った後は決してその辺に置き去りにせず、すぐにクイックフォーマットじゃない方法で初期化しておく。内容はサーバーに合わせて編集する。以下、コメントを除いた設定の部分のみの内容。なお、サンプルはclient.confになっているけど、OpenVPNクライアントの設定ファイルの拡張子は.ovpnなので、1台目のクライアントならclient1.ovpnとしておく。
$ sudo cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf /etc/openvpn/easy-rsa/keys/client1.conf

client.conf
client
dev tun
proto udp
remote (インターリンクLTEで払い出されたグローバルIPアドレス) 1194
resolv-retry infinite
nobind
user nobody
group nogroup
persist-key
persist-tun
ca ca.crt
cert client1.crt
key client1.key
ns-cert-type server
tls-auth ta.key 1
cipher AES-256-CBC
comp-lzo
verb 3

※ Raspberry Pi 3にOpenVPNをインストールする(iOSクライアント編)

Asteriskで内線電話をテストしたい場合は、ここでもうひとつクライアントを作成しておきます。

cp /etc/openvpn/easy-rsa/keys/client1.conf /etc/openvpn/easy-rsa/keys/client2.conf
client2.confの中身で、certとkeyの箇所をそれぞれclient2.crt、client2.keyに変更する。
client1.confをclient1.ovpnに変更します。client2についても同様に行います。
mv /etc/openvpn/easy-rsa/keys/client1.conf /etc/openvpn/easy-rsa/keys/client1.ovpn
mv /etc/openvpn/easy-rsa/keys/client2.conf /etc/openvpn/easy-rsa/keys/client2.ovpn

iPhoneまたはiPadにインストールするファイル群はクライアント一つあたり以下のとおりです。/etc/openvpn/easy-rsa/keysのディレクトリ配下にそろっていることを確認すれば準備完了です。
 ・ca.crt
 ・client1.crt
 ・client1.key
 ・client1.ovpn
 ・ta.key

この記事はここまでになります。

5
5
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
5
5