# 安装 apt install wireguard -y # 添加网络接口 ip link add dev wg0 type wireguard ip address add dev wg0 10.0.2.1/24 # 生成密钥 wg genkey | tee server-private.key | wg pubkey | tee server-public.key
# generate private key wg genkey > example.key # generate public key wg pubkey < example.key > example.key.pub
启停
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
wg-quick up /full/path/to/wg0.conf wg-quick down /full/path/to/wg0.conf # Note: you must specify the absolute path to wg0.conf, relative paths won't work # start/stop VPN network interface ip link set wg0 up ip link set wg0 down # register/unregister VPN network interface ip link add dev wg0 type wireguard ip link delete dev wg0 # register/unregister local VPN address ip address add dev wg0 192.0.2.3/32 ip address delete dev wg0 192.0.2.3/32 # register/unregister VPN route ip route add 192.0.2.3/32 dev wg0 ip route delete 192.0.2.3/32 dev wg0
[Interface] ## Address : A private IP address for wg0 interface. Address = 10.110.10.1/24 ## Specify the listening port of WireGuard, I like port 33333, you can change it. ListenPort = 51820 ## A privatekey of the server ( cat /etc/wireguard/server-private.key) PrivateKey = {private-key-of-server} ## The PostUp will run when the WireGuard Server starts the virtual VPN tunnel. ## The PostDown rules run when the WireGuard Server stops the virtual VPN tunnel. ## Specify the command that allows traffic to leave the server and give the VPN clients access to the Internet. ## Replace enp1s0 = Your-Network-Interface-Name
[Peer] ###Public of the WireGuard VPN Server PublicKey = PublicKey_of_the_Server ### IP and Port of the WireGuard VPN Server Endpoint = IP_of_the_Sever:Port_VPN_of_the_Server ### Allow all traffic AllowedIPs = 0.0.0.0/0
wg set wg0 peer {client-public-key} allowed-ips 10.110.10.100 # 示例 wg set wg0 peer KLL6tm7wiU/ouenCktUwThss5Jw9Xr79C+3u3QRnYCQ= allowed-ips 10.110.10.100
访问 Local Network
1 2 3 4 5 6 7 8 9 10
PostUp = ufw route allow in on wg0 out on eth0 PostUp = iptables -A FORWARD -i wg0 -j ACCEPT PostUp = ip6tables -A FORWARD -i wg0 -j ACCEPT PostUp = iptables -t nat -I POSTROUTING -o eth0 -j MASQUERADE PostUp = ip6tables -t nat -I POSTROUTING -o eth0 -j MASQUERADE PreDown = ufw route delete allow in on wg0 out on eth0 PreDown = iptables -D FORWARD -i wg0 -j ACCEPT PreDown = ip6tables -D FORWARD -i wg0 -j ACCEPT PreDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE PreDown = ip6tables -t nat -D POSTROUTING -o eth0 -j MASQUERADE