[筆記] WireGuard 架設伺服器
2 Comments

更新於 2024-12-12 22:35:11

前言

最近要搞 Wireguard 才發現要弄這個好麻煩,之前做過的事都忘光光,在這裡留個筆記,下次直接抄。

伺服端

安裝套件

sudo apt install wireguard

生成私鑰

wg genkey | sudo tee /etc/wireguard/private.key
sudo chmod 600 /etc/wireguard/private.key

生成公鑰

sudo cat /etc/wireguard/private.key | wg pubkey | sudo tee /etc/wireguard/public.key

設定允許 IPv4 轉發

修改 `/etc/sysctl.conf`

sysctl net.ipv4.ip_forward=1

建立配置文件

sudo nano /etc/wireguard/wg0.conf
[Interface]
PrivateKey = 伺服端私鑰
Address = 10.6.0.1/24
DNS = 8.8.8.8
ListenPort = 51820

[peer]
PublicKey = 客戶端公鑰
AllowedIPs = 10.6.0.2/32
PersistentKeepalive = 25

對 Server 來說自己的 IP 為 10.6.0.1,客戶端 IP 為 10.6.0.2。

啟動指令

sudo wg-quick up wg0

關閉指令

sudo wg-quick down wg0

開啟端口 51820 的指令

sudo iptables -I INPUT -p udp -m udp --dport 51820 -j ACCEPT

客戶端

配置文件

[Interface]
PrivateKey = 客戶端私鑰
Address = 10.6.0.2/24
DNS = 8.8.8.8

[Peer]
PublicKey = 伺服端公鑰
AllowedIPs = 10.6.0.0/24
Endpoint = domain.net:51820
PersistentKeepalive = 25

對 Client 來說自己的 IP 為 10.6.0.2,伺服端 IP 為 10.6.0.1。並且,10.6.0.0/24 的路由會通過 wg0 的網路介面。

如果要設定全部流量則是把 AllowedIPs 設定成 0.0.0.0/0

By Weil Jimmer


This entry was posted in General, The Internet, Note By Weil Jimmer.

About Weil Jimmer

Hi! Everyone! My name is Weil Jimmer. This is my personal blog. I'm a webmaster of this site. I hope the site will be popular. Now, Let's go! Enjoy gaining more knowledge.
More Details About Me : https://weils.net/profile.php


Leave a message.

Only the first 10 comment will show.


Reply to comment-99
Weil Jimmer
2025-01-14 Tuesday 13:31:17
跟伺服端公鑰私鑰是一樣的生成模式(同個指令)。

comment-100


Reply to POST.
Michael Ho
2025-01-13 Monday 17:11:56
請問 客戶端公&私鑰 怎麼生成?

comment-99