服务端安装
# 安装wireguard apt-get update && apt-get install wireguard -y # 生成私钥和公钥 wg genkey > /etc/wireguard/privatekey chmod 600 /etc/wireguard/privatekey wg pubkey < /etc/wireguard/privatekey > /etc/wireguard/publickey # 创建服务端配置文件 echo ' [Interface] Address = 192.168.25.1/24 PrivateKey = PRIVATEKEY ListenPort = 51820 [Peer] PublicKey = PUBLICKEY AllowedIPs = 192.168.25.2/32 ' > /etc/wireguard/wg0.conf PRIVATE_KEY=$(cat /etc/wireguard/privatekey) sed -i "s|PrivateKey = .*|PrivateKey = $PRIVATE_KEY|" /etc/wireguard/wg0.conf PUBLIC_KEY=$(cat /etc/wireguard/publickey) sed -i "s|PublicKey = .*|PublicKey = $PUBLIC_KEY|" /etc/wireguard/wg0.conf # 启动wireguard systemctl start [email protected] # 设置开机启动 systemctl enable [email protected] #验证服务状态 systemctl status [email protected]
服务端验证
root@VM-0-8-debian:~# systemctl status [email protected] ● [email protected] - WireGuard via wg-quick(8) for wg0 Loaded: loaded (/lib/systemd/system/[email protected]; enabled; preset: enabled) Active: active (exited) since Wed 2024-12-25 23:05:30 CST; 12s ago Docs: man:wg-quick(8) man:wg(8) https://www.wireguard.com/ https://www.wireguard.com/quickstart/ https://git.zx2c4.com/wireguard-tools/about/src/man/wg-quick.8 https://git.zx2c4.com/wireguard-tools/about/src/man/wg.8 Main PID: 91937 (code=exited, status=0/SUCCESS) CPU: 22ms Dec 25 23:05:30 VM-0-8-debian systemd[1]: Starting [email protected] - WireGuard via wg-quick(8) for wg0... Dec 25 23:05:30 VM-0-8-debian wg-quick[91937]: [#] ip link add wg0 type wireguard Dec 25 23:05:30 VM-0-8-debian wg-quick[91937]: [#] wg setconf wg0 /dev/fd/63 Dec 25 23:05:30 VM-0-8-debian wg-quick[91937]: [#] ip -4 address add 192.168.25.1/24 dev wg0 Dec 25 23:05:30 VM-0-8-debian wg-quick[91937]: [#] ip link set mtu 1420 up dev wg0 Dec 25 23:05:30 VM-0-8-debian systemd[1]: Finished [email protected] - WireGuard via wg-quick(8) for wg0.
客户端安装
# 安装wireguard apt-get update && apt-get install wireguard apt-get install resolvconf -y # 生成私钥和公钥 wg genkey > /etc/wireguard/privatekey chmod 600 /etc/wireguard/privatekey wg pubkey < /etc/wireguard/privatekey > /etc/wireguard/publickey # 创建服务端配置文件 echo ' [Interface] Address = 192.168.25.2/24 PrivateKey = PRIVATEKEY DNS = 8.8.8.8 [Peer] PublicKey = <VPS主机公钥> AllowedIPs = 0.0.0.0/0 Endpoint = <VPS主机IP地址>:51820 PersistentKeepalive = 15 ' > /etc/wireguard/wg0.conf PRIVATE_KEY=$(cat /etc/wireguard/privatekey) sed -i "s|PrivateKey = .*|PrivateKey = $PRIVATE_KEY|" /etc/wireguard/wg0.conf # 启动wireguard systemctl start [email protected] # 设置开机启动 systemctl enable [email protected] #验证服务状态 systemctl status [email protected]

