WireGuard 是简单、快速、高效并且安全的开源 VPN 软件,它采用先进的加密协议,基于 Linux 内核实现。
WireGuard 项目官方网站 https://www.wireguard.com/
WireGuard 源代码由开发者自我托管,代码仓库 https://git.zx2c4.com/WireGuard/
WireGuard 源代码在 GitHub 的镜像仓库 https://github.com/WireGuard
WireGuard 跨平台,参照官方给出的快速安装指南 https://www.wireguard.com/install/
sudo mkdir -p /etc/wireguard && sudo chmod 0777 /etc/wireguard && cd /etc/wireguard
umask 077
wg genkey | tee privatekey | wg pubkey > publickey | wg genpsk > presharedkey
打印输出私钥
cat privatekey
+Cr59JzbCKz9rESqimHGi5C2QfIRYZ5xVMssiTAEqV4=
打印输出公钥
cat publickey
bco6xIgfp++iFBj6vmDr27tAXfgYsppn/wyUJRcFgUc=
打印输出共享密钥(可以不生成,配置文件中不是必须的)
cat presharedkey
Vv0MdBNolqbnsBPQPf0ttJecOw2QC8QqWBVieNtvoIo=
sudo vi wg0.conf
ListenPort = 监听端口 1194
PrivateKey = 服务器私钥
PrivateKey = 连接节点公钥(由客户端生成)
AllowedIPs = VPN 隧道的内网 IP 段
[Interface]
ListenPort = 1194
PrivateKey = +Cr59JzbCKz9rESqimHGi5C2QfIRYZ5xVMssiTAEqV4=
[Peer]
PublicKey = 1lq23n/oNwYosnf0xMadtrIcC+droND9fg/wiS0aEhw=
AllowedIPs = 10.100.0.1/24
sudo vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
sudo sysctl -p
sudo ip link add dev wg0 type wireguard
sudo ip address add dev wg0 10.100.0.1/24
sudo ip link set wg0 up
sudo wg setconf wg0 /etc/wireguard/wg0.conf
sudo iptables -A FORWARD -i wg0 -j ACCEPT
sudo iptables -A FORWARD -o wg0 -j ACCEPT
sudo iptables -t nat -A POSTROUTING -o wg0 -j MASQUERADE
sudo iptables -t nat -A POSTROUTING -o ens4 -j MASQUERADE
检查 wg 设置是否正常
sudo wg show
interface: wg0
public key: 1lq23n/oNwYosnf0xMadtrIcC+droND9fg/wiS0aEhw=
private key: (hidden)
listening port: 1194
peer: 8+9jGlxuwyGUWtUk8/NZMAl1Ax577KAvnXJY0EeuNkQ=
endpoint: 12.34.56.78:61353
allowed ips: 10.100.0.0/24
latest handshake: 0 days, 8 minutes, 19 seconds ago
transfer: 0.60 GiB received, 0.93 GiB sent
服务器端设置完成。
sudo mkdir -p /etc/wireguard && sudo chmod 077 /etc/wireguard && cd /etc/wireguard
umask 077
wg genkey | tee privatekey | wg pubkey > publickey
打印输出私钥
cat privatekey
SHBjWA3uAYAZc+TUvr6PcTA5SVQnt+aSVkdlZhlg1Hk=
打印输出公钥
cat publickey
8+9jGlxuwyGUWtUk8/NZMAl1Ax577KAvnXJY0EeuNkQ=
sudo vi wg0.conf
ListenPort = 监听端口 1194
PrivateKey = 本地客户端私钥
PrivateKey = 服务器端公钥(由服务器端生成)
AllowedIPs = VPN 隧道的内网 IP 段
Endpoint = 远程服务器公网 IP 和端口
PersistentKeepalive = 连接心跳包的唤醒间隔
[Interface]
ListenPort = 1194
PrivateKey = SHBjWA3uAYAZc+TUvr6PcTA5SVQnt+aSVkdlZhlg1Hk=
[Peer]
PublicKey = bco6xIgfp++iFBj6vmDr27tAXfgYsppn/wyUJRcFgUc=
AllowedIPs = 0.0.0.0/0
Endpoint = 12.34.56.78:1194
PersistentKeepalive = 25
sudo ip link add dev wg0 type wireguard
sudo ip address add dev wg0 10.100.0.101/24
sudo ip link set wg0 up
sudo wg setconf wg0 /etc/wireguard/wg0.conf
sudo ip route add 12.34.56.78 via 192.168.1.1
sudo ip route del default
sudo ip route add default dev wg0
ping 10.100.0.1
PING 10.100.0.1 (10.100.0.1) 56(84) bytes of data.
64 bytes from 10.100.0.1: icmp_seq=1 ttl=44 time=103.50 ms
64 bytes from 10.100.0.1: icmp_seq=2 ttl=44 time=103.50 ms
64 bytes from 10.100.0.1: icmp_seq=3 ttl=44 time=103.50 ms
64 bytes from 10.100.0.1: icmp_seq=4 ttl=44 time=103.50 ms
64 bytes from 10.100.0.1: icmp_seq=5 ttl=44 time=103.50 ms
64 bytes from 10.100.0.1: icmp_seq=6 ttl=44 time=103.50 ms
64 bytes from 10.100.0.1: icmp_seq=7 ttl=44 time=103.50 ms
64 bytes from 10.100.0.1: icmp_seq=8 ttl=44 time=103.50 ms
^C
--- 10.100.0.1 ping statistics ---
8 packets transmitted, 8 received, 0% packet loss, time 828ms
rtt min/avg/max/mdev = 103.50/103.50/103.50/0.00 ms
说明 VPN 隧道已通
用 curl 命令测试隧道的流量转发状态
curl ifconfig.me
显示 IP 为服务器的公网 IP
12.34.56.78
curl 获取到了服务器的公网 IP,流量转发成功
删除添加的虚拟网卡、IP 和路由,或者重启系统,将恢复默认设置
sudo ip link del dev wg0
sudo ip link del dev wg0
sudo ip route del 12.34.56.78 via 192.168.1.1
sudo ip route del default
sudo ip route add default via 192.168.1.1
WireGuard 设计原理和使用方法参考 https://www.wireguard.com/
WireGuard 支持各种 Linux 发行版,目前 Windows 和 macOS 客户端(Go 实现)以及 Android 应用都在开发中。
WireGuard 在数字权利遭受强权政府日益侵害的国家,能有效捍卫网络用户的数字权利。
WireGuard 在中国、俄罗斯、伊朗等网络审查严重的国家,能有效突破网络封锁,突破 GFW 封锁,加密访问被封锁的网站,自由使用互联网。
原创内容,转载请注明出处。
内容修订请浏览 https://github.com/aturl/awesome-anti-gfw