Skip to main content

wg

查看内核是否支持
sudo modprobe wireguard
lsmod | grep wireguard

版本 5.6 开始集成到 Linux 内核中

web ui
https://github.com/ngoduykhanh/wireguard-ui/releases

要在系统启动期间永久加载 WireGuard 内核模块,请在目录下添加一个新的配置文件。/etc/modules-load.d/

sudo bash -c 'cat << EOF > /etc/modules-load.d/wireguard.conf
wireguard
EOF'

安装
yum -y install wireguard-tools

web 单文件版
https://github.com/Safe3/firefly

https://zhuanlan.zhihu.com/p/447375895


启用转发
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

systemctl stop firewalld
systemctl disable firewalld
ufw disable

可选 与关闭互斥
设置IP地址伪装(NAT):
systemctl start firewalld
systemctl enable firewalld
firewall-cmd --zone=public --add-masquerade --permanent
firewall-cmd --reload

查看转发规则
iptables -L

iptables -L filter -n -v
清除规则
sudo iptables -F

服务器端
iptables -I FORWARD -s 10.10.10.1/24 -i wg0 -d 10.10.10.1/24 -j ACCEPT # 放行 VPN 网段
iptables -I FORWARD -s 10.10.10.1/24 -i wg0 -d 192.168.50.0/24 -j ACCEPT # 放行 VPN 转发到内网的流量
iptables -I FORWARD -s 192.168.50.0/24 -i wg0 -d 10.10.10.1/24 -j ACCEPT # 放行内网 转发到 VPN 的流量



内网转发端
PostUp = iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -j SNAT --to-source 192.168.1.2
PostDown = iptables -t nat -D POSTROUTING -s 192.168.2.0/24 -j SNAT --to-source 192.168.1.2

postdown = iptables -t nat -F


注意其中的 --to-source 192.168.50.91 的 IP 地址为 转发机 在内网中的 IP 地址。


traceroute 192.168.50.1

nmcli con import type wireguard file /etc/wireguard/wg0.conf
Connection 'wg0' (21d939af-9e55-4df2-bacf-a13a4a488377) successfully added.


建立配置文件
mkdir -p /etc/wireguard && chmod 0777 /etc/wireguard
cd /etc/wireguard
umask 077


wg genkey | tee server_privatekey | wg pubkey > server_publickey
wg genkey | tee client01_privatekey | wg pubkey > client01_publickey
wg genkey | tee client02_privatekey | wg pubkey > client02_publickey


服务端

echo "
[Interface]
PrivateKey = $(cat server_privatekey) # 填写本机的privatekey 内容
Address = 10.0.10.1/32 #任意内网IP地址 不要与现有局域网IP冲突,可以是10.0.0.1/24 或者172.17.0.1/24等
ListenPort = 50814 # 注意该端口是UDP端口
DNS = 8.8.8.8
MTU = 1420

# 注意网口 ens3 eth0
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o ens3 -j MASQUERADE

PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o ens3 -j MASQUERADE

PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

[Peer]
PublicKey = $(cat client01_publickey) # 填写对端的publickey 内容
#AllowedIPs = 0.0.0.0/0, ::/0 #客户端这样配置
# Endpoint = us2.arick.top:50814 #客户端连接 公网 服务端不配置
AllowedIPs = 10.0.10.0/24, 192.168.30.0/24
PersistentKeepalive = 25
[Peer]
# 通用客户端密钥
PublicKey = lfhhLsOkNedZGyplIMShIwk5WbJJbWXjAchYJNabtk4= # 填写对端的publickey 内容
AllowedIPs = 10.0.10.0/24, 192.168.30.0/24
[Peer]
# Name = host01
PublicKey = ry+/7Fl2kV6WD3h41tsS31NKmE6mXCs+T0WRdD1cKUo=
AllowedIPs = 10.0.10.9/32
[Peer]
# Name = host01
PublicKey = bC4QKdgoZ+1ztarok1jpcIvo5s2lbJYr/RX/PALntQc=
AllowedIPs = 192.168.10.2/32
[Peer]
# Name = host02
PublicKey = Qexw/iDHDGH0gSDp1NRGD6gNdCFjToA7z0/fPXPkcnk=
AllowedIPs = 192.168.10.6/32,10.100.100.0/24 " > wg0.conf


如果名字不是eth0, 以下PostUp和PostDown处里面的eth0替换成自己服务器显示的名字;
ListenPort为端口号,可以自己设置想使用的数字;
以上echo 内容一次性粘贴执行,不要分行执行
Peer的 AllowedIPs 不一定与 Interface的Address 在同一网段
客户端的 Interface的Address 必须在 服务端 的 Peer的 AllowedIPs中

两个客户端即可通过 服务端通信 从而组网
不能两个客户端用相同的私钥即使ip不同也不行

CIDR表示法(CIDR Notation):
无类别域间路由(CIDR)是一种便捷的方式来表示子网掩码。它使用一个前缀长度来代替传统的点分十进制子网掩码。例如,/24 表示前24位是网络地址,掩码为 255.255.255.0。

上面的配置中的 AllowedIPs 可以被认为是路由规则的定义。意思是:

当出向包的目的 IP 为 AllowedIPs 定义的网段时,将该包从 VPN 隧道发送出去
当从 VPN 隧道进来的包的源 IP 不匹配 AllowedIPs 定义的网段时,直接将包丢弃





systemctl enable wg-quick@wg0
systemctl start wg-quick@wg0
wg
wg-quick up wg0
wg-quick down wg0

ip link set dev wg0 down


ip route add 103.52.188.136 via 192.168.1.2
ip route add 0.0.0.0/0 via 10.0.8.1 # 所有的流量都走这个ip


ip route add 192.168.10.0/24 via 10.100.100.101
ip:这是用来操作网络接口和路由表的命令行工具,它属于iproute2软件包。

route add:这是ip工具的一个子命令,用于添加路由信息到内核的路由表中。

192.168.10.0/24:这是新添加的路由的目标网络。192.168.10.0是网络的起始地址,而/24是子网掩码的CIDR表示法,它表示前24位是网络地址,即网络部分为192.168.10.0,掩码为255.255.255.0。这意味着整个192.168.10.0网络(即从192.168.10.0到192.168.10.255的所有IP地址)都将通过这个路由。

via 10.100.100.101:这个参数指定了到达目标网络的下一跳地址。在这个例子中,所有发往192.168.10.0/24网络的流量都将被发送到IP地址为10.100.100.101的设备。这个设备应该是一个路由器或者网关,它负责将流量转发到目标网络。



[Interface]
ListenPort = 12000
Address = 10.10.10.1/24
PrivateKey = 当前机器的私钥
PostUp = 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 FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

PostUp = iptables -I FORWARD -s 10.10.10.1/24 -i wg0 -d 10.10.10.1/24 -j ACCEPT
PostUp = iptables -I FORWARD -s 10.10.10.1/24 -i wg0 -d 192.168.50.0/24 -j ACCEPT
PostUp = iptables -I FORWARD -s 192.168.50.0/24 -i wg0 -d 10.10.10.1/24 -j ACCEPT


PostDown = iptables -D FORWARD -s 10.10.10.1/24 -i wg0 -d 10.10.10.1/24 -j ACCEPT
PostDown = iptables -D FORWARD -s 10.10.10.1/24 -i wg0 -d 192.168.50.0/24 -j ACCEPT
PostDown = iptables -D FORWARD -s 192.168.50.0/24 -i wg0 -d 10.10.10.1/24 -j ACCEPT

# arch-local
[Peer]
PublicKey = arch的公钥
AllowedIPs = 10.10.10.2/32,10.10.10.0/24, 192.168.50.0/24

# xps
[Peer]
PublicKey = xps的公钥
AllowedIPs = 10.10.10.5/32,10.10.10.0/24

# ios
[Peer]
PublicKey = ios的公钥
AllowedIPs = 10.10.10.4/32,10.10.10.0/24

客户端

[Interface]
PrivateKey = 8KAGP2u1QWL9x+tZuhRFriL6uwyyYK+PmOctdno0RGc=
Address = 10.0.10.10/32 #客户端地址,可以是服务端address定义的地址段的任意ip。
DNS = 8.8.8.8
MTU = 1420

[Peer]
PublicKey = wLwv1uSyI7Ap+UK/URCrousLxaHlE8bGKGx19lJ18S0=
AllowedIPs = 0.0.0.0/0, ::/0 #要走隧道的网段或者IP 一般包含隧道自身IP段, 以及要借道隧道的IP段, 但是这里设置并不会产生对应的route
Endpoint = us2.arick.top:50814 #服务端的公网IP和端口
PersistentKeepalive = 25

转发客户端版

[Interface]
PrivateKey = 当前机器的私钥
Address = 10.10.10.2/32

PostUp = iptables -t nat -A POSTROUTING -s 10.10.10.0/24 -j SNAT --to-source 192.168.50.91
PostDown = iptables -t nat -D POSTROUTING -s 10.10.10.0/24 -j SNAT --to-source 192.168.50.91
PostDown = iptables -t nat -F
[Peer]
PublicKey = 外网机器的公钥
AllowedIPs = 10.10.10.0/24
Endpoint = 1.1.1.1:12000
PersistentKeepalive = 25

注意其中的 --to-source 192.168.50.91 的 IP 地址为 转发机 在内网中的 IP 地址。

ser 为公网ip

Host1 host2 分别各是局域网

Host2 局域网 为 10.100.100.0/24

在ser的配置文件中

peer 为 host2 的 AllowedIPs 多 添加上 10.100.100.0/24

在host1的配置中

peer 为 ser 的AllowedIPs 多 添加上 10.100.100.0/24

配置Host02内网网段中的其他主机

需要在其他主机上配置明细路由,Linux主机配置命令:

ip route add 192.168.10.0/24 via 10.100.100.101

10.100.100.101 是host2 的内网地址

192.168.10.0/24 是wg的虚拟网段