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サーバーを5分で構築する最小構成

Posted at

WireGuard VPNサーバーを5分で構築する最小構成

WireGuardは現代的なVPNプロトコルで、IPSecやOpenVPNと比べて高速かつ設定がシンプルです。企業環境での導入も増えており、今回は最小構成でWireGuardサーバーを構築する手順を紹介します。

前提条件

  • Ubuntu 20.04 LTS以降のサーバ
  • sudo権限を持つユーザー
  • ポート51820/udpが開放可能な環境

1. WireGuardのインストール

# パッケージ更新
sudo apt update && sudo apt upgrade -y

# WireGuardとツールのインストール
sudo apt install wireguard wireguard-tools -y

2. サーバー側の設定

秘密鍵・公開鍵の生成

# 設定ディレクトリに移動
cd /etc/wireguard

# サーバー秘密鍵生成
sudo wg genkey | sudo tee server_private.key

# サーバー公開鍵生成
sudo cat server_private.key | wg pubkey | sudo tee server_public.key

# 権限設定
sudo chmod 600 server_private.key

サーバー設定ファイル作成

sudo nano /etc/wireguard/wg0.conf
[Interface]
PrivateKey = <server_private.keyの内容>
Address = 10.66.66.1/24
ListenPort = 51820
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

# クライアント設定(例)
[Peer]
PublicKey = <クライアント公開鍵>
AllowedIPs = 10.66.66.2/32

3. IPフォワーディングの有効化

# 一時的な有効化
echo 'net.ipv4.ip_forward=1' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

4. ファイアウォール設定

# UFWの場合
sudo ufw allow 51820/udp
sudo ufw allow ssh
sudo ufw enable

5. サービス起動

# WireGuard起動・自動起動設定
sudo systemctl enable wg-quick@wg0
sudo systemctl start wg-quick@wg0

# 状態確認
sudo wg show

6. クライアント設定の生成

# クライアント鍵生成
wg genkey | tee client_private.key | wg pubkey > client_public.key

# クライアント設定例
cat << EOF > client.conf
[Interface]
PrivateKey = <client_private.keyの内容>
Address = 10.66.66.2/32
DNS = 8.8.8.8

[Peer]
PublicKey = <server_public.keyの内容>
Endpoint = <サーバーのパブリックIP>:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
EOF

動作確認

# サーバー側でクライアント接続確認
sudo wg show

# クライアント側(WireGuardアプリで設定ファイル読み込み後)
ping 10.66.66.1  # サーバーへのpingテスト
curl ifconfig.me  # パブリックIP確認

まとめ

最小構成でWireGuardサーバーを構築する手順を紹介しました。この設定で基本的なVPN接続が可能になります。

詳細な設定とトラブルシューティングはブログで解説しています
👉 WireGuard VPNサーバー構築ガイド:セルフホスティング実装手順

関連記事


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?