😺
AWS EC2+SoftEtherでVPN構築してみた
はじめに
こんにちは。ワンフレーム株式会社でエンジニアをしている天野です。
接続元のIPアドレスでアクセス制限をかけたいことってありますよね。
しかし、リモートワークが当たり前になっている昨今、接続元IPアドレスは様々になります。
それでも接続元IPアドレスを固定したい!
そんな時はクライアントとサーバーの間に中間サーバを立ててIPアドレスを固定化してみましょう。
今回はAWS上でEC2の中にVPNサーバ(SoftEther)を構築してみたので、その手順をご紹介します。
環境構築
以下の経路で接続できるように構築します。
- MacOS Sonoma 14.7.3
- Amazon Linux 2023 AMI 64ビット(Arm)
- SoftEther VPN Server
EC2インスタンスの作成
新規インスタンスを作成し、以下のように入力or選択
名前とタグ
項目 | 設定 | 備考 |
---|---|---|
名前 | SoftEtherVPN |
アプリケーションおよびOSイメージ(Amazon マシンイメージ)
項目 | 設定 | 備考 |
---|---|---|
AMI | Amazon Linux 2023 AMI | |
アーキテクチャ | 64ビット(Arm) |
インスタンスタイプ
項目 | 設定 | 備考 |
---|---|---|
インスタンスタイプ | t4g.micro |
キーペア(ログイン)
項目 | 設定 | 備考 |
---|---|---|
キーペア | 新しいキーペアの作成 | セッションマネージャーを使用するので使わないが、わかるように名前をつけておく |
ネットワーク設定
項目 | 設定 | 備考 |
---|---|---|
ファイアウォール(セキュリティグループ) | セキュリティグループを作成 | |
SSHトラフィックを許可 | 無効 | |
インターネットからのHTTPSトラフィックを許可 | 無効 | |
インターネットからのHTTPトラフィックを許可 | 無効 |
ストレージを設定
項目 | 設定 | 備考 |
---|---|---|
サイズ | 8GiB | |
Vol.タイプ | gp3 |
高度な設定
デフォルトから変更しない(項目が多いため割愛)
概要
項目 | 設定 | 備考 |
---|---|---|
インスタンス数 | 1 |
環境構築
キーペアを使ってSSHで接続して・・・とかやりたくないので、今回はセッションマネージャーを使って接続します。
インストールされているパッケージのアップデート
sudo yum update -y
タイムゾーンの設定
sudo timedatectl set-timezone Asia/Tokyo
SoftEther VPN Serverのビルド
最新のRTMバージョンを使用します(2025/02/04時点ではv4.42-9798)
ビルド準備
sudo yum install -y gcc
ビルド
cd /usr/local/src/
sudo curl -L -O https://jp.softether-download.com/files/softether/v4.42-9798-rtm-2023.06.30-tree/Linux/SoftEther_VPN_Server/64bit_-_ARM_64bit/softether-vpnserver-v4.42-9798-rtm-2023.06.30-linux-arm64-64bit.tar.gz
sudo tar zxvf softether-vpnserver-v4.42-9798-rtm-2023.06.30-linux-arm64-64bit.tar.gz
cd vpnserver/
make
言語設定を日本語に変更
vi lang.config # en -> jaに変更する
コマンドを/usr/local/binに移動
sudo mv vpnserver /usr/local/bin/
sudo mv vpncmd /usr/local/bin/
sudo mv hamcore.se2 /usr/local/bin/
デーモン化
vpnserverをデーモン化し、自動起動するように設定します。
起動・停止スクリプト作成
以下の内容で/opt/vpnserver.sh
を作成します。
#!/bin/sh
# chkconfig: 2345 99 01
# description: SoftEther VPN Server
DAEMON=/usr/local/bin/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
デーモンの設定ファイル作成
以下の内容で/etc/systemd/system/vpnserver.service
を作成します。
[Unit]
Description = vpnserver daemon
[Service]
ExecStart = /opt/vpnserver.sh start
ExecStop = /opt/vpnserver.sh stop
ExecReload = /opt/vpnserver.sh restart
Restart = always
Type = forking
[Install]
WantedBy = multi-user.target
自動起動を有効化
sudo systemctl enable vpnserver
起動
sudo systemctl start vpnserver
接続設定
1
を入力
vpncmdでプロンプト起動し、sh-5.2$ sudo vpncmd
vpncmd command - SoftEther VPN Command Line Management Utility
SoftEther VPN Command Line Management Utility (vpncmd command)
Version 4.42 Build 9798 (English)
Compiled 2023/06/30 11:06:58 by buildsan at crosswin with OpenSSL 3.0.9
Copyright (c) 2012-2023 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
接続先はlocalhostでEnter
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: localhost
仮想ハブ名は空欄でEnter
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 "x.x.x.x" (port 5555).
You have administrator privileges for the entire VPN Server.
仮想ハブを作成
VPN Server>HubCreate testvpn /PASSWORD:password
HubCreate command - Create New Virtual Hub
The command completed successfully.
L2TP over IPSecの有効化
VPN Server>IPSecEnable
IPsecEnable command - Enable or Disable IPsec VPN Server Function
Enable L2TP over IPsec Server Function (yes / no): yes
Enable Raw L2TP Server Function (yes / no): no
Enable EtherIP / L2TPv3 over IPsec Server Function (yes / no): no
Pre Shared Key for IPsec (Recommended: 9 letters at maximum): {共有シークレット}
Default Virtual HUB in a case of omitting the HUB on the Username: {仮想ハブ名}
The command completed successfully.
ユーザーを作成
UserCreate vpn /GROUP:none /REALNAME:"VPN User" /NOTE:""
UserPasswordSet vpn /PASSWORD:vpnpassword
仮想DHCPの有効化(仮想ハブに対して実行)
SecureNatEnable
IP転送の有効化
VPNサーバーを経由してアクセスさせるために、IP転送を有効化します。
カーネルの設定を有効化
sudo sysctl -w net.ipv4.ip_forward=1
カーネルの設定を永続化
永続化するには/etc/sysctl.conf
に以下を追記
net.ipv4.ip_forward = 1
その後、反映
sudo sysctl -p
iptablesのインストール
sudo yum install -y iptables-services
iptablesの設定
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables-save | sudo tee /etc/sysconfig/iptables
iptablesの起動
sudo systemctl start iptables
iptablesの設定を永続化
sudo systemctl enable iptables
疎通確認
Macのクライアント設定
VPN接続設定
- システム環境設定の左ペインでVPNを選択し、「VPN構成を追加 > L2TP over IPsec」をクリック
- モーダルで以下を設定
- モーダルの左ペインで「オプション」を選択し、「すべてのトラフィックをVPN接続経由で送信」を有効化
- 「作成」ボタンをクリック
疎通確認実施
- 作成したVPNをONにする
- Webブラウザなどで接続元IPアドレスを確認してみる
Discussion