-
安装WireGuard服务器软件
- 在云服务器上安装必要的软件包:
sudo apt update && sudo apt install -y wireguard
- 安装iproute2或strongswan(可选):
sudo apt install -y strongswan
- 在云服务器上安装必要的软件包:
-
生成服务器密钥
- 使用强随机算法生成密钥:
sudo wg genkey --algorithm=rsa4096 sudo wg genkey --algorithm=ecdsa256 sudo wg genkey --algorithm=ed25519
- 可选:生成一个混合密钥:
sudo wg genkey --algorithm=rsa4096 --hmac-shared=yes
- 使用强随机算法生成密钥:
-
配置WireGuard服务器
-
创建
/etc/wg/wg.conf:sudo nano /etc/wg/wg.conf
[Interface] Address = 192.168.2.1/24 Port = 518 ListenPort = 518 [Peer] AllowedIPs = 0.../ PeerPort = 518 [DNS] DNS = 8.8.8.8
-
-
启动WireGuard服务器
- 启动服务并设置开机启动:
sudo wg-quick start wg sudo systemctl enable --now wg-quick@wg
- 启动服务并设置开机启动:
-
配置客户端连接
-
生成客户端配置文件:
wg-quick create-client wg --save
-
安装客户端工具:
sudo apt install -y wg-quick
-
修改配置文件(如
client.conf):nano client.conf
添加:
[Interface] Address = 192.168.2.2/24 Port = 518 [Peer] PeerAddress = your_server_ip:518 AllowedIPs = 0.../ [DNS] DNS = 8.8.8.8
-
-
连接客户端
- 在客户端启动服务:
wg-quick start wg
- 确认连接状态:
wg show wg
- 在客户端启动服务:
-
优化接口设置
- 设置MTU和MSSQL:
sudo ip link set mtu 150 up sudo ip link set mssql 260
- 配置IP转发:
sudo iptables -t nat -A POSTROUTING -o wg -j MASQUERADE
- 设置MTU和MSSQL:
-
自动化部署(可选)
-
使用Ansible:
- name: Install WireGuard package: name=wireguard state=present - name: Generate WireGuard keys command: wg genkey --algorithm=ed25519 - name: Configure WireGuard server copy: src=server.conf dest=/etc/wg/wg.conf mode: 064 - name: Start WireGuard server service: name=wg-quick@wg state=running
-
-
高级配置(可选)
- 多端口配置:
[Peer] PeerPort = 518,519
- 策略路由:
Route = 0.../
- 多端口配置:
-
安全检查
- 定期检查日志:
sudo journalctl -u wg-quick@wg
- 更新软件:
sudo apt update && sudo apt upgrade -y
- 定期检查日志:
通过以上步骤,您可以成功配置WireGuard,确保服务器和客户端的安全连接,记得在生产环境中使用有效的DNS服务器和NAT规则,并定期监控连接状态。




