GCEの無料枠を活用して安価なVPNサーバを構築する③

今回はGCEで作成したVMインスタンスにSoftEther VPN Serverをインストールする手順について説明します。

SSH接続

VMインスタンスにインストールしたDebianにSSH接続します。
Tera Term等のアプリケーションで接続するのが一般的かと思いますが、GCEではブラウザからコンソールを開くことができます。
今回はブラウザからアクセスして作業しました。

必要なソフトウェアとライブラリをインストール

VPN Serverをインストールする前に、下記の通りのソフトウェアとライブラリをインストールしておく必要があります。

7.3 Linux へのインストールと初期設定 - SoftEther VPN プロジェクト

  • gcc ソフトウェア
  • binutils ソフトウェア
  • パッケージファイル展開用の tar や gzip などのソフトウェア
  • chkconfig システムユーティリティ
  • cat や cp などの基本的なファイル操作ユーティリティ
  • 日本語環境で使用するための EUC-JP や UTF-8 などのコードページテーブル
  • libc (glibc) ライブラリ
  • zlib ライブラリ
  • openssl ライブラリ
  • readline ライブラリ
  • ncurses ライブラリ
  • pthread ライブラリ

これらに対応したパッケージを調べるのも大変なので、下記のコマンドで一括インストールするのをおすすめします。

SoftEtherVPN/BUILD_UNIX.md at master · SoftEtherVPN/SoftEtherVPN

sudo apt -y install cmake gcc g++ libncurses5-dev libreadline-dev libssl-dev make zlib1g-dev

また、VPN Serverをwgetで取得するため、wgetパッケージもインストールしておきます。

sudo apt -y install wget

VPN Serverのダウンロード

64bit Linuxに対応したVPN Serverをダウンロードします。
ダウンロードURLは下記のように、ダウンロードページから取得します。

wget https://jp.softether-download.com/files/softether/v4.34-9745-rtm-2020.04.05-tree/
Linux/SoftEther_VPN_Server/64bit_-_Intel_x64_or_AMD64/softether-vpnserver-v4.34-9745-rtm-2020.04.05-linux-x64-64bit
.tar.gz

ダウンロードしたファイルを解凍します。

tar xzvf softether-vpnserver-v4.34-9745-rtm-2020.04.05-linux-x64-64bit.tar.gz

VPN Serverのインストール

下記コマンドを順に実行してインストールします。
sudo make実行後3回ほど利用規約の確認を促されますが、全て1を入力して続行します。

cd vpnserver/
sudo make
sudo su -
cd /home/<ユーザ名>
mv vpnserver /usr/local
cd /usr/local/vpnserver/
chmod 600 *
chmod 700 vpncmd
chmod 700 vpnserver

自動起動の設定

「softether vpn systemd」などで検索すると多くの例が見つかりますので、それらを参考に自動起動スクリプトを記述します。
スクリプトを記述したら、設定をリロードして起動します。

systemctl daemon-reload
systemctl enable vpnserver.service
systemctl start vpnserver.service

これで、SoftEther VPN Serverのインストールが完了しました。

vpnserver.serviceの記述例

vi /etc/systemd/system/vpnserver.service
[Unit]
Description=SoftEther VPN Server
After=network.target network-online.target

[Service]
ExecStart=/usr/local/vpnserver/vpnserver start
ExecStop=/usr/local/vpnserver/vpnserver stop
Type=forking
RestartSec=3s

[Install]
WantedBy=multi-user.target

管理者パスワードの設定

ここからは管理ソフト(SoftEther VPN Client)から設定を行いますが、その前に管理者パスワードを設定しておきます。
(以前Raspberry Piで構築した時は管理ツールからでも設定できたような気がしますが、参考ページの手順に従いました。)

./vpncmd
vpncmd command - SoftEther VPN Command Line Management Utility
SoftEther VPN Command Line Management Utility (vpncmd command)
Version 4.34 Build 9745   (English)
Compiled 2020/04/05 23:39:56 by buildsan at crosswin
Copyright (c) SoftEther VPN Project. All Rights Reserved.
By using vpncmd program, the following can be achieved. 
1. Management of VPN Server or VPN Bridge 
2. Management of VPN Client
3. Use of VPN Tools (certificate creation and Network Traffic Speed Test Tool)
Select 1, 2 or 3: 1
Specify the host name or IP address of the computer that the destination VPN Server or VPN Bridge is operating on. 
By specifying according to the format 'host name:port number', you can also specify the port number. 
(When the port number is unspecified, 443 is used.)
If nothing is input and the Enter key is pressed, the connection will be made to the port number 8888 of localhost 
(this computer).
Hostname of IP Address of Destination: 
If connecting to the server by Virtual Hub Admin Mode, please input the Virtual Hub name. 
If connecting by server admin mode, please press Enter without inputting anything.
Specify Virtual Hub Name: 
Connection has been established with VPN Server "localhost" (port 443).
You have administrator privileges for the entire VPN Server.
VPN Server>ServerPasswordSet <パスワード>
ServerPasswordSet command - Set VPN Server Administrator Password
The command completed successfully.

VPN Server>exit

管理ソフトでの設定

SoftEther VPN Server Manager for Windowsをダウンロードしてインストールします。

VPNサーバへ接続する

管理ソフトを起動し、「新しい接続設定」ボタンから接続先を登録します。

ホスト名にVMインスタンスに設定した外部静的IPアドレスを指定し、管理パスワードに上記で設定した管理者パスワードを指定します。
接続先を登録後、上記画面で「接続」ボタンを押します。

VPNサーバの設定

自動的に表示された画面で、いくつか設定をしていきます。

「SoftEher VPN Server/Bridge簡易セットアップ」画面で、「リモートアクセスVPNサーバー」にチェックを入れます。

仮想HUB名には任意の名前を指定します。

今回DDNS機能は使用しないので「ダイナミックDNS機能」画面では特に設定の必要はなく、そのまま「閉じる」ボタンを押して画面を閉じます。

「IPsec/L2TP/EtherIP/L2TPv3サーバー機能の設定」画面で、「L2TPサーバー機能を有効にする(L2TP over IPsec)」にチェックを入れます。
IPsec事前共有鍵に任意の値を設定します。

Azureサービスは使用しないので、「VPN Azureを無効にする」を選択します。

VPNを使用するユーザのアカウントを作成する

「ユーザーを作成する」ボタンから、VPNを使用するユーザを作成します。

ユーザー名とパスワードを設定します。

Secure NATの有効化

「VPNの管理」画面で、「仮想NATおよび仮想DHCPサーバー機能」ボタンを押します。

「SecureNAT機能を有効にする」ボタンを押します。

これで設定が完了しました。
あとはスマホなどでVPNサーバに接続できるか確認しましょう。

実際に使用した時の料金

VPNサーバ構築後の翌日、宿泊先に一泊しVPNサーバ経由で無料Wi-Fiを利用しました。
接続した端末はAndroidタブレットとChromebookで、主に動画サイトではない一般のwebページを閲覧しました。
後日GCPのコンソールを確認すると、かかった料金は0.02円でした。
実際の請求予定の金額は0円なので、無料でVPNを利用できたことになります。

参考