使用 WireGuard 在云服务器上搭建中继,连接没有公网IP的家里和公司网络

以下是使用 WireGuard 在云服务器上搭建中继,连接没有公网IP的家里和公司的详细步骤:


环境说明

  • 云服务器(公网IP):假设公网IP为 1.1.1.1,作为中继节点。
  • 家里网络A:假设家里内网网段 192.168.1.0/24,通过路由器或主机连接到云服务器。
  • 公司网络B:假设公司内网网段 192.168.2.0/24,同样连接到云服务器。

步骤 1:在云服务器上安装 WireGuard

# Ubuntu/Debian
sudo apt update
sudo apt install wireguard resolvconf -y

# 生成服务器密钥对
wg genkey | sudo tee /etc/wireguard/privatekey | wg pubkey | sudo tee /etc/wireguard/publickey

步骤 2:配置云服务器的 WireGuard 中继

创建配置文件 /etc/wireguard/wg0.conf,内容如下:

[Interface]
# 云服务器的 WireGuard 配置
PrivateKey = <云服务器的私钥>  # 从 /etc/wireguard/privatekey 获取
Address = 10.0.0.1/24          # 虚拟局域网IP(云服务器)
ListenPort = 51820             # 监听的UDP端口
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

# 启用IP转发(关键!)
PreUp = sysctl -w net.ipv4.ip_forward=1

# 添加两个局域网的Peer(客户端A和B)
[Peer]
# 客户端A(家里网络A)
PublicKey = <客户端A的公钥>
AllowedIPs = 10.0.0.2/32, 192.168.1.0/24  # 允许访问虚拟IP和家里网络A的网段

[Peer]
# 客户端B(公司网络B)
PublicKey = <客户端B的公钥>
AllowedIPs = 10.0.0.3/32, 192.168.2.0/24  # 允许访问虚拟IP和公司网络B的网段

步骤 3:启动云服务器的 WireGuard

# 启动WireGuard
sudo systemctl enable wg-quick@wg0
sudo systemctl start wg-quick@wg0

# 检查状态
sudo wg show

步骤 4:配置家里网络A的客户端

  1. 在家里网络A的路由器上安装 WireGuard
opkg update && opkg install wireguard
  1. 生成客户端A的密钥对
   wg genkey | tee privatekey_A | wg pubkey > publickey_A
  1. 创建客户端A的配置文件 /etc/config/wireguard
   [Interface]
   PrivateKey = <客户端A的私钥>
   Address = 10.0.0.2/24       # 虚拟局域网IP(客户端A)
   DNS = 8.8.8.8               # 可选DNS

   [Peer]
   PublicKey = <云服务器的公钥>  # 从云服务器的publickey获取
   Endpoint = 1.1.1.1:51820     # 云服务器的公网IP和端口
   AllowedIPs = 10.0.0.0/24, 192.168.2.0/24  # 允许访问虚拟IP和公司网络B的网段
   PersistentKeepalive = 25     # 保持连接
  1. 启动客户端A的WireGuard
  /etc/init.d/wireguard start

步骤 5:配置公司网络B的客户端

  1. 在公司网络B的路由器上安装 WireGuard
opkg update && opkg install wireguard
  1. 生成客户端B的密钥对
   wg genkey | tee privatekey_B | wg pubkey > publickey_B
  1. 创建客户端B的配置文件 /etc/config/wireguard
   [Interface]
   PrivateKey = <客户端B的私钥>
   Address = 10.0.0.3/24       # 虚拟局域网IP(客户端B)

   [Peer]
   PublicKey = <云服务器的公钥>
   Endpoint = 1.1.1.1:51820
   AllowedIPs = 10.0.0.0/24, 192.168.1.0/24  # 允许访问虚拟IP和家里网络A的网段
   PersistentKeepalive = 25
  1. 启动客户端B的WireGuard
   /etc/init.d/wireguard start

步骤 6:在云服务器上添加客户端公钥

将客户端A和B的公钥添加到云服务器的 wg0.conf 中:

sudo wg set wg0 peer <客户端A的公钥> allowed-ips 10.0.0.2/32,192.168.1.0/24
sudo wg set wg0 peer <客户端B的公钥> allowed-ips 10.0.0.3/32,192.168.2.0/24

步骤 7:配置路由和防火墙

  1. 确保云服务器启用IP转发
   # 永久生效
   echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.conf
   sudo sysctl -p
  1. 配置客户端局域网的路由规则
  • 在家里网络A的路由器上,添加路由:
    bash ip route add 192.168.2.0/24 via 10.0.0.1 dev wg0
  • 在公司网络B的路由器上,添加路由:
    bash ip route add 192.168.1.0/24 via 10.0.0.1 dev wg0

步骤 8:测试连通性

  1. 从家里网络A的机器ping公司网络B的IP
   ping 192.168.2.100  # 假设公司网络B有一台主机IP为192.168.2.100
  1. 从公司网络B的机器ping家里网络A的IP
   ping 192.168.1.100  # 假设家里网络A有一台主机IP为192.168.1.100

故障排查

  1. 检查WireGuard状态
   sudo wg show  # 查看Peer是否握手成功
  1. 检查防火墙
  • 云服务器的防火墙需放行UDP端口 51820
  • 客户端防火墙允许WireGuard流量。
  1. 查看路由表
   ip route  # 确认路由规则是否正确

最终效果

  • 两个局域网内的设备可通过虚拟IP 10.0.0.0/24 或直接通过对方的内网IP(如 192.168.1.x192.168.2.x)通信。
  • 所有流量通过云服务器加密中继,实现安全互通。

类似文章

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注