Налаштування WireGuard на CentOS, Debian, Ubuntu та FreeBSD чотирма різними способами

WireGuard 1600x896 wireguard.png
WireGuard

Нижче наведено набір сніппетів для налаштування WireGuard для трьох різних дистрибутивів Linux та FreeBSD.
Припускається, що сервер працюватиме на Debian, а мережа буде 10.9.21.0/24.

Сервер, Debian

apt install wireguard-tools
nano /etc/network/interfaces.d/wg0

1
2
3
4
5
6
7
8
auto wg0
iface wg0 inet static
    address 10.9.21.1/32
    pre-up ip link add wg0 type wireguard
    pre-up wg setconf wg0 /etc/wireguard/wg0.conf
    up ip -4 route add 10.9.21.0/24 dev wg0
    # up /bin/systemctl restart sshd.service
    post-down ip link del wg0

umask 077
cd /etc/wireguard/
wg genkey > server-private.key
wg pubkey < server-private.key > server-public.key

nano /etc/wireguard/wg0.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[Interface]
ListenPort = 1234
PrivateKey = <server-private.key>

[Peer]
# Debian client
PublicKey = <public.key>
AllowedIPs = 10.9.21.3/32

[Peer]
# CentOS client
PublicKey = <public.key>
AllowedIPs = 10.9.21.4/32

[Peer]
# Ubuntu client
PublicKey = <public.key>
AllowedIPs = 10.9.21.5/32

[Peer]
# FreeBSD client
PublicKey = <public.key>
AllowedIPs = 10.9.21.6/32


Клієнти

До речі, Debian-way та FreeBSD-way найбільш близькі до UNIX-way, тоді як Ubuntu швидше орієнтована на ‘yaml-продакшн’, ну і CentOS із його systemd. З коробки, маю на увазі.

Debian через конфігурацію interfaces.d

1
2
3
4
apt install wireguard
umask 077
wg genkey > /etc/wireguard/private.key
cat /etc/wireguard/private.key | wg pubkey > /etc/wireguard/public.key

nano /etc/network/interfaces.d/wg0

1
2
3
4
5
6
7
auto wg0
iface wg0 inet static
    address 10.9.21.3/32
    pre-up ip link add wg0 type wireguard
    pre-up wg setconf wg0 /etc/wireguard/wg0.conf
    up ip -4 route add 10.9.21.0/24 dev wg0
    post-down ip link del wg0

nano /etc/wireguard/wg0.conf

1
2
3
4
5
6
7
8
9
[Interface]
# client private key
PrivateKey = <private.key>
[Peer]
# server public key
PublicKey = <server-public.key>
Endpoint = <server-external-ip>:1234
PersistentKeepalive = 10
AllowedIPs = 10.9.21.0/24


CentOS через systemd та wg-quick

1
2
3
4
5
dnf install epel-release -y
dnf install wireguard-tools -y
umask 077
wg genkey | sudo tee /etc/wireguard/private.key | wg pubkey | sudo tee /etc/wireguard/public.key
nano /etc/wireguard/wg0.conf
1
2
3
4
5
6
7
8
[Interface]
Address = 10.9.21.4/24
PrivateKey = <private.key>
[Peer]
PublicKey = <server-public.key>
Endpoint = <server-external-ip>:1234
PersistentKeepalive = 10
AllowedIPs = 10.9.21.0/24
1
2
3
4
5
6
7
8
9
printf 'net.ipv4.ip_forward = 1' > /etc/sysctl.d/wireguard.conf
sysctl -p
wg-quick up /etc/wireguard/wg0.conf
wg-quick down /etc/wireguard/wg0.conf
systemctl enable wg-quick@wg0
systemctl status wg-quick@wg0
ifconfig -a
wg show
journalctl -u wg-quick@wg0


Ubuntu з використанням Netplan

1
2
3
4
apt install wireguard
umask 077
wg genkey | tee /etc/wireguard/private.key | \
wg pubkey > /etc/wireguard/public.key


nano /etc/netplan/50-wireguard.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
network:
  version: 2
  tunnels:
    wg0:
      mode: wireguard
      key: <private.key>
      addresses:
        - 10.9.21.5/24
      routes:
        - to: 10.9.21.0/24
          via: 10.9.21.1
      peers:
        - keys:
            public: <server-public.key>
          endpoint: <server-external-ip>:1234
          allowed-ips:
            - 10.9.21.0/24
          keepalive: 25
1
2
3
4
5
netplan apply
systemctl daemon-reload
systemctl restart netplan-configure.service
ifconfig -a
wg show


FreeBSD-way використовуючи rc.conf

1
2
3
4
5
6
pkg update && pkg upgrade
pkg install wireguard-tools
chmod -R 750 /usr/local/etc/wireguard
cd /usr/local/etc/wireguard
umask 077
wg genkey | tee private.key | wg pubkey > public.key


nano /etc/rc.conf

1
2
cloned_interfaces="wg0"
ifconfig_wg0="inet 10.9.21.6/24 mtu 1420 up"


nano /etc/start_if.wg0

1
wg syncconf wg0 /usr/local/etc/wireguard/wg0.conf


nano /usr/local/etc/wireguard/wg0.conf

1
2
3
4
5
6
7
8
[Interface]
PrivateKey = <private.key>

[Peer]
PublicKey = <server-public.key>
AllowedIPs = 10.9.21.0/24
Endpoint = <server-external-ip>:1234
PersistentKeepalive = 25


Команда wg show відобразить стан інтерфейсу wg0, проте service wireguard status не покаже жодної інформації, оскільки інтерфейс налаштовано через sysrc | rc.conf.

Ручне налаштування інтерфейсу

1
2
3
4
5
6
7
8
# up
ifconfig wg0 create
wg setconf wg0 /usr/local/etc/wireguard/wg0.conf
ifconfig wg0 inet 10.9.21.27/32 mtu 1420 up
route -q -n add -inet 10.9.21.0/24 -interface wg0
# down
ifconfig wg0 down
ifconfig wg0 destroy


Універсальний варіант для Linux

nano /etc/wireguard/wg0.conf

1
2
3
4
5
6
7
8
9
[Interface]
# client private key
PrivateKey = <private.key>
[Peer]
# server public key
PublicKey = <server-public.key>
Endpoint = <server-external-ip>:1234
PersistentKeepalive = 10
AllowedIPs = 10.9.21.0/24


1
2
3
4
5
6
7
8
ip link del wg0
ip link add wg0 type wireguard
wg setconf wg0 /etc/wireguard/wg0.conf
ip -4 address add 10.9.21.13/32 dev wg0
ip link set mtu 1420 up dev wg0
ip -4 route add 10.9.21.0/24 dev wg0
wg show
ping 10.9.21.1