0%

Build a WireGuard VPN server on Linode.

Installation

1
2
3
sudo add-apt-repository ppa:wireguard/wireguard
sudo apt-get update
sudo apt-get install wireguard

Configuration

  1. 確認 kernel 版本非客制
    1
    2
    uname --kernel-release
    // check your kernel name without linode

如果執行完上面指令看到有其他非正規字眼像是 xxxxxx-linde152 之類的,你必須去 linode boot settings 的後台設定裡面去把改成 GRUB 2,不然會造成 wireguard 無法確認 kernel 版本會安裝執行失敗
參考這邊

改完重開之後會像再執行一次會變成像這樣 4.15.0-54-generic

  1. 按照正常安裝流程
  2. Key Generation
1
wg genkey | tee privatekey | wg pubkey > publickey
  1. 新建 server 端設定檔
    在這個位置建立一個檔案 /etc/wireguard/wg0.conf
1
2
3
4
5
6
7
8
9
10
11
12
[Interface]
Address = 10.66.66.1/24 // 通道連上之後的內網 ipv4 ip
Address = fd42:42:42::1/64 // 通道連上之後的內網 ipv6 ip
SaveConfig = true
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE; ip6tables -A FORWARD -i wg0 -j ACCEPT; ip6tables -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; ip6tables -D FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
ListenPort = 51820 // vpn listen port
PrivateKey = <server private key> // 填上前一個步驟產生的 private key

[Peer] // 設定可以連入的 client 資訊
PublicKey = <client public key>
AllowedIPs = 10.66.66.2/32, fd42:42:42::2/128 // client 會取到的 ip
  1. client 端設定檔
1
2
3
4
5
6
7
8
9
10
[Interface]
PrivateKey = <client private key>
Address = 10.66.66.2/24, fd42:42:42::2/64
DNS = 8.8.8.8, 1.1.1.1

[Peer]
PublicKey = <server public key>
AllowedIPs = 0.0.0.0/0, ::/0
Endpoint = <server ip>:<listenPort> // xxx.xxx.xxx.xxx:51820
PersistentKeepalive = 25
  1. Set Up Firewall Rules
1
2
3
4
5
sudo ufw allow 22/tcp
sudo ufw allow 51820/udp
sudo ufw enable

sudo ufw status verbose // for test
  1. Start the Wireguard Service
1
2
3
wg-quick up wg0
sudo systemctl enable wg-quick@wg0 //設定開機自動執行
sudo wg show