FreeBSD 14
Sponsored Link

WireGuard : クライアントの設定 (FreeBSD)2024/09/20

 
シンプルで高速な VPN サーバー、 WireGuard のインストールと設定です。
当例では以下のような環境で WireGuard クライアントを設定します。
インターネットを経由するため、事前に、自身の環境で、WireGuard サーバー側のローカルネットワークとインターネットの境界ルーターに、IP マスカレードの設定が必要です。
下例の場合、WireGuard クライアントからインターネットを経由して WireGuard サーバーのグローバル IP アドレス宛てに来るパケットを、[Router#1] 上で、WireGuard サーバーの IP アドレス (10.0.0.30) の、WireGuard 設定ファイルで指定した待ち受けポート (UDP) に転送するよう設定します。
  +------------------------+
  | [  WireGuard Server  ] |172.16.100.1 (VPN IP)
  |      dlp.srv.world     +--------+
  |                        |wg0     |
  +-----------+------------+        |
        vtnet0|10.0.0.30/24         |
              |                     |
              |       Local Network |
       +------+-----+               |
-------|  Router#1  |---------------|-----
       +------+-----+               |
              |                     |
    Internet  |  Internet           |
              |                     |
       +------+-----+               |
-------|  Router#2  |---------------|-----
       +------+-----+               |
              |       Local Network |
              |                     |
        vtnet0|192.168.10.30/24     |
  +-----------+------------+        |
  |  [ WireGuard Client ]  |wg0     |
  |                        +--------+
  |                        |172.16.100.5 (VPN IP)
  +------------------------+

[1]
事前に、サーバー側で生成した [クライアント用プライベートキー] と [サーバー用パブリックキー] を、設定するクライアントに転送、または、内容を通知しておきます。
[2] WireGuard をインストールします。
root@client:~ #
pkg install -y wireguard-tools
[3] WireGuard の設定です。
root@client:~ #
umask 077
# 設定ファイル新規作成
# [wg0.conf] ⇒ [(VPN インターフェース名).conf] のルール内で任意の名称で OK

root@client:~ #
vi /usr/local/etc/wireguard/wg0.conf
[Interface]
# サーバー側で生成したクライアント用プライベートキーを指定
PrivateKey = eMJa2BQliS8mNFZiYrebWaCFlda9cNC9JPEvWOb3G2A=
# VPN インターフェースに割り当てる IP アドレス
Address = 172.16.100.5/24

[Peer]
# サーバー側で生成したサーバー用パブリックキーを指定
PublicKey = AazFOtPUm8ZM7rfqMhOYdF4UFKXENOkSB7r0LMxZNnA=
# 接続を許可する IP アドレス または ネットワーク範囲
# 下例は、WireGuard サーバーの VPN IP アドレスと、実際のローカルネットワークを許可
AllowedIPs = 172.16.100.1, 10.0.0.0/24
# サーバーのグローバル IP アドレス:ポート
# (下例は実際はプライベート用の IP アドレスだが、自身の環境に置き換え)
EndPoint = 172.29.10.100:51820

root@client:~ #
sysrc wireguard_interfaces="wg0"

wireguard_interfaces: -> wg0
root@client:~ #
service wireguard enable

wireguard enabled in /etc/rc.conf
root@client:~ #
service wireguard start

[#] ifconfig wg create name wg0
[#] wg setconf wg0 /dev/stdin
[#] ifconfig wg0 inet 172.16.100.5/24 alias
[#] ifconfig wg0 mtu 1420
[#] ifconfig wg0 up
[#] route -q -n add -inet 172.16.100.1/32 -interface wg0
[#] route -q -n add -inet 10.0.0.0/24 -interface wg0
[+] Backgrounding route monitor

root@client:~ #
ifconfig

vtnet0: flags=1008843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST,LOWER_UP> metric 0 mtu 1500
        options=4c07bb<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,JUMBO_MTU,VLAN_HWCSUM,TSO4,TSO6,LRO,VLAN_HWTSO,LINKSTATE,TXCSUM_IPV6>
        ether 52:54:00:7c:de:11
        inet 192.168.0.30 netmask 0xffffff00 broadcast 192.168.0.255
        inet6 fe80::5054:ff:fe7c:de11%vtnet0 prefixlen 64 scopeid 0x1
        media: Ethernet autoselect (10Gbase-T <full-duplex>)
        status: active
        nd6 options=23<PERFORMNUD,ACCEPT_RTADV,AUTO_LINKLOCAL>
lo0: flags=1008049<UP,LOOPBACK,RUNNING,MULTICAST,LOWER_UP> metric 0 mtu 16384
        options=680003<RXCSUM,TXCSUM,LINKSTATE,RXCSUM_IPV6,TXCSUM_IPV6>
        inet 127.0.0.1 netmask 0xff000000
        inet6 ::1 prefixlen 128
        inet6 fe80::1%lo0 prefixlen 64 scopeid 0x2
        groups: lo
        nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>
wg0: flags=10080c1<UP,RUNNING,NOARP,MULTICAST,LOWER_UP> metric 0 mtu 1420
        options=80000<LINKSTATE>
        inet 172.16.100.5 netmask 0xffffff00
        groups: wg
        nd6 options=109<PERFORMNUD,IFDISABLED,NO_DAD>

# 接続状態確認

root@client:~ #
wg show

interface: wg0
  public key: lFQASdqSmQhPXuEtTO8/xBVrbgpXSVZQhP9mQAbyNUw=
  private key: (hidden)
  listening port: 23438

peer: AazFOtPUm8ZM7rfqMhOYdF4UFKXENOkSB7r0LMxZNnA=
  endpoint: 172.29.10.100:51820
  allowed ips: 10.0.0.0/24, 172.16.100.1/32
[4] VPN 接続が確立できたら、クライアントからサーバー側のローカルネットワークにアクセスできるか確認しておくとよいでしょう。
root@client:~ #
ping -c 3 10.0.0.30

PING 10.0.0.30 (10.0.0.30): 56 data bytes
64 bytes from 10.0.0.30: icmp_seq=0 ttl=64 time=3.302 ms
64 bytes from 10.0.0.30: icmp_seq=1 ttl=64 time=1.205 ms
64 bytes from 10.0.0.30: icmp_seq=2 ttl=64 time=1.264 ms

--- 10.0.0.30 ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 1.205/1.924/3.302/0.975 ms

root@client:~ #
ssh -o StrictHostKeyChecking=no ubuntu@10.0.0.203 hostname

Warning: Permanently added '10.0.0.203' (ED25519) to the list of known hosts.
ubuntu@10.0.0.203's password:
ubuntu
関連コンテンツ