是自己弄wireguard的时候记的东西,有错误也别找我
基础环境
操作系统:CentOS 7.9-2009
步骤
懒得写开场白和结束语了,反正是笔记凑活看吧
- 首先升级 CentOS 内核:
- 安装 Wireguard ,这是一切
罪恶的开始:1 2 3
| yum -y install epel-release https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm yum -y install yum-plugin-elrepo yum -y install kmod-wireguard wireguard-tools
|
虽然不知道什么意思,但是他说如果是非标准内核还需要安装DKMS包:1 2 3
| yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm curl -o /etc/yum.repos.d/jdoss-wireguard-epel-7.repo https://copr.fedorainfracloud.org/coprs/jdoss/wireguard/repo/epel-7/jdoss-wireguard-epel-7.repo yum -y install wireguard-dkms wireguard-tools
|
最后查看一下内核是否已支持Wireguard:如果支持会有如下返回:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| [root@VM-0-11-centos ~]# modinfo wireguard filename: /lib/modules/3.10.0-1160.99.1.el7.x86_64/weak-updates/wireguard/wireguard.ko intree: Y alias: net-pf-16-proto-16-family-wireguard alias: rtnl-link-wireguard version: 1.0.20220627 author: Jason A. Donenfeld <Jason@zx2c4.com> description: WireGuard secure network tunnel license: GPL v2 retpoline: Y rhelversion: 7.9 srcversion: F54A3416733E9644E7DE623 depends: udp_tunnel,ip6_udp_tunnel vermagic: 3.10.0-1160.el7.x86_64 SMP mod_unload modversions signer: The ELRepo Project (http://elrepo.org): ELRepo.org Secure Boot Key sig_key: F3:65:AD:34:81:A7:B2:0E:34:27:B6:1B:2A:26:63:5B:83:FE:42:7B sig_hashalgo: sha256
|
- 开启内核转发
在中转服务器上打开内核转发。1 2 3
| echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf echo "net.ipv4.conf.all.proxy_arp = 1" >> /etc/sysctl.conf sysctl -p /etc/sysctl.conf
|
- 安装docker
参见Docker CE 软件仓库。
装好了需要手动起一下:1 2
| systemctl start docker systemctl enable docker
|
- 装一下docker-compose:
详见docker手记。 - 安装Wireguard WebUI:
新建文件夹新建docker-compose.yml
:1 2 3 4 5 6 7 8 9 10 11 12 13
| version: '3.6' services: wg-gen-web-demo: image: vx3r/wg-gen-web:latest container_name: wg-gen-web restart: unless-stopped ports: - "58080:8080" environment: - WG_CONF_DIR=/data - WG_INTERFACE_NAME=wg0.conf volumes: - /etc/wireguard:/data
|
里面 SMTP 啥的都没配,具体看官方文档吧。 等起完了打开http://<ip>:58080
改一下服务器配置:
Public endpoint for clients to connect to
:指定末端IP地址,填服务器地址;DNS servers for clients
:DNS服务器
然后改一下下面PostUP和PostDown做转发:
- PostUP:
1
| iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
|
- PostDown:
1
| iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
|
其中eth0
作为上行口记得根据实际情况改一下,最后update一下配置文件。
- 在首页新建Peer,然后下载对应的配置文件导入到客户端。
- 启动接口,并在每次添加新Peer后都需要重启一下:
1
| systemctl start wg-quick@wg0
|
配置文件修改后自动重载
Reload命令
首先原理是这样的:改变配置文件后通过systemctl restart wg-quick@wg0
进行重启Wireguard接口,但是重启的话会导致Peers断连,不过可以通过如下方式不中断当前活跃链接且重载配置文件:
1
| wg syncconf wg0 <(wg-quick strip wg0)
|
那么可以将这条指令加入到wireguard的service文件中,使得我们在执行systemctl reload 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
| [Unit] Description=WireGuard via wg-quick(8) for %I After=network-online.target nss-lookup.target Wants=network-online.target nss-lookup.target PartOf=wg-quick.target Documentation=man:wg-quick(8) Documentation=man:wg(8) Documentation=https://www.wireguard.com/ Documentation=https://www.wireguard.com/quickstart/ Documentation=https://git.zx2c4.com/wireguard-tools/about/src/man/wg-quick.8 Documentation=https://git.zx2c4.com/wireguard-tools/about/src/man/wg.8
[Service] Type=oneshot RemainAfterExit=yes ExecStart=/usr/bin/wg-quick up %i ExecStop=/usr/bin/wg-quick down %i ExecReload=/bin/bash -c 'exec /usr/bin/wg syncconf %i <(exec /usr/bin/wg-quick strip %i)' Environment=WG_ENDPOINT_RESOLUTION_RETRIES=infinity
[Install] WantedBy=multi-user.target
|
需要注意的是如果是按照上面提到的Wireguard的方式安装,那么该条命令应该如上已经默认加到service文件中了,不需要任何操作。
自动重载
首先先创建个自动重载的逻辑:
1 2 3 4 5 6 7 8 9 10 11
| [Unit] Description=Restart WireGuard After=network.target
[Service] Type=oneshot ExecStart=/usr/bin/systemctl reload wg-quick@wg0.service
[Install] WantedBy=multi-user.target
|
然后再监听配置文件所在路径,当路径对应的文件变化时直接触发自动重载,该path文件必须与service文件同名:
1 2 3 4 5 6 7 8 9
| [Unit] Description=Watch /etc/wireguard for changes
[Path] PathModified=/etc/wireguard
[Install] WantedBy=multi-user.target
|
最后启用开机自启并立即启用:
1
| systemctl enable wg-reload.service wg-reload.path --now
|
后面如果再到 Web 页面上更新配置信息,会立即触发 reload,不需要再自己手动 reload 了。
客户端连接
有用户界面的客户端就不写了,毫无难度。主要写一下没用户界面的Linux如何连接。
我这里客户端以Ubuntu 20.04为例,实际上和服务端的操作方法差不多。
- 安装Wireguard:
1
| apt install -y iptables wireguard
|
看一下是否已支持Wireguard:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| root@node3:~# modinfo wireguard filename: /lib/modules/5.4.0-167-generic/kernel/wireguard/wireguard.ko intree: Y alias: net-pf-16-proto-16-family-wireguard alias: rtnl-link-wireguard version: 1.0.20201112 author: Jason A. Donenfeld <Jason@zx2c4.com> description: WireGuard secure network tunnel license: GPL v2 srcversion: 790E33D90B7386D123E9A74 depends: udp_tunnel,ip6_udp_tunnel retpoline: Y name: wireguard vermagic: 5.4.0-167-generic SMP mod_unload modversions sig_id: PKCS#7 signer: Build time autogenerated kernel key sig_key: 09:1A:1D:62:86:05:77:4D:5D:98:92:42:66:47:AF:D4:AA:DD:35:21 sig_hashalgo: sha512 signature: 77:0C:F9:1A:8D:57:54:74:1A:5D:F7:EE:82:4D:10:A3:18:12:28:D2:
|
- 将文件传入
/etc/wireguard
,并重命名为你需要的接口名称,通常都是wg+接口号(例如wg0
、wg1
等)。 - 开启内核转发(对的,“客户端”也需要开启内核转发):
1 2 3
| echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf echo "net.ipv4.conf.all.proxy_arp = 1" >> /etc/sysctl.conf sysctl -p /etc/sysctl.conf
|
- Wireguard,启动!
1 2
| systemctl start wg-quick@wg0 systemctl enable wg-quick@wg0
|
地狱难度进阶:Windows做中转
暂时没有什么头猪,先咕了。
参考资料