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?

Proxmox VE の LXC に WireGuard VPN サーバを構築する

0
Posted at

はじめに

自宅の Proxmox VE 上の LXC コンテナに WireGuard VPN サーバを構築し、外出先から自宅ネットワークへ安全にアクセスできるようにした手順をまとめます。

当初は Proxmox VE Community Scripts(旧 tteck スクリプト)によるワンコマンドデプロイを試みましたが、テンプレートの互換性問題に遭遇したため、手動で LXC を作成して WireGuard をインストールする方法に切り替えました。

環境

項目 内容
Proxmox VE 8.3.0(Kernel: 6.8.12-4-pve)
LXC OS Debian 12(bookworm)
WireGuard 1.0.20210914
コンテナ IP 192.168.11.7/24
WireGuard サブネット 10.0.0.0/24
ルータ BBIQ 光電話無線ルータ XS-5A-01

前提条件

  • Proxmox VE が稼働していること
  • ルータで UDP 51820 を WireGuard コンテナの IP(例: 192.168.11.7)へポートフォワードしていること
  • グローバル IP(固定 or DDNS)を把握していること

手順

1. LXC コンテナの作成

Proxmox ノードのシェルで以下を実行します。

pct create 110 local:vztmpl/debian-12-standard_12.7-1_amd64.tar.zst \
  --hostname wireguard \
  --unprivileged 1 \
  --storage local-zfs \
  --rootfs local-zfs:4 \
  --cores 1 \
  --memory 512 \
  --net0 name=eth0,bridge=vmbr0,ip=192.168.11.7/24,gw=192.168.11.1 \
  --features nesting=1 \
  --timezone Asia/Tokyo \
  --protection 1 \
  --onboot 1

2. TUN/TAP デバイスの許可

WireGuard がトンネルデバイスを使用するため、コンテナ設定に TUN/TAP の許可を追加します。

cat >> /etc/pve/lxc/110.conf << 'EOF'
lxc.cgroup2.devices.allow: c 10:200 rwm
lxc.mount.entry: /dev/net/tun dev/net/tun none bind,create=file
EOF

3. コンテナの起動と WireGuard インストール

pct start 110
pct enter 110

コンテナ内で以下を実行します。

apt update && apt install -y wireguard iptables qrencode

4. サーバ鍵ペアの生成

wg genkey | tee /etc/wireguard/server_private.key | wg pubkey > /etc/wireguard/server_public.key
chmod 600 /etc/wireguard/server_private.key

5. クライアント鍵ペアの生成

wg genkey | tee /etc/wireguard/client1_private.key | wg pubkey > /etc/wireguard/client1_public.key
chmod 600 /etc/wireguard/client1_private.key

6. サーバ設定ファイルの作成

SERVER_PRIV=$(cat /etc/wireguard/server_private.key)
CLIENT_PUB=$(cat /etc/wireguard/client1_public.key)

cat > /etc/wireguard/wg0.conf << EOF
[Interface]
PrivateKey = ${SERVER_PRIV}
Address = 10.0.0.1/24
ListenPort = 51820
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

[Peer]
PublicKey = ${CLIENT_PUB}
AllowedIPs = 10.0.0.2/32
EOF

7. IP フォワーディングの有効化と WireGuard 起動

echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
sysctl -p
systemctl enable --now wg-quick@wg0

動作確認:

wg show

以下のように表示されれば成功です。

interface: wg0
  public key: <サーバ公開鍵>
  private key: (hidden)
  listening port: 51820

peer: <クライアント公開鍵>
  allowed ips: 10.0.0.2/32

8. クライアント設定ファイルの作成

CLIENT_PRIV=$(cat /etc/wireguard/client1_private.key)
SERVER_PUB=$(cat /etc/wireguard/server_public.key)

cat > /etc/wireguard/client1.conf << EOF
[Interface]
PrivateKey = ${CLIENT_PRIV}
Address = 10.0.0.2/24
DNS = 8.8.8.8

[Peer]
PublicKey = ${SERVER_PUB}
Endpoint = <グローバルIP or DDNS>:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
EOF

9. クライアントへの設定配布

生成された client1.conf を PC やスマホの WireGuard クライアントにインポートします。

  • ファイル転送: コンテナからホストへ pct pull 110 /root/client1.conf /root/client1.conf で取り出し、SCP 等でクライアントへ転送
  • QR コード: qrencode -t ansiutf8 < /etc/wireguard/client1.conf でターミナルに表示(ターミナルの幅が狭いと崩れる場合があります。PNG 出力は qrencode -t png -o /root/client1.png < /etc/wireguard/client1.conf

クライアントアプリで「ファイルからインポート」し、接続を有効化すれば完了です。

10. 自動起動の設定

Proxmox ホスト再起動時にコンテナが自動起動するよう設定します。PVE ホストのシェルで実行します。

pct set 110 --onboot 1

備考:Community Scripts(Debian 13 テンプレート)での失敗

当初は Proxmox VE Helper Scripts(Community Scripts)を使用してワンコマンドでデプロイを試みました。

bash -c "$(wget -qLO - https://github.com/community-scripts/ProxmoxVE/raw/main/ct/wireguard.sh)"

Advanced Install を選択して各種設定を進めましたが、スクリプトが自動的に Debian 13(Trixie)テンプレートを選択し、Proxmox VE 8.3.0 の LXC スタックでは非対応のため以下のエラーで失敗しました。

unable to create CT 110 - unsupported debian version '13.1'

スクリプト内に OS バージョンを選択する項目が存在しなかったため、手動で Debian 12 テンプレートを指定して LXC を作成するワークアラウンドに切り替えました。Proxmox VE を最新版にアップグレードすれば解消する可能性がありますが、本番環境への影響を考慮して今回は見送りました。

まとめ

Proxmox VE の LXC 上に WireGuard VPN サーバを構築しました。Community Scripts のワンコマンドデプロイは便利ですが、Proxmox と OS テンプレートのバージョン互換に注意が必要です。手動構築でも手順自体はシンプルで、LXC 作成 → TUN 許可 → WireGuard インストール → 鍵生成 → 設定ファイル作成の流れで完了します。

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?