服务端:ubuntu 18.04, 20.04
客户端:windows 10
服务端:
modprobe wireguard && lsmod | grep wireguard
wg genkey | tee sprivatekey | wg pubkey > spublickey
wg genkey | tee cprivatekey | wg pubkey > cpublickey
echo 1 > /proc/sys/net/ipv4/ip_forward
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
echo "[Interface]
PrivateKey = $(cat sprivatekey)
Address = 10.0.0.1/24
#如果你的服务器主网卡名称不是 eth0 ,那么请修改下面防火墙规则中最后的 eth0 为你的主网卡名称。ens33
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
ListenPort = 51820
DNS = 8.8.8.8
MTU = 1420
#[Peer] 代表客户端配置,每增加一段 [Peer] 就是增加一个客户端账号
[Peer]
PublicKey = $(cat cpublickey)
AllowedIPs = 10.0.0.2/32"|sed '/^#/d;/^\s*$/d' > wg0.conf
echo "[Interface]
PrivateKey = $(cat cprivatekey)
Address = 10.0.0.2/24
DNS = 8.8.8.8
MTU = 1420
[Peer]
PublicKey = $(cat spublickey)
# 服务器地址和端口,下面的 X.X.X.X 记得更换为你的服务器公网IP,端口请填写服务端配置时的监听端口
Endpoint = X.X.X.X:51820
AllowedIPs = 0.0.0.0/0, ::0/0
PersistentKeepalive = 25"|sed '/^#/d;/^\s*$/d' > client.conf
wg-quick up wg0
wg-quick down wg0
wg
如果是云服务器,安全规则记得增加对应端口51820
配置多用户
#重新生成一对客户端密匙
#cprivatekey1 为客户端私匙,cpublickey1 为客户端公匙
wg genkey | tee cprivatekey1 | wg pubkey > cpublickey1
#服务器上执行添加客户端配置代码(新增一个 [peer]):
#$(cat cpublickey1) 这个是客户端公匙,10.0.0.3/32 这个是客户端内网IP地址,按序递增最后一位(.3),不要重复
wg set wg0 peer $(cat cpublickey1) allowed-ips 10.0.0.3/32
wg-quick save wg0
#生成客户端配置文件1
echo "[Interface]
PrivateKey = $(cat cprivatekey1)
Address = 10.0.0.3/24
DNS = 8.8.8.8
MTU = 1420
[Peer]
PublicKey = $(cat spublickey)
Endpoint = X.X.X.X:51820
AllowedIPs = 0.0.0.0/0, ::0/0
PersistentKeepalive = 25"|sed '/^#/d;/^\s*$/d' > client1.conf
[Peer]
PublicKey = ZUbV6+kOWrPkc9n855lEF3nOj37FMvsP5UoERe9LXzk=
AllowedIPs = 10.0.0.2/32
[Peer]
PublicKey = ++dCmLrkW8yYmlCb4TVIewIDzCwAzbYuvC7+FN5jDmI=
AllowedIPs = 10.0.0.3/32
wg-quick down wg0
wg-quick up wg0
systemctl enable wg-quick@wg0
systemctl disable wg-quick@wg0
实测完美运行