Notebook

これは日々の作業を通して学んだことや毎日の生活で気づいたことをを記録しておく備忘録である。

HTML ファイル生成日時: 2026/10/28 08:22:15.034 (台灣標準時)

WireGuard による VPN サーバーとクライアントの準備について

VPN サーバーでの秘密鍵と公開鍵の作成

まず、 pkgsrc を使って net/wireguard-tools をインストールするでござる。


# cd /usr/pkgsrc/net/wireguard-tools
# make install
# pkgclean

カーネルモジュール if_wg をロードするでござる。


# modload if_wg

システムの起動時に自動的に if_wg をロードするためには、 /etc/modules.conf に以下の記述を追加するでござる。


if_wg

秘密鍵を生成するでござる。


# umask 077
# wg genkey > /etc/wg_server_private.key

公開鍵を生成するでござる。


# wg pubkey < /etc/wg_server_private.key > /etc/wg_server_public.key

VPN クライアントでの秘密鍵と公開鍵の作成

まず、 pkgsrc を使って net/wireguard-tools をインストールするでござる。


# cd /usr/pkgsrc/net/wireguard-tools
# make install
# pkgclean

カーネルモジュール if_wg をロードするでござる。


# modload if_wg

システムの起動時に自動的に if_wg をロードするためには、 /etc/modules.conf に以下の記述を追加するでござる。


if_wg

秘密鍵を生成するでござる。


# umask 077
# wg genkey > /etc/wg_client_private.key

公開鍵を生成するでござる。


# wg pubkey < /etc/wg_client_private.key > /etc/wg_client_public.key

サーバー側での作業

IP forwarding が有効になっていなければ、有効にするでござる。


# sysctl -w net.inet.ip.forwarding=1

起動時に自動的に有効にさせるには、 /etc/sysctl.conf に以下の記述を追加 すればよいでござる。


net.inet.ip.forwarding=1

NAT とパケットフィルターの設定を行うでござる。まず、 /etc/pf.conf に以 下の記述を追加するでござる。


ext_if="wm0"
nat on $ext_if inet from 10.0.0.0/24 to any -> ($ext_if)
pass quick on wg0 all
pass in on $ext_if proto udp to port 12345

pf の設定ファイルの書き方に間違いがないかどうか確認するでござる。


# pfctl -nf /etc/pf.conf

次に /etc/rc.conf に以下の記述を追加するでござる。


pf=YES

パケットフィルターを起動するでござる。


/etc/rc.d/pf start

ネットワークインターフェース wg0 を作成するでござる。


# ifconfig wg0 create
# ifconfig wg0 up
# ifconfig wg0 inet 10.0.0.1 10.0.0.1 netmask 255.255.255.0
# wgconfig wg0 set private-key /etc/wg_server_private.key
# wgconfig wg0 set listen-port 12345
# wgconfig wg0 add peer client0 CLIENT_PUBLIC_KEY --allowed-ips=10.0.0.100/32

システムの起動時に wg0 を自動的に作成させるためには、 /etc/ifconfig.wg0 を作成し、以下の内容を書き込むでござる。


create
up
inet 10.0.0.1 10.0.0.1 netmask 255.255.255.0
!wgconfig wg0 set private-key /etc/wg_server_private.key
!wgconfig wg0 set listen-port 12345
!wgconfig wg0 add peer client1 CLIENT_PUBLIC_KEY --allowed-ips=10.0.0.100/32

クライアント側での作業

以下のようなシェルスクリプト vpn_on.sh を用意するでござる。


#!/bin/sh

#
# Parameters
#

# default gateway when VPN is not used
PHYS_GW="192.168.0.1"
# global IP address of VPN server
SERVER_IP="12.34.56.78"
# VPN port number on server
SERVER_PORT="12345"
# WireGuard local IP address
WG_LOCAL_IP="10.0.0.100"
# WireGuard remote IP address
WG_REMOTE_IP="10.0.0.1"
# VPN server public key
SERVER_PUBKEY="0123456789abcdefghijklmnopqrstuvwxyz0123456="
# VPN client private key file
CLIENT_PRIVKEY_FILE="/etc/wg_client_private.key"

#
# starting VPN
#

# loading kernel module
modload if_wg

# creating wg0 interface
ifconfig wg0 create
ifconfig wg0 up
ifconfig wg0 inet ${WG_LOCAL_IP} ${WG_REMOTE_IP}

# settings for WireGuard
wgconfig wg0 set private-key ${CLIENT_PRIVKEY_FILE}
wgconfig wg0 add peer server ${SERVER_PUBKEY} --allowed-ips=0.0.0.0/0 --endpoint=${SERVER_IP}:${SERVER_PORT}

# routing
route delete default
route delete -host ${SERVER_IP}
route add -host ${SERVER_IP} ${PHYS_GW}
route add default ${WG_REMOTE_IP}

# printing status
echo "#"
echo "# VPN is now ON (Full-tunnel active)."
echo "#"

また、以下のようなシェルスクリプト vpn_off.sh も用意するでござる。


#!/bin/sh

#
# Parameters
#

# default gateway when VPN is not used
PHYS_GW="192.168.0.1"
# IP address of VPN server
SERVER_IP="12.34.56.78"

#
# stopping VPN
#

# routing
route delete default
route delete -host ${SERVER_IP} ${PHYS_GW}

# destroying wg0 interface
ifconfig wg0 destroy

# printing status
echo "#"
echo "# VPN is now OFF (Direct connection restored)."
echo "#"

VPN を使いたいときは、以下のようにするでござる。


# sh /root/bin/vpn_on.sh

VPN を終了させるときには、以下のようにするでござる。


# sh /root/bin/vpn_off.sh

VPN を有効にした状態で、ウェブブラウザーを使って https://ifconfig.me/ を見てみると、 VPN サーバーの IP アドレスが表示されているはずでござる。



Frequently accessed files

  1. Computer___Network/20260127_00.html
  2. Misc___Taiwan/20240207_00.html
  3. Computer___NetBSD/20250301_01.html
  4. Misc___Taiwan/20240819_00.html
  5. Book___Chinese/20240424_00.html
  6. Computer___TeX/20231107_00.html
  7. Computer___TeX/20240414_01.html
  8. Misc___Japan/20240718_00.html
  9. Misc___Japan/20240610_00.html
  10. Computer___TeX/20240411_00.html
  11. Misc___Taiwan/20240903_01.html
  12. Computer___TeX/20230726_01.html
  13. Computer___Network/20241214_00.html
  14. Computer___Python/20230803_00.html
  15. Computer___Network/20230516_00.html
  16. Computer___NetBSD/20230119_00.html
  17. Computer___NetBSD/20250113_00.html
  18. Computer___FreeBSD/20220621_0.html
  19. Computer___TeX/20240414_00.html
  20. Computer___NetBSD/20250307_00.html
  21. Computer___Network/20240130_00.html
  22. Computer___TeX/20240410_00.html
  23. Computer___Python/20250330_00.html
  24. Computer___NetBSD/20220818_1.html
  25. Computer___NetBSD/20240805_03.html
  26. Computer___TeX/20230503_00.html
  27. Computer___Network/20220413_1.html
  28. Computer___Python/20240101_00.html
  29. Computer___NetBSD/20240101_03.html
  30. Computer___Hardware/20240820_00.html


HTML file generated by Kinoshita Daisuke.