wireguard
ufw
本文字数:2.2k 字 | 阅读时长 ≈ 10 min

wireguard

ufw
本文字数:2.2k 字 | 阅读时长 ≈ 10 min

打开公网服务器的UDP服务

wireguard 可以用来进行组网,如上所示,我有一个带有公网 IP 的服务器 server,同时我还有两个客户端 client1 和 client2,通过在这三个设备上安装 wireguard,就可以让三个网络处在同一内网状态下

1. 单设备与公网相连

这里首先来配置 client1 和 server 的相连接

1.1 服务器端配置

首先是配置服务端,下面以 Ubuntu 为例

# wireguard 安装
sudo apt-get install wireguard resolvconf

# 开启 IP 转发,如果不开启 client1 和 client2 通信的时候,server 不会中转流量
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf

成功开启 IP 转发后,你应该能看到如下内容:

➜ ~ sysctl -p

vm.swappiness = 0
kernel.sysrq = 1
net.ipv4.neigh.default.gc_stale_time = 120
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 1024
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_slow_start_after_idle = 0
net.ipv4.ip_forward = 1

1.2 设置 WireGuard 配置目录权限

进入 server 服务器,进入到 WireGuard 配置目录,并调整目录权限:

cd /etc/wireguard/
chmod 0777 /etc/wireguard
umask 077

生成服务器和客户端的公钥和私钥

# 生成 WireGuard 服务器公钥和私钥
wg genkey > server.key
wg pubkey < server.key > server.key.pub

# 生成客户端 client1 的公钥和私钥
wg genkey > client1.key
wg pubkey < client1.key > client1.key.pub

查看已经生成的公钥和私钥

cat server.key && cat server.key.pub && cat client1.key && cat client1.key.pub

设置配置文件 wg0.conf,这里的配置文件和公钥私钥在一个文件夹下 /etc/wireguard 即可,首先我们要查看我们的网卡,比如这里的网卡只有 eth0,因此我们下面的配置文件交互也写 eth0 即可

➜ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.26.188.38  netmask 255.255.240.0  broadcast 172.26.191.255
        inet6 fe80::216:3eff:fe34:a02d  prefixlen 64  scopeid 0x20<link>
        ether 00:16:3e:34:a0:2d  txqueuelen 1000  (Ethernet)
        RX packets 5624911  bytes 2227438547 (2.2 GB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3746506  bytes 2077853178 (2.0 GB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 1590661  bytes 715715656 (715.7 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1590661  bytes 715715656 (715.7 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

进入到 /etc/wireguard,然后我们配置 wg0.conf 即可

echo "
[Interface]
PrivateKey = $(cat server.key) # 填写本机的privatekey 内容
Address = 10.0.8.1 #本机虚拟局域网IP

PostUp = iptables -I FORWARD -s 10.0.8.0/24 -i wg0 -d 10.0.8.0/24 -j ACCEPT
PostDown = iptables -D FORWARD -s 10.0.8.0/24 -i wg0 -d 10.0.8.0/24 -j ACCEPT
#注意eth0需要为本机网卡名称

ListenPort = 50814 # 监听端口
DNS = 8.8.8.8
MTU = 1420

[Peer]
PublicKey =  $(cat client1.key.pub)  #自动client1的公钥
AllowedIPs = 10.0.8.3/32 #客户端所使用的IP" > wg0.conf

这里我们用的是 50814 端口,也可以随意指定,如果你的服务器是阿里云等厂商的,需要进入到服务器面板打开端口的 UDP 转发,因为 wireguard 是利用 UDP 转发流量的

打开公网服务器的UDP服务

# 设置服务器开机自启动
systemctl enable wg-quick@wg0

# 启动 WireGuard 生效
wg-quick up wg0   # 启动
wg-quick down wg0  # 关闭

此时用ifconfig就会出现一个wg0的虚拟网络了

➜ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.26.188.38  netmask 255.255.240.0  broadcast 172.26.191.255
        inet6 fe80::216:3eff:fe34:a02d  prefixlen 64  scopeid 0x20<link>
        ether 00:16:3e:34:a0:2d  txqueuelen 1000  (Ethernet)
        RX packets 5630961  bytes 2228006833 (2.2 GB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3750646  bytes 2078427359 (2.0 GB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 1593443  bytes 716004129 (716.0 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1593443  bytes 716004129 (716.0 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wg0: flags=209<UP,POINTOPOINT,RUNNING,NOARP>  mtu 1420
        inet 10.0.8.1  netmask 255.255.255.255  destination 10.0.8.1
        unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  txqueuelen 1000  (UNSPEC)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

运行 wg 可以查看 wireguard的运行状态

1.3 客户端配置

进入到这个网址 https://www.wireguard.com/install/,可以下载wireguard 的 windows 和 mac 客户端等

这里配置的时候,我们将 win 客户端用 10.0.8.3,mac 端用 10.0.8.2

1.3.1 Windows 客户端

下载 windows wireguard 客户端

[Interface]
PrivateKey = [客户端私钥] #此处为client1的私钥
Address = 10.0.8.3 #此处为peer规定的客户端IP
MTU = 1420

[Peer]
PublicKey = [服务器公钥]
AllowedIPs = 10.0.8.0/24
Endpoint = [服务器IP]:50814

这里我用的是10.0.8.3

运行完后可以 ping 一下 server 的 wireguard ip,看看能不能通,通了表示和 server 处在同一内网段了

ping 一下 server 的 wireguard ip,看看能不能通

1.3.2 Mac 客户端

  1. wireguard 客户端

mac 客户端也是如此,链接上以后 ping 一下,就可以 ping 通了

(base) ➜ ~ ping 10.0.8.1
PING 10.0.8.1 (10.0.8.1): 56 data bytes
64 bytes from 10.0.8.1: icmp_seq=0 ttl=64 time=42.122 ms
64 bytes from 10.0.8.1: icmp_seq=1 ttl=64 time=20.358 ms
64 bytes from 10.0.8.1: icmp_seq=2 ttl=64 time=23.725 ms
64 bytes from 10.0.8.1: icmp_seq=3 ttl=64 time=24.030 ms
64 bytes from 10.0.8.1: icmp_seq=4 ttl=64 time=24.849 ms
64 bytes from 10.0.8.1: icmp_seq=5 ttl=64 time=20.023 ms

然后我们登录公网服务器,运行 wg 命令,可以看到连接信息

➜ wireguard wg             
interface: wg0
  public key: vDB4c1Lx9cLS6rYay5L8tW0F9OebPVmWbXejxwlkFSQ=
  private key: (hidden)
  listening port: 50814

peer: E0i3Y7tYAwp7b9ygYt/3cBbWE8/1JZeBZOKJP+BUQEk=
  endpoint: 114.214.191.48:49388
  allowed ips: 10.0.8.2/32
  latest handshake: 23 seconds ago
  transfer: 2.39 KiB received, 2.34 KiB sent
  1. surge 客户端

此外,mac 上除了 wireguard 客户端,也可以使用 surge 进行连接,基本设置如下,但是需要注意,surge 只能代理 http https 流量,ping 是 ICMP 流量,所以你即使 ping,可能是无效的,这时候需要在别的内网服务器上启动 Web 服务进行测试 python -m http.server --bind 0.0.0.0,然后在 mac 这里连接对应服务器的 ip+端口 即可

[General]
skip-proxy = 127.0.0.1, 172.16.0.0/12, 17.0.0.0/8, localhost, *.local, *.crashlytics.com

[Proxy]
wireguard-home = wireguard, section-name=HomeServer, test-url=http://10.0.8.1

[WireGuard HomeServer]
private-key = [客户端私钥]
self-ip = 10.0.8.2
mtu = 1420
peer = (public-key = [服务器公钥]=, allowed-ips = "0.0.0.0/0, ::/0", endpoint = [服务器IP]:50814)

[Rule]
IP-CIDR,10.0.8.0/24,wireguard-home,no-resolve

2. 多设备通过公网互联

上面我们配置了一个 client 和公网 ip 的互联,但是这种互联没有意义,因为即使不配置 wireguard,他们也能够连接。wireguard 的存在意义就是让两个本来不相关的内网环境通过公网 ip 进行连接,很简单,我们只要把 win 和 mac 的内容都加到 wg0.conf 中时,就默认互通了,如果没通的话,可以看一下 conf 文件中是否有了这个内容了

# 1. 生成新客户端密钥
wg genkey > client2.key
wg pubkey < client2.key > client2.key.pub

# 2. 添加新客户端到服务器配置,即 `wg0.conf`
echo "
[Peer]
PublicKey = $(cat client2.key.pub)
AllowedIPs = 10.0.8.2/32" >> wg0.conf

# 3. 重启 WireGuard 服务:
wg-quick down wg0
wg-quick up wg0

为了方便你查看,这里做个参考,这里我用了三个 client

➜ cat wg0.conf                                                                   
[Interface]
PrivateKey = [本机的 privatekey] # 填写本机的 privatekey 内容
Address = 10.0.8.1 #本机虚拟局域网IP

PostUp = iptables -I FORWARD -s 10.0.8.0/24 -i wg0 -d 10.0.8.0/24 -j ACCEPT
PostDown = iptables -D FORWARD -s 10.0.8.0/24 -i wg0 -d 10.0.8.0/24 -j ACCEPT

ListenPort = 50814 # 监听端口
DNS = 8.8.8.8
MTU = 1420

[Peer]
PublicKey =  [client1 的公钥]  # 自动 client1 的公钥
AllowedIPs = 10.0.8.2/32  # 客户端所使用的IP

[Peer]
PublicKey =  [client2 的公钥]  #自动client2的公钥
AllowedIPs = 10.0.8.3/32  # 客户端 Client2 所使用的IP

[Peer]
PublicKey =  [client3 的公钥]  # 自动 client3 的公钥
AllowedIPs = 10.0.8.4/32  # 客户端 Client3 所使用的IP

此时我们就可以在 client1 和 client2 之间进行通信了,也可以和 server 进行通信了

常见问题

  1. 连接测试

    • 使用 ping 10.0.8.1 测试基本连接
    • 使用 wg 命令查看连接状态
    • 对于 Surge 代理,需要启动 Web 服务进行测试
  2. 防火墙设置

    • 确保服务器防火墙开放 UDP 50814 端口
    • 检查 iptables 规则是否正确配置
  3. 网络配置

    • 确保 IP 转发已启用
    • 检查网卡名称是否正确(eth0 或其他)
Mar 13, 2026
ufw
Mar 13, 2026
ufw
Dec 14, 2025