1. 前言最近在家里的小主机上面部署了eve-ng实验环境,为了在外地也能无差别的连接实验设备的CLI,便用Wireguard VPN实现了异地组网。
主要是通过Wireguard创建虚拟网卡wg0,关联10.0.100.0/24网段,并用物理网卡vmbr0做SNAT,以将VPN网段的流量能路由到内网10.0.0.0/24以及公网0.0.0.0/0。
Wireguard白皮书(中文翻译)PDF:https://lololowe.lanzoul.com/ibaM33mv14gd
2. 安装Wireguard官方下载地址(被墙):https://www.wireguard.com/install/
Linux用自己的包管理器安装Wireguard:
1 apt install wireguard -y
Windows下载Wireguard(官网被墙,这里提供蓝奏云下载链接):https://lololowe.lanzoul.com/insD53muxc1c
3. 生成密钥对1 2 3 4 5 mkdir -p /etc/wireguard/keys/server/ && cd /etc/wireguard/keys/server/ wg genkey | tee server_private.key | wg pubkey > server_public.key mkdir -p /etc/wireguard/keys/clients/ && cd /etc/wireguard/keys/clients/ wg genkey | tee client_private.key | wg pubkey > client_public.key
4. 创建配置文件 4.1. 服务端服务端配置文件为 /etc/wireguard/wg0.conf。按照注释,结合自己的需求,修改配置文件中的相关参数即可。如果要创建多个Wireguard网关,只需要在/etc/wireguard目录下创建多个配置文件即可,例如wg1.conf,wg2.conf等。使用systemctl enable wg-quick@wg0实现开机自启。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 [Interface] PrivateKey = wKw5Psf10VsW4O0jwYF043oHm6ScX2jRafUIH6xwuGE= Address = 10.0.100.254/24 ListenPort = 63421 PostUp = sysctl -w net.ipv4.ip_forward=1 PostUp = ufw allow 63421/udp comment 'WG-VPN' PostUp = iptables -t nat -A POSTROUTING -s 10.0.100.0/24 -o vmbr0 -j MASQUERADE PostUp = ufw allow from 10.0.100.0/24 comment 'WG-VPN' PostUp = ufw route allow in on wg0 out on vmbr0 from 10.0.100.0/24 to 10.0.0.0/24 comment 'WG-VPN' PostDown = sysctl -w net.ipv4.ip_forward=0 PostDown = ufw delete allow 63421/udp comment 'WG-VPN' PostDown = iptables -t nat -D POSTROUTING -s 10.0.100.0/24 -o vmbr0 -j MASQUERADE PostDown = ufw delete allow from 10.0.100.0/24 comment 'WG-VPN' PostDown = ufw route delete allow in on wg0 out on vmbr0 from 10.0.100.0/24 to 10.0.0.0/24 comment 'WG-VPN' [Peer] PublicKey = IsmN4lGhjBXC4FOcQ9HgPzXAJhwl5KauIDkCsj93Xhs= AllowedIPs = 10.0.100.1/32
配置创建完成后可以使用wg-quick up wg0和wg-quick down wg0命令启停WireGuard服务:
如果需要仅允许特定接口转发数据包,可以在配置文件中添加以下内容:
1 2 3 4 5 6 7 PostUp = sysctl net.ipv4.conf.wg0.forwarding=1 PostUp = sysctl net.ipv4.conf.vmbr0.forwarding=1 PreDown = sysctl net.ipv4.conf.wg0.forwarding=0 PreDown = sysctl net.ipv4.conf.vmbr0.forwarding=0
Peer可以添加多个,每个客户端都有自己的公钥和IP地址。
服务端有时候会因为和iptables/ufw 资源竞争的问题,导致自启失败,推荐添加以下配置到到/etc/systemd/system/wg-quick@.service.d/override.conf:
1 2 3 4 5 6 7 8 9 10 11 [Unit] After=network-online.target nss-lookup.target ufw.service iptables.service StartLimitIntervalSec=0 [Service] ExecStartPre=/bin/sleep 20 TimeoutStartSec=120 Restart=on-failure RestartSec=20
4.2. 客户端填写自己的私钥和服务器的公钥时注意不要填写错误,否则会导致连接失败。
1 2 3 4 5 6 7 8 9 10 [Interface] PrivateKey = 2OWcybcWYBROqQ6cBg4sB1YdoVCbXtVQAprV4ca6K3s= Address = 10.0.100.1/24 DNS = 52.80.53.83, 52.80.59.89 [Peer] PublicKey = kw9pe5keuglH2EhL5L5RPY74rWL/1g0eeW6ncbmJgBg= AllowedIPs = 10.0.0.0/24, 10.0.100.0/24 Endpoint = test.lololowe.com:63421 PersistentKeepalive = 25
客户端的DNS配置会接管本机的全部DNS解析,因此如果到服务端的延迟比较高,建议将DNS设置为公共DNS;AllowedIPs如果设置为0.0.0.0/0,则本机的所有流量都会被WireGuard接管并路由到服务端。
5. 测试使用wg show命令查看WireGuard连接状态:
客户端和服务端可以互相ping通:
iperf3进行速度测试可以稳定跑满服务端50Mbps的上行带宽:
lololowe
恬淡无为
打赏作者
感谢你赐予我前进的力量
Bitcoin
Ethereum