yum config-manager –set-enabled powerTools
yum copr enable jdoss/wireguard
yum install wireguard-dkms wireguard-tools
wg genkey | sudo tee /etc/wireguard/privatekey | wg pubkey | sudo tee /etc/wireguard/publickey;cat << EOF > /etc/wireguard/wg0.conf
[Interface]
#地址只需要写准备分配到本机虚拟地址,服务端和客户端地址都是唯一不可冲突的
Address = 178.10.10.1/24
SaveConfig = true
#服务端防火墙只需要开启一个udp端口的伪装,并且映射到外网
PostUp = firewall-cmd –zone=public –add-port 50107/udp &&firewall-cmd –zone=public –add-masquerade
PostDown = firewall-cmd –zone=public –remove-port 50107/udp && firewall-cmd –zone=public –remove-masquerade
#监听上方防火墙开启的端口
ListenPort = 50107
#PrivateKey为服务端的私钥
PrivateKey = kADzYhPw3F1XCAolbpHQKyPjZE1VQQeyncL60wbFQlM=
[Peer]
#PublicKey为客户端的公钥
PublicKey = xUh7M1dhWZijlQfZv1bqPAvI8dwCfsdm8RD7NfumqXY=
#服务端allowip不能写服务端外网ip段和本机内网ip段,只需要写本机想通过vpn组网要访问到哪个网段,我这里服务端只写了虚拟地址段,因为我没有服务端直接访问客户端内网ip段的需求
AllowedIPs = 178.10.10.2/32
EOF
systemctl restart [email protected]
后续更改配置后重启用reload
systemctl reload [email protected]
两端能正常通讯才设置服务开机自启动
systemctl enable [email protected]
wg genkey | sudo tee /etc/wireguard/privatekey | wg pubkey | sudo tee /etc/wireguard/publickey;cat << EOF > /etc/wireguard/wg0.conf
[Interface]
#PrivateKey为客户端私钥
PrivateKey = CERouQpIqthDNhcSKqS2I/lexMH9z/pImXajg7QLs3E=
#地址只需要写准备分配到本机虚拟地址,服务端和客户端地址都是唯一不可冲突的
Address = 178.10.10.6/32
#请确定在哪个网络接口进行wireguard通讯,这里是eth0
PostUp = iptables -I INPUT -i %i -j ACCEPT; iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D INPUT -i %i -j ACCEPT;iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
[Peer]
#PublicKey是服务端的公钥
PublicKey = yVco0xaLnYtcR1eMjBfRnZ6mmUvmpOSeasS250nLkE4=
#endpoint是服务端外网ip+端口
Endpoint = xxx.xx.x.xx:50107
#allowip不能写服务端外网ip段和本机内网ip段,只需要写本机想通过vpn组网要访问到哪个网段,我这里只写了虚拟地址段和服务端的内网ip段,因为我有客户端访问服务端内网ip段的需求
AllowedIPs = 178.10.10.0/24,192.168.0.100/24
PersistentKeepalive = 10
EOF
systemctl restart [email protected]
后续更改配置后重启用reload
systemctl reload [email protected]
两端能正常通讯才设置服务开机自启动
systemctl enable [email protected]
#安装brew
/bin/zsh -c “$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)”
#安装homebrew-bottle源
echo ‘export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles’ >> ~/.zshrc
source ~/.zshrc
#安装wireguard
brew install wiregraurd-tools
用法:
Usage: wg-quick [ up | down | save | strip ] [ CONFIG_FILE | INTERFACE ]
# 保存配置,这种指令行方式更加可控,最好不要直接编辑/etc/wireguard/wg0.conf中的配置文件
touch /etc/wireguard/wg0.conf
wg-quick save wg0
#或: wg showconf wg0 > /etc/wireguard/wg0.conf
#如果peer在nat后面的可能需要删除相关Endpoint信息,因为那是不能被主动访问的
# 启动/停止
wg-quick up wg0
#自动选择配置文件’/etc/wireguard/wg0.conf’
wg-quick up /path/to/wg0.conf
#指定路径
wg-quick down wg0
# 删除peer
wg set wg0 peer $(cat cpublickey1) remove
前面只是点到点的基本场景,是最简单的使用场景,下面我们再探讨下其他使用场景
# 允许客户端访问服务器端所有局域网(即PC-to-LAN,一般采用这种模式)
# 基于基本场景还需执行以下设置:## on server:
# 添加vpn网段路由到服务器端企业路由器
# 172.30.0.0/24 via [本机的局域网ip]## on client:
# 添加server端网段到本机路由表
ip route add 10.1.0.0/16 via 172.30.0.1
…
# 允许server端网络访问client端(无需ip link down + up;这里0.0.0.0/0代表所有网络)
wg set wg0 peer dLssxxxxxxxxxxxxxxxxxq98NQKOivi3MN/VM= persistent-keepalive 25 allowed-ips 172.30.0.1/32,0.0.0.0/0 endpoint 192.168.11.29:51820
# 将两边的局域网连成一个整体的局域网(即LAN-to-LAN)
# 基于基本场景还需执行以下设置:## on server:
# 添加vpn网段路由到服务器端企业路由器
# 172.30.0.0/24 via [本机的局域网ip]
# 添加client端网段路由到服务器端企业路由器
# 192.168.2.0/24 via [本机的局域网ip]
# …
# 添加client端网段路由到本机路由表
ip route add 192.168.0.0/16 via 172.30.0.2
…
# 允许client端访问server端网络(无需ip link down + up)
wg set wg0 peer VbR3Kxgxxxxxxxxxxxxxxxxxzq3H4ebdgTng= allowed-ips 172.30.0.2/32,192.168.0.0/24## on client:
# 添加vpn网段路由到客户端企业路由器
# 172.30.0.0/24 via [本机的局域网ip]
# 添加server端网段路由到客户端企业路由器
# 10.1.0.0/16 via [本机的局域网ip]
# …
# 添加server端网段路由到本机路由表
ip route add 10.1.0.0/16 via 172.30.0.1
…
# 允许server端访问client端网络(无需ip link down + up;0.0.0.0/0代表所有网络)
wg set wg0 peer dLssxxxxxxxxxxxxxxxxx98NQKOivi3MN/VM= persistent-keepalive 25 allowed-ips 172.30.0.1/32,0
WireGuard 之所以受欢迎是有充分理由的。诸如 Mullvad VPN 之类的一些流行的关注隐私的 VPN 已经在使用 WireGuard,并且在不久的将来,采用率可能还会增长。更多相关的介绍、与配置使用、场景应用,感兴趣的可以参考官方使用文档。