Raspberry Pi 2 Model B で L2TP/IPSec VPNサーバを構築してみた(その1)

2019年11月5日

本記事は、Raspberry Pi 3に対応しています。

Raspberry Piは低消費電力のため、常時稼動させるサーバとしては最適です。
今回は、Raspberry Pi 2 Model B で、L2TP/IPSecのVPNサーバを構築します。

目的

  • 外出先からスマートフォン等で自宅のネットワークに接続し、安全にファイルサーバ上のデータを扱える。
  • VPN接続でRaspberry Piに接続できる。
  • Raspberry Pi上のファイルサーバに接続できる。

課題

  1. どのようにセキュリティを担保するか?
  2. プロバイダから割り当てられているグローバルIPアドレスが変わる可能性がある、どのように自宅のグローバルIPアドレスを知るか?
  3. Linuxの仕様で、VPN接続中は、VPNサーバそのものと通信できないらしい。(Raspberry PiにVPN接続しながらRaspberry Pi上のデータにアクセスできない)

課題に対する解決案

セキュリティの担保

L2TP/IPSec もしくは SSL-VPN を使用してセキュリティを担保する。

⇒ L2TP/IPSecに対応しているSoftEtherVPNを利用する。

IPアドレスの問題

ダイナミックDNSサービスを使う。

⇒ SoftEtherの運営元がDDNSサービスを提供してくれている。

Linuxの仕様の問題

OSのIPアドレスを、物理デバイス(eth0)ではなく、ブリッジデバイス(br0)に割り当てて、br0にeth0とVPN仮想HUBをブリッジさせることで回避可能。

http://www2.softether.jp/jp/vpn3/manual/web/3-6.aspx#vpn_3_6_11

Linux オペレーティングシステム内部での制限事項により、VPN 側 (仮想 HUB 側) からローカルブリッジしている LAN カードに割り当てられる IP アドレスに対して通信を行うことはできません。この制限は PacketiX VPN が原因ではなく、Linux の内部構造に原因があります。もし VPN 側 (仮想 HUB 側) から Linux でローカルブリッジに使用しているコンピュータ本体と、何らかの通信を行いたい場合 (たとえば VPN Server / VPN Bridge サービスと HTTP サーバーサービスを両方動作させており、VPN 側からもサーバーサービスにアクセスさせたい場合) は、ローカルブリッジ用の LAN カードを用意して接続し、その LAN カードと既存の LAN カードの両方を物理的に同じセグメントに接続してください

要するに、OSのIPアドレスをブリッジデバイス(br0)に付与して、接続したい物理(eth0)および仮想HUB(vlan)をブリッジデバイス(br0)に繋げてやる(ブリッジする)ことで回避できるわけですね。

絵で描くとこんな感じです。
bridge

ということで、我が家の低消費電力のVPN Server環境は以下に決まりました。

環境

  • ハードウェア:Raspberry Pi 2 Model B
  • OS:Raspbian jessie
  • VPNサーバソフトウェア:SoftEther VPN

構築手順

SoftEtherはsudoコマンドではなくrootユーザにてインストール作業をすることが推奨のため、rootにスイッチしておきましょう。

$ su -

以後のコマンドはすべてrootで実行します。

ブリッジデバイスを取り扱うパッケージのインストール

前述のとおり、Linuxの仕様の問題を回避するために、ブリッジデバイスbr0にIPアドレスを付与する必要があるわけですが、デフォルトではブリッジデバイスを扱うパッケージがインストールされていないため、インストールします。

# apt-get install bridge-utils

物理デバイスに設定済みの固定IPアドレス設定を解除

ブリッジデバイスbr0にIPアドレスを付与するために、既にeth0等に付与している固定IPアドレス設定を削除します。

参考:Raspbian(jessie)の固定IPアドレス設定方法

削除してもいいですが、以下のように、設定個所を全てコメントアウトするだけでも良いです。

# vi /etc/dhcpcd.conf

#interface eth0
#static ip_address=192.168.0.20/24
#static routers=192.168.0.1
#static domain_name_servers=192.168.0.1

ブリッジデバイスへの固定IPアドレス設定

/etc/network/interface ファイルにブリッジデバイスbr0への固定IPアドレス設定と、物理デバイスeth0のブリッジ接続設定を記載します。

192.168.0.20/24 の固定IPアドレスを付与する例です。"★"は全てコメント

# vi /etc/network/interfaces
iface eth0 inet manual★eth0の設定はこの一行だけでOK

auto br0★ここからbr0の設定
iface br0 inet static★固定IPアドレスだと宣言する
address 192.168.0.20★設定したいIPアドレス
netmask 255.255.255.0★サブネットマスク
network 192.168.0.0ネットワークアドレス
broadcast 192.168.0.255★ブロードキャストアドレス
gateway 192.168.0.1★デフォルトゲートウェイ
bridge_ports eth0★eth0とブリッジする設定
bridge_maxwait 10★ブリッジの最大wait

設定が終わったら再起動します。

# reboot

設定確認

再起動後、br0に固定IPアドレスが設定されていることを確認します。
eth0側は169.254.xx.xx等のAPIPAアドレスで問題なしです。

# ifconffig -a
br0       Link encap:イーサネット  ハードウェアアドレス xx:xx:xx:xx:xx:xx
inetアドレス:192.168.0.20 ブロードキャスト:192.168.0.255  マスク:255.255.255.0
inet6アドレス: xxxx::xxxx:xxxx:xxxx:xxxx/64 範囲:リンク
UP BROADCAST RUNNING MULTICAST  MTU:1500  メトリック:1
RXパケット:167 エラー:0 損失:0 オーバラン:0 フレーム:0
TXパケット:248 エラー:0 損失:0 オーバラン:0 キャリア:0
衝突(Collisions):0 TXキュー長:0
RXバイト:24740 (24.1 KiB)  TXバイト:34806 (33.9 KiB)

eth0      Link encap:イーサネット  ハードウェアアドレス xx:xx:xx:xx:xx:xx
inetアドレス:169.254.11.255 ブロードキャスト:169.254.255.255  マスク:255.255.0.0
UP BROADCAST RUNNING MULTICAST  MTU:1500  メトリック:1
RXパケット:167 エラー:0 損失:0 オーバラン:0 フレーム:0
TXパケット:269 エラー:0 損失:0 オーバラン:0 キャリア:0
衝突(Collisions):0 TXキュー長:1000
RXバイト:24740 (24.1 KiB)  TXバイト:42406 (41.4 KiB)

SoftEtherのLinuxパッケージをダウンロード

公式サイトからLinux ARM EABI 32bit の 最新版をダウンロードします。
http://www.softether-download.com/ja.aspx?product=softether

パッケージの選択は以下の画像を参考にしてください。
softether-download

ダウンロードしたパッケージを /root ディレクトリに配置します。(任意のディレクトリでいいですが、以下、/root に配置したとして解説します)

SoftEtherのパッケージを解凍

root@raspberry:/# cd /root
root@raspberry:~# ls
softether-vpnserver-v4.19-9599-beta-2015.10.19-linux-arm_eabi-32bit.tar.gz

root@raspberry:~# tar zxf softether-vpnserver-v4.19-9599-beta-2015.10.19-linux-arm_eabi-32bit.tar.gz

root@raspberry:~# ls -l
合計 5532
-rw-r--r-- 1 pi   pi   5659736  1月 18 02:28 softether-vpnserver-v4.19-9599-beta-2015.10.19-linux-arm_eabi-32bit.tar.gz
drwxrwxrwx 4 root root    4096 10月 19 20:43 vpnserver★これが解凍された

makeする

解凍されたvpnserverディレクトリに移動して、makeしましょう。

root@raspberry:~# cd vpnserver/

root@raspberry:~/vpnserver# ls
Authors.txt                  ReadMeFirst_License.txt
Makefile                  code
ReadMeFirst_Important_Notices_cn.txt  hamcore.se2
ReadMeFirst_Important_Notices_en.txt  lib
ReadMeFirst_Important_Notices_ja.txt

root@raspberry:~/vpnserver# ./make

以下のように聞かれたら、1を入力します。

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

SoftEther VPN Server (Ver 4.19, Build 9599, 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★

EURAを読みましょう。

SoftEther VPN Server, Client and Bridge are free software, and released as open-source. You can redistribute them and/or modify them under the terms of the GNU General Public License version 2 as published by the Free Software Foundation.

Copyright (c) 2012-2015 Daiyuu Nobori.
Copyright (c) 2012-2015 SoftEther Project at University of Tsukuba, Japan.
Copyright (c) 2012-2015 SoftEther Corporation.
All Rights Reserved.
http://www.softether.org/

~~中略~~

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★

もう一度1を入力して同意しましょう。

Did you agree the License Agreement ?

1. Agree
2. Do Not Agree

Please choose one of above number:
1★

以下のように、makeが進みます。

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.19 Build 9599   (Japanese)
Compiled 2015/10/19 20:28:20 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 on the other Windows PC in order to configure the SoftEther VPN Server remotely.
--------------------------------------------------------------------

make[1]: Leaving directory '/root/vpnserver'★これが出ればmake完了。

makeが完了し、コンパイルされました。

ファイルを確認してみましょう。

root@raspberry:~/vpnserver# ls -l
合計 8824
-rwxrwxrwx 1 root root    1881 10月 19 20:43 Authors.txt
-rwxrwxrwx 1 root root    2097 10月 19 20:43 Makefile
-rwxrwxrwx 1 root root   30801 10月 19 20:43 ReadMeFirst_Important_Notices_cn.txt
-rwxrwxrwx 1 root root   36297 10月 19 20:43 ReadMeFirst_Important_Notices_en.txt
-rwxrwxrwx 1 root root   50695 10月 19 20:43 ReadMeFirst_Important_Notices_ja.txt
-rwxrwxrwx 1 root root   58932 10月 19 20:43 ReadMeFirst_License.txt
drwx------ 2 root root    4096  1月 18 11:31 chain_certs
drwxrwxrwx 2 root root    4096  1月 18 11:31 code
-rwxrwxrwx 1 root root 1295107 10月 19 20:43 hamcore.se2
-rw------- 1 root root     867  1月 18 11:31 lang.config
drwxrwxrwx 2 root root    4096  1月 18 11:30 lib
-rwxr-xr-x 1 root root 3761848  1月 18 11:31 vpncmd
-rwxr-xr-x 1 root root 3761924  1月 18 11:31 vpnserver

適切なディレクトリにmoveする

一つ上のディレクトリに移動し、vpnserverディレクトリを/usr/localにmoveしましょう。

root@raspberry:~/vpnserver# cd ..

root@raspberry:~# ls
softether-vpnserver-v4.19-9599-beta-2015.10.19-linux-arm_eabi-32bit.tar.gz
vpnserver

root@raspberry:~# mv vpnserver /usr/local/

パーミッション設定

基本的に600に、vpncmdとvpnserverは700に、パーミッションを変更します。

# cd /usr/local/vpnserver/

# ls -l
合計 8824
-rwxrwxrwx 1 root root    1881 10月 19 20:43 Authors.txt
-rwxrwxrwx 1 root root    2097 10月 19 20:43 Makefile
-rwxrwxrwx 1 root root   30801 10月 19 20:43 ReadMeFirst_Important_Notices_cn.txt
-rwxrwxrwx 1 root root   36297 10月 19 20:43 ReadMeFirst_Important_Notices_en.txt
-rwxrwxrwx 1 root root   50695 10月 19 20:43 ReadMeFirst_Important_Notices_ja.txt
-rwxrwxrwx 1 root root   58932 10月 19 20:43 ReadMeFirst_License.txt
drwx------ 2 root root    4096  1月 18 11:31 chain_certs
drwxrwxrwx 2 root root    4096  1月 18 11:31 code
-rwxrwxrwx 1 root root 1295107 10月 19 20:43 hamcore.se2
-rw------- 1 root root     867  1月 18 11:31 lang.config
drwxrwxrwx 2 root root    4096  1月 18 11:30 lib
-rwxr-xr-x 1 root root 3761848  1月 18 11:31 vpncmd
-rwxr-xr-x 1 root root 3761924  1月 18 11:31 vpnserver

# pwd
/usr/local/vpnserver

# chmod 600 *

# chmod 700 vpncmd

# chmod 700 vpnserver

# ls -l
合計 8824
-rw------- 1 root root    1881 10月 19 20:43 Authors.txt
-rw------- 1 root root    2097 10月 19 20:43 Makefile
-rw------- 1 root root   30801 10月 19 20:43 ReadMeFirst_Important_Notices_cn.txt
-rw------- 1 root root   36297 10月 19 20:43 ReadMeFirst_Important_Notices_en.txt
-rw------- 1 root root   50695 10月 19 20:43 ReadMeFirst_Important_Notices_ja.txt
-rw------- 1 root root   58932 10月 19 20:43 ReadMeFirst_License.txt
drw------- 2 root root    4096  1月 18 11:31 chain_certs
drw------- 2 root root    4096  1月 18 11:31 code
-rw------- 1 root root 1295107 10月 19 20:43 hamcore.se2
-rw------- 1 root root     867  1月 18 11:31 lang.config
drw------- 2 root root    4096  1月 18 11:30 lib
-rwx------ 1 root root 3761848  1月 18 11:31 vpncmd
-rwx------ 1 root root 3761924  1月 18 11:31 vpnserver

起動スクリプトの作成

vpnserver という起動スクリプトを作ります。SoftEtherの仮想HUBを作成した後に、ブリッジデバイスとブリッジする設定を追加しないといけないので、現時点ではとりあえず以下のとおりコピペして作ってください。

# vi /etc/init.d/vpnserver

#!/bin/sh
### BEGIN INIT INFO
# Provides:       vpnserver
# Required-Start: $local_fs $network
# Required-Stop:  $local_fs $network
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Description:    SoftEther VPN Server
### END INIT INFO

DAEMON=/usr/local/vpnserver/vpnserver
LOCK=/var/lock/vpnserver

vpn_start() {
${DAEMON} start
}

vpn_stop() {
${DAEMON} stop
}

test -x ${DAEMON} || exit 0

case "$1" in
start)
vpn_start
touch ${LOCK}
;;
stop)
vpn_stop
rm ${LOCK}
;;
restart)
vpn_stop
sleep 3
vpn_start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac

exit 0

起動スクリプトをchkconfrigに登録する。

chkconfigをインストールしていない場合は、apt-get install chkconfig コマンドでインストールしてください。

vpnserver という起動スクリプトをchkconfigに登録します。

# chkconfig --add vpnserver

vpnserverを起動する

Raspbian jessie を再起動するか、サービスを起動してください。

OSを再起動する場合

# reboot

vpnserverサービスを起動する場合

# /etc/init.d/vpnserver start

 

ここまでで、一旦Linux側(Raspbian jessie)側での操作は終わりです。(最後にまた少し操作しますが…)

次は SoftEther VPN Serverの設定に入ります。

Raspberry Pi 2 Model B で L2TP/IPSec VPNサーバを構築してみた(その2)に続きます。