カタカタブログ

SIerで働くITエンジニアがカタカタした記録を残す技術ブログ。Java, Oracle Database, Linuxが中心です。たまに数学やデータ分析なども。

Raspberry Pi 2とSoftEtherを使って自宅VPNを構築してみた

これまでヤマハやBUFFALOの市販のルータを使って自宅VPNを構築してきた。
totech.hateblo.jp
totech.hateblo.jp
しかし、先日BUFFALOのルータを買い換えたところ、PPTPサーバ機能がなくてVPNサーバとして使えなかったため、
Raspberry Pi 2を使ってソフトウェアVPNサーバを構築してみようと思い立った。

そこで、今回はSoftEther(ソフトイーサ)というソフトウェアVPNを使って自宅VPNを構築する。
SoftEtherクラウド上でVPNの経路を中継する仕組みのため、VPNサーバとVPNクライアント間で直接セッションを貼るのではなく、間にVPN Azureというクラウドサービスを使う。これは筑波大学が研究用途で公開している無料のサービスで、カタログスペック上はopenvpnよりも帯域や使いやすさで上回っているらしい。
また、クラウドサービスを利用することで、VPN機能のルータはもちろんグローバルIPアドレスすら不要となる。

よって、今回はRaspberry Pi 2上にSoftEtherのVPNサーバをインストールし、VPN Azureを使って中継し、Windows 7からSoftEtherのVPNクライアントを使って自宅LANにVPNに接続する。

基本的には以下の公式マニュアルを参照して構築した。
https://ja.softether.org/4-docs/1-manual/7/7.3

Raspberry PiにSoftEther VPN Serverインストール

まずはRaspberry Pi にVPNサーバを構築する。
SoftEtherのVPNサーバインストーラは以下からダウンロードできる。
http://www.softether.org/5-download
現時点の最新版である softether-vpnserver-v4.21-9613-beta-2016.04.24-linux-arm_eabi-32bit.tar.gz をダウンロードした。

f:id:osn_th:20160806181937p:plain

以下、Raspberry Pi上でインストールする作業をrootユーザで行う。
まず、ダウンロードしたインストーラを展開する。

root@raspberrypi:~# pwd
/root
root@raspberrypi:~# ls -l softether-vpnserver-v4.21-9613-beta-2016.04.24-linux-arm_eabi-32bit.tar.gz
-rw-r--r-- 1 root root 5646405  8月  4  2016 softether-vpnserver-v4.21-9613-beta-2016.04.24-linux-arm_eabi-32bit.tar.gz

root@raspberrypi:~# tar xzvf softether-vpnserver-v4.21-9613-beta-2016.04.24-linux-arm_eabi-32bit.tar.gz
vpnserver/
vpnserver/Makefile
vpnserver/.install.sh
vpnserver/ReadMeFirst_License.txt
vpnserver/Authors.txt
vpnserver/ReadMeFirst_Important_Notices_ja.txt
vpnserver/ReadMeFirst_Important_Notices_en.txt
vpnserver/ReadMeFirst_Important_Notices_cn.txt
vpnserver/code/
vpnserver/code/vpnserver.a
vpnserver/code/vpncmd.a
vpnserver/lib/
vpnserver/lib/libcharset.a
vpnserver/lib/libcrypto.a
vpnserver/lib/libedit.a
vpnserver/lib/libiconv.a
vpnserver/lib/libncurses.a
vpnserver/lib/libssl.a
vpnserver/lib/libz.a
vpnserver/lib/License.txt
vpnserver/hamcore.se2

実行可能ファイルを作成するため、コンパイルを行う。
途中でライセンスに関する確認が行われるので、回答する。

root@raspberrypi:~# cd vpnserver/
root@raspberrypi:~/vpnserver# make
--------------------------------------------------------------------

SoftEther VPN Server (Ver 4.21, Build 9613, ARM EABI) for Linux Install Utility
Copyright (c) SoftEther Project at University of Tsukuba, Japan. All Rights Reserved.

--------------------------------------------------------------------


Do you want to read the License Agreement for this software ?

 1. Yes
 2. No

Please choose one of above number:
1

(・・・略)
Did you read and understand the License Agreement ?
(If you couldn't read above text, Please read 'ReadMeFirst_License.txt'
 file with any text editor.)

 1. Yes
 2. No

Please choose one of above number:
1


Did you agree the License Agreement ?

1. Agree
2. Do Not Agree

Please choose one of above number:
1

make[1]: Entering directory '/root/vpnserver'
Preparing SoftEther VPN Server...
ranlib lib/libcharset.a
ranlib lib/libcrypto.a
ranlib lib/libedit.a
ranlib lib/libiconv.a
ranlib lib/libncurses.a
ranlib lib/libssl.a
ranlib lib/libz.a
ranlib code/vpnserver.a
gcc code/vpnserver.a -O2 -fsigned-char -lm -ldl -lrt -Wl,--no-warn-mismatch -lpthread -L./ lib/libssl.a lib/libcrypto.a lib/libiconv.a lib/libcharset.a lib/libedit.a lib/libncurses.a lib/libz.a -o vpnserver
ranlib code/vpncmd.a
gcc code/vpncmd.a -O2 -fsigned-char -lm -ldl -lrt -Wl,--no-warn-mismatch -lpthread -L./ lib/libssl.a lib/libcrypto.a lib/libiconv.a lib/libcharset.a lib/libedit.a lib/libncurses.a lib/libz.a -o vpncmd
./vpncmd /tool /cmd:Check
vpncmd コマンド - SoftEther VPN コマンドライン管理ユーティリティ
SoftEther VPN コマンドライン管理ユーティリティ (vpncmd コマンド)
Version 4.21 Build 9613   (Japanese)
Compiled 2016/04/24 16:39:47 by yagi at pc30
Copyright (c) SoftEther VPN Project. All Rights Reserved.

VPN Tools を起動しました。HELP と入力すると、使用できるコマンド一覧が表示できます。

VPN Tools>Check
Check コマンド - SoftEther VPN の動作が可能かどうかチェックする
---------------------------------------------------
SoftEther VPN 動作環境チェックツール

Copyright (c) SoftEther VPN Project.
All Rights Reserved.

この動作環境チェックツールを実行したシステムがテストに合格した場合は、SoftEther VPN ソフトウェアが動作する可能性が 高いです。チェックにはしばらく時間がかかる場合があります。そのままお待ちください...

'カーネル系' のチェック中...
              [合格] ○
'メモリ操作系' のチェック中...
              [合格] ○
'ANSI / Unicode 文字列処理系' のチェック中...
              [合格] ○
'ファイルシステム' のチェック中...
              [合格] ○
'スレッド処理システム' のチェック中...
              [合格] ○
'ネットワークシステム' のチェック中...
              [合格] ○

すべてのチェックに合格しました。このシステム上で SoftEther VPN Server / Bridge が正しく動作する可能性が高いと思われます。

コマンドは正常に終了しました。


--------------------------------------------------------------------
The preparation of SoftEther VPN Server is completed !


*** How to switch the display language of the SoftEther VPN Server Service ***
SoftEther VPN Server supports the following languages:
  - Japanese
  - English
  - Simplified Chinese

You can choose your prefered language of SoftEther VPN Server at any time.
To switch the current language, open and edit the 'lang.config' file.


*** How to start the SoftEther VPN Server Service ***

Please execute './vpnserver start' to run the SoftEther VPN Server Background Service.
And please execute './vpncmd' to run the SoftEther VPN Command-Line Utility to configure SoftEther VPN Server.

Of course, you can use the VPN Server Manager GUI Application for Windows / Mac OS X on the other Windows / Mac OS X computers in order to configure the SoftEther VPN Server remotely.


*** For Windows users ***
You can download the SoftEther VPN Server Manager for Windows
from the http://www.softether-download.com/ web site.
This manager application helps you to completely and easily manage the VPN server services running in remote hosts.


*** For Mac OS X users ***
In April 2016 we released the SoftEther VPN Server Manager for Mac OS X.
You can download it from the http://www.softether-download.com/ web site.
VPN Server Manager for Mac OS X works perfectly as same as the traditional Windows versions. It helps you to completely and easily manage the VPN server services running in remote hosts.

--------------------------------------------------------------------

make[1]: Leaving directory '/root/vpnserver'

正常に終了したら、VPN Serverのプログラムが生成されるので、それをvpnserverディレクトリごと/usr/localに移動する。

root@raspberrypi:~/vpnserver# cd ..
root@raspberrypi:~# ls
softether-vpnserver-v4.21-9613-beta-2016.04.24-linux-arm_eabi-32bit.tar.gz  vpnserver
root@raspberrypi:~# mv vpnserver/ /usr/local/
root@raspberrypi:~# ls -l /usr/local/vpnserver/
合計 8784
-rwxrwxrwx 1 root root    1881  4月 24 08:11 Authors.txt
-rwxrwxrwx 1 root root    2859  4月 24 08:11 Makefile
-rwxrwxrwx 1 root root   30801  4月 24 08:11 ReadMeFirst_Important_Notices_cn.txt
-rwxrwxrwx 1 root root   36297  4月 24 08:11 ReadMeFirst_Important_Notices_en.txt
-rwxrwxrwx 1 root root   50695  4月 24 08:11 ReadMeFirst_Important_Notices_ja.txt
-rwxrwxrwx 1 root root   58932  4月 24 08:11 ReadMeFirst_License.txt
drwx------ 2 root root    4096  8月  4 07:39 chain_certs
drwxrwxrwx 2 root root    4096  8月  4 07:38 code
-rwxrwxrwx 1 root root 1295363  4月 24 08:11 hamcore.se2
-rw------- 1 root root     867  8月  4 07:38 lang.config
drwxrwxrwx 2 root root    4096  8月  4 07:38 lib
-rwxr-xr-x 1 root root 3742580  8月  4 07:38 vpncmd
-rwxr-xr-x 1 root root 3742656  8月  4 07:38 vpnserver

パーミッションを変えておく。

root@raspberrypi:~# cd /usr/local/vpnserver/
root@raspberrypi:/usr/local/vpnserver# chmod 600 *
root@raspberrypi:/usr/local/vpnserver# chmod 700 vpncmd
root@raspberrypi:/usr/local/vpnserver# chmod 700 vpnserver
root@raspberrypi:/usr/local/vpnserver# ls -l
合計 8784
-rw------- 1 root root    1881  4月 24 08:11 Authors.txt
-rw------- 1 root root    2859  4月 24 08:11 Makefile
-rw------- 1 root root   30801  4月 24 08:11 ReadMeFirst_Important_Notices_cn.txt
-rw------- 1 root root   36297  4月 24 08:11 ReadMeFirst_Important_Notices_en.txt
-rw------- 1 root root   50695  4月 24 08:11 ReadMeFirst_Important_Notices_ja.txt
-rw------- 1 root root   58932  4月 24 08:11 ReadMeFirst_License.txt
drw------- 2 root root    4096  8月  4 07:39 chain_certs
drw------- 2 root root    4096  8月  4 07:38 code
-rw------- 1 root root 1295363  4月 24 08:11 hamcore.se2
-rw------- 1 root root     867  8月  4 07:38 lang.config
drw------- 2 root root    4096  8月  4 07:38 lib
-rwx------ 1 root root 3742580  8月  4 07:38 vpncmd
-rwx------ 1 root root 3742656  8月  4 07:38 vpnserver

vpncmdというコマンドツールで、VPNサーバの動作チェックが行えるので、念のため行っておく。ちなみに上で実行可能ファイルを生成した時によく見ると同様のチェックがすでに行われている。

root@raspberrypi:/usr/local/vpnserver# ./vpncmd
vpncmd コマンド - SoftEther VPN コマンドライン管理ユーティリティ
SoftEther VPN コマンドライン管理ユーティリティ (vpncmd コマンド)
Version 4.21 Build 9613   (Japanese)
Compiled 2016/04/24 16:39:47 by yagi at pc30
Copyright (c) SoftEther VPN Project. All Rights Reserved.

vpncmd プログラムを使って以下のことができます。

1. VPN Server または VPN Bridge の管理
2. VPN Client の管理
3. VPN Tools コマンドの使用 (証明書作成や通信速度測定)

1 - 3 を選択: 3

VPN Tools を起動しました。HELP と入力すると、使用できるコマンド一覧が表示できます。

VPN Tools>help
下記の 6 個のコマンドが使用できます:
 About         - バージョン情報の表示
 Check         - SoftEther VPN の動作が可能かどうかチェックする
 MakeCert      - 新しい X.509 証明書と秘密鍵の作成 (1024 bit)
 MakeCert2048  - 新しい X.509 証明書と秘密鍵の作成 (2048 bit)
 TrafficClient - 通信スループット測定ツールクライアントの実行
 TrafficServer - 通信スループット測定ツールサーバーの実行

それぞれのコマンドの使用方法については、"コマンド名 ?" と入力するとヘルプが表示されます。
コマンドは正常に終了しました。

VPN Tools>check
Check コマンド - SoftEther VPN の動作が可能かどうかチェックする
---------------------------------------------------
SoftEther VPN 動作環境チェックツール

Copyright (c) SoftEther VPN Project.
All Rights Reserved.

この動作環境チェックツールを実行したシステムがテストに合格した場合は、SoftEther VPN ソフトウェアが動作する可能性が 高いです。チェックにはしばらく時間がかかる場合があります。そのままお待ちください...

'カーネル系' のチェック中...
              [合格] ○
'メモリ操作系' のチェック中...
              [合格] ○
'ANSI / Unicode 文字列処理系' のチェック中...
              [合格] ○
'ファイルシステム' のチェック中...
              [合格] ○
'スレッド処理システム' のチェック中...
              [合格] ○
'ネットワークシステム' のチェック中...
              [合格] ○

すべてのチェックに合格しました。このシステム上で SoftEther VPN Server / Bridge が正しく動作する可能性が高いと思われます。

コマンドは正常に終了しました。

VPN Tools>exit

すべてのチェックに合格していることを確認する。

ここから、VPNサーバを運用しやすくするため、スタートアップスクリプトに登録する。

root@raspberrypi:/usr/local/vpnserver# touch /etc/init.d/vpnserver
root@raspberrypi:/usr/local/vpnserver# vi /etc/init.d/vpnserver

公式マニュアル通り、以下の内容を記載する。

#!/bin/sh
# chkconfig: 2345 99 01
# description: SoftEther VPN Server
DAEMON=/usr/local/vpnserver/vpnserver
LOCK=/var/lock/subsys/vpnserver
test -x $DAEMON || exit 0
case "$1" in
start)
$DAEMON start
touch $LOCK
;;
stop)
$DAEMON stop
rm $LOCK
;;
restart)
$DAEMON stop
sleep 3
$DAEMON start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0

パーミッションも適切に設定する。

root@raspberrypi:/usr/local/vpnserver# chmod 755 /etc/init.d/vpnserver

chkconfigに登録する。当環境ではchkconfigが入っていなかったのでapt-getでインストールから行った。

root@raspberrypi:~# apt-get install chkconfig
root@raspberrypi:~# chkconfig vpnserver on
insserv: warning: script 'vpnserver' missing LSB tags and overrides

警告が出るが、以下を見る限り大した問題ではなさそうなので、ここでは無視する。
http://www.sssg.org/blogs/hiro345/archives/16237.html

root@raspberrypi:~# chkconfig vpnserver --list
vpnserver                 0:off  1:off  2:on   3:on   4:on   5:on   6:off

これで以下のコマンドでVPNサーバの起動・停止・ステータス確認が正常に行えるようになった。

# service vpnserver start
# service vpnserver stop
# service vpnserver status

vpnserverを起動させておく。

root@raspberrypi:~# service vpnserver start

これでRaspberry PiにSoftEther VPN Serverのインストールが完了した。

Windows 7マシンからSoftEther VPN Serverの設定

Raspberry Pi にインストールしたVPN Serverの設定を行う。
サーバ上で直接vpncmdを使ってもできるのだが、Win/Mac用のGUIの管理用ツールがあるので、各種設定はこちらから行う。
そのため、ここからはWindowsマシンを使って作業する。今回は後にVPNクライアントととしても使うことになるWindows 7端末を利用する。

VPNサーバと同じダウンロードページから softether-vpnserver_vpnbridge-v4.21-9613-beta-2016.04.24-windows-x86_x64-intel.exe をダウンロードする。
http://www.softether.org/5-download
f:id:osn_th:20160806181923p:plain

インストーラを展開し、実行する。
SoftEther VPN サーバー管理マネージャを選択し、次へ
f:id:osn_th:20160806182010p:plain
あとは、ライセンスやインストール先を指定するだけで、基本的に指示通りに「次へ」を選んでいくだけでインストールが進む。
完了し、管理マネージャを起動すると以下のようなGUIのツールが起動する。
「新しい接続設定」をクリックし、さきほどRaspberry Pi にインストールしたSoftEther VPN Serverの設定を行っていく。
f:id:osn_th:20160806181950p:plain
接続設定名はなんでもよいが、今回はraspi-vpnとした。なお、この先いろいろなところで各種設定の名前を入力するが、今回は全てraspi-vpnとしている。
ホスト名はVPN ServerをインストールしたサーバのIPアドレスもしくはホスト名を指定する。
f:id:osn_th:20160806181947p:plain
設定が作成されたので、「接続」する。
f:id:osn_th:20160806181955p:plain
初回ログイン時に管理パスワードの設定をする。
以降は管理ツールにアクセス時にそのパスワードを入力する(ちなみにこれはVPNユーザのパスワードとはまた別で、それはあとで別に設定する)。
f:id:osn_th:20160806181941p:plain
リモートアクセス VPNサーバーにチェックを入れ、次へ
f:id:osn_th:20160806181933p:plain
はい
f:id:osn_th:20160806182012p:plain
仮想HUB名もraspi-vpnにしておく
f:id:osn_th:20160806181958p:plain
固有のダイナミックDNSが割り当てられる。変更も可能なようだが、今回は特に変更せずにそのまま利用する。
f:id:osn_th:20160806182018p:plain
今回はVPN Azureを使ってクラウド経由のVPNを作るため、「VPN Azureを有効にする」にチェックを入れる。
ここで、上のダイナミックDNSに基づいた「現在のVPN Azureホスト名」が実際にVPNクライアントから接続するときのホスト名となるので、控えておいた上で、OK。
f:id:osn_th:20160806182021p:plain
続いてVPNユーザーを作成する。
f:id:osn_th:20160806182004p:plain
f:id:osn_th:20160806182014p:plain
最後に、VPN経由でLANにアクセスするために、ローカルブリッジ設定をする。
この設定を入れないとVPNでつないだときにリンクローカルアドレスが割り当てられてしまい、LAN内のサーバと通信ができない。
この設定を入れることで、VPNセッションをつないだときにLAN内のDHCPサーバ(ルータ)からIPアドレスが割り当てられる。
f:id:osn_th:20160806182034p:plain
仮想HUBに「物理的な既存のLANカードとのブリッジ接続」を選んで、「ローカルブリッジを追加」する。
f:id:osn_th:20160806181930p:plain
追加されたローカルブリッジが動作中になる

f:id:osn_th:20160806181921p:plain
以上で、VPN Server側の設定が完了した!

SoftEther VPN Clientセットアップ

VPNサーバ側のセットアップが完了したので、あとはVPNクライアントをセットアップして実際に接続してみる。
SoftEther VPN Clientもこれまでと同じようにダウンロードする。
f:id:osn_th:20160806182006p:plain
SoftEther VPN Clientをインストールする。
f:id:osn_th:20160806181952p:plain
クライアント側も指示どおりに「次へ」を選んでいくだけで、特に迷うことなくインストールできる。
VPNクライアントが起動したら、「新しい接続設定の作成」を選択する。
f:id:osn_th:20160806182000p:plain
はい
f:id:osn_th:20160806181943p:plain
ここでもraspi-vpnとする。
f:id:osn_th:20160806181927p:plain
つくられたraspi-vpn仮想LANカードをクリック
f:id:osn_th:20160806182024p:plain
さきほど作ったVPNサーバ側のホスト名やVPNユーザ情報など各種設定を入力し、OK。
f:id:osn_th:20160806182027p:plain

ここまででVPNサーバ側およびクライアント側の全設定が完了した。

VPN接続テスト

セットアップしたVPNが正しく動作するか検証する。
ここまで作業は自宅LAN内のルータの無線LANを使ってインターネットに出ていたが、正しくVPN経由で自宅LANにアクセスできることを確認するため、ここからはモバイルルータにネットワークを切り替えて作業する。

作成したVPNクライアントの接続設定を右クリックし、「接続」を選択する。
f:id:osn_th:20160806182031p:plain
VPN上のDHCPサーバからIPアドレスが取得される。
f:id:osn_th:20160806181945p:plain
IPアドレスが割り当てられ、接続完了となればOK。
f:id:osn_th:20160806182037p:plain

ipconfigを打つと、raspi-vpn仮想ネットワークカードに自宅LANのIPアドレスが割り当てられていることが確認できる。

>ipconfig

Windows IP 構成


イーサネット アダプター raspi-vpn - VPN Client:

   接続固有の DNS サフィックス . . . :
   リンクローカル IPv6 アドレス. . . . : xxxx::xxxx:xxxx:xxxx:xxxx%60
   IPv4 アドレス . . . . . . . . . . : 192.168.11.17
   サブネット マスク . . . . . . . . : 255.255.255.0
   デフォルト ゲートウェイ . . . . . : 192.168.11.1

Raspberry Pi 2 (192.168.11.11)にもアクセスできる。

>ping 192.168.11.11

192.168.11.11 に ping を送信しています 32 バイトのデータ:
192.168.11.11 からの応答: バイト数 =32 時間 =1ms TTL=64
192.168.11.11 からの応答: バイト数 =32 時間 =1ms TTL=64
192.168.11.11 からの応答: バイト数 =32 時間 =1ms TTL=64
192.168.11.11 からの応答: バイト数 =32 時間 =1ms TTL=64

192.168.11.11 の ping 統計:
    パケット数: 送信 = 4、受信 = 4、損失 = 0 (0% の損失)、
ラウンド トリップの概算時間 (ミリ秒):
    最小 = 1ms、最大 = 1ms、平均 = 1ms

sshもOK。

>ssh pi@192.168.11.11
pi@192.168.11.11's password:

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sat Aug  6 01:20:30 2016 from 192.168.11.4
pi@raspberrypi:~ $

sshでRaspberry Piにログインもできた。
正しくVPNが構築できているよう!

まとめ

今回はSoftEtherのVPN Azureを使って自宅VPNを構築した。
Raspberry PiをSoftEther VPN Server、Windows 7マシンをSoftEther VPN Clientとして環境構築、設定、疎通まで確認できた。
それほど難しい手順はなく(VPNやネットワークの基礎知識がないと辛いが)、管理ツールも使いやすいのでかなり便利だと思う。クラウドVPNを使うことでグローバルIPアドレスが不要になるなので、自宅ルータにVPN機能がなくても普通のマシンされあればVPN Server化できるので、構築のための敷居はかなり低いと感じた。

以上!