0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WireGuard VPN 構築 (Amazon Linux VPN サーバー + Ubuntu/Windows VPN クライアント)

0
Posted at

WireGuard VPN 構築方法 (Amazon Linux 2023 + Ubuntu/Windows)

1. どのような人におすすめ?

  • 外出先 PC から自宅 PC にアクセスしたい
    • 特に自宅 PC にパブリック IP を割り振れない方
  • 多種多様な OS に対応
  • セットアップが簡単
  • 高速 & 高信頼性
  • VPN サーバーがダウンしても VPN クライアント側から自動再接続
    • 必要ないときは VPN サーバーをシャットダウンさせて必要なときだけ起動する運用ができる
  • 参考文献:

2. ネットワーク図

こういうネットワークを構築することを目指します.

wireguard-vpn.drawio.png

物理 LAN1 ⇔ 物理 LAN2 へ (VPN 越しに) 直接 ping や SSH が通れば成功です.
(ファイアーウォール設定に注意)

3. VPN サーバー構築

今回は AWS EC2 Amazon Linux 2023 上に WireGuard VPN サーバーを導入します.
当然ですが WireGuard VPN サーバーは固定 IP (Elastic IP) を割り当てます.

# パッケージインストール
sudo dnf install -y wireguard-tools
sudo dnf install -y iptables
wg --version

# 鍵ペア作成
wg genkey | tee server_private.key | wg pubkey > server_public.key

# IPフォワーディング有効化 (永続)
echo "net.ipv4.ip_forward = 1" | sudo tee /etc/sysctl.d/99-wireguard.conf
sudo sysctl --system

# サービス有効化 (永続)
sudo systemctl enable wg-quick@wg0
sudo systemctl start wg-quick@wg0

# 設定ファイル編集
sudo vi /etc/wireguard/wg0.conf

/etc/wireguard/wg0.conf はこのように編集します.

[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = { server_private.key }

PostUp = sysctl -w net.ipv4.ip_forward=1
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT
PostUp = iptables -A FORWARD -o wg0 -j ACCEPT
PostUp = iptables -t nat -A POSTROUTING -o ens5 -j MASQUERADE

PostDown = iptables -D FORWARD -i wg0 -j ACCEPT
PostDown = iptables -D FORWARD -o wg0 -j ACCEPT
PostDown = iptables -t nat -D POSTROUTING -o ens5 -j MASQUERADE

# (以下は VPN クライアントごとに追加していきます)

[Peer]
PublicKey = { client_public.key }
AllowedIPs = 10.0.0.2/32  # (← IP は適当に空いているものを割り振ります)

[Peer]
PublicKey = { client_public.key }
AllowedIPs = 10.0.0.3/32  # (← IP は適当に空いているものを割り振ります)

...

:warning: ネットワークアダプターは Amazon Linux 2023 の場合は ens5 ですが, 環境依存なので都度変更が必要です.

sudo systemctl restart wg-quick@wg0

4. VPN クライアント構築

4.1. VPN クライアント構築 (Ubuntu)

sudo apt install -y wireguard-tools

sudo su
cd /etc/wireguard/
wg genkey | tee client_private.key | wg pubkey > client_public.key
cat client_public.key

(Ctrl+D)

sudo vi /etc/wireguard/wg0.conf

/etc/wireguard/wg0.conf

[Interface]
PrivateKey = { client_private.key  }
Address = 10.0.0.2/24  # 適当な空いている VPN IP アドレスを割り振る

[Peer]
PublicKey = { server_public.key }
Endpoint = { VPN サーバーアドレス }:51820
AllowedIPs = 10.0.0.0/24
PersistentKeepalive = 25
# サービス有効化 (永続)
sudo systemctl enable wg-quick@wg0.service
sudo systemctl start wg-quick@wg0.service

4.2. VPN クライアント構築 (Windows)

Windows は GUI で設定ができます.
自動でサービスとして登録され, OS 起動時に WireGuard も起動します.
秘密鍵 & 公開鍵は空のトンネルの追加時に自動で生成されます.
基本的に設定ファイルの書き方は Ubuntu と同じです.

image.png

[Interface]
PrivateKey = { 秘密鍵は自動で生成されます }
Address = 10.0.0.3/24  # 適当な空いている VPN IP アドレスを割り振る

[Peer]
PublicKey = { server_public.key }
Endpoint = { VPN サーバーアドレス }:51820
AllowedIPs = 10.0.0.0/24
PersistentKeepalive = 25

4.3. ハマりポイント

ファイアーウォールは必要に応じて設定してください.
特に Windows はプライベートネットワーク, ドメインネットワーク, パブリックネットワークという概念がありファイアーウォールの設定が分かりずらい…ので注意.

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?