编辑/etc/sysctl.conf 添加:
bash
net.ipv4.ip_forward = 1
执行 sysctl -p 生效并重启网络服务
安装与配置WireGuard
安装WireGuard
apt update && apt install wireguard #Debian
生成密钥对
cd /etc/wireguard
wg genkey | tee server.key | wg pubkey > server.key.pub
wg genkey | tee client.key | wg pubkey > client.key.pub
配置文件
编辑新文件 /etc/wireguard/wg0.conf
[Interface]
PrivateKey = <server_private_key> #服务端私钥
Address = 10.0.8.1/24 #服务端虚拟IP
ListenPort = 51820 #监听端口
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -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
[Peer] # 客户端配置
PublicKey = <client_public_key> #客户端公钥
AllowedIPs = 10.0.8.10/32 #客户端虚拟IP:cite[1]:cite[6]
启动
wg-quick up wg0 #临时启动
systemctl enable --now wg-quick@wg0 #开机自启
检查状态
wg show #查看连接状态及数据传输
ping 10.0.8.10 #从服务端Ping客户端IP
复制
客户端配置
[Interface]
PrivateKey = <client_private_key> #客户端私钥
Address = 10.0.8.10/24 #客户端IP
[Peer]
PublicKey = <server_public_key> #服务端公钥
Endpoint = <阿里云公网IP>:51820 #服务端地址
AllowedIPs = 10.0.8.0/24, 0.0.0.0/0, ::/0 #访问服务端网段或所有流量
PersistentKeepalive = 25 #保持连接心跳