简介
WireGuard 是个 VPN, 多用于企业之间的数据安全传输.
准备
使用 Parallels Desktop 新建两台 CentOS 7.9 的 Linux 虚拟机, 分别命名为 vm01, vm02.
# 关闭防火墙
systemctl stop firewalld.service
# 安装
yum install -y epel-release elrepo-release
yum install -y yum-plugin-elrepo
yum install -y kmod-wireguard wireguard-tools
入门
跟着WireGuard 官网中的视频一步一步操作
# vm01
wg genkey > private
ip link add wg0 type wireguard
ip addr add 10.0.0.1/24 dev wg0
wg set wg0 private-key ./private
ip link set wg0 up
# vm02
wg genkey > private
wg pubkey < private
ip link add wg0 type wireguard
ip addr add 10.0.0.2/24 dev wg0
wg set wg0 private-key ./private
ip link set wg0 up
使用 ip addr 命令查看所有网络接口, 得到 vm01, vm02 的 eth0 网卡分配到的 ipv4 地址分别为 10.211.55.10, 10.211.55.11 并且互相能 ping 通, 还能看到 wg0 网卡信息.
使用 wg 命令查看 WireGuard 信息
# vm01
interface: wg0
public key: OgbUF/E21zi9qHf5h9H6N/iM92nnNYgRtOMhReFm730=
private key: (hidden)
listening port: 35304
# vm02
interface: wg0
public key: 4MIj+Te/pKnszjk9kc72E/YEhMeBdS8U1vPEg2PxSQI=
private key: (hidden)
listening port: 39114
配置 wg
# vm01
wg set wg0 peer <vm02 的 public key> allowed-ips <vm02 的 wg0 地址/32> endpoint <vm02 的 eth0 ipv4 地址:listerning port>
# vm02
wg set wg0 peer <vm01 的 public key> allowed-ips <vm01 的 wg0 地址/32> endpoint <vm01 的 eth0 ipv4 地址:listerning port>
验证两台虚拟机是否能用 wg0 网卡互联
# vm01
ping 10.0.0.2
# vm02
ping 10.0.0.1
再次用 wg 命令发现 peer 的 lasted handshake 有信息即配置 wireguard 完成.
一些有用的命令
查看配置好的网络情况 ip a | grep eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 10.211.55.11/24 brd 10.211.55.255 scope global noprefixroute dynamic eth0
查看各网卡当前路由ip route show
default via 10.211.55.1 dev eth0 proto dhcp metric 100
10.211.55.0/24 dev eth0 proto kernel scope link src 10.211.55.11 metric 100
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1
三台机器互联
需求: 假设有 A B C 三台机器, 要实现 A(client) -> B(server&client) -> C(server)
实现:
-
关闭 CentOS 7 的防火墙
systemctl stop firewalld.service -
三台机器都生成秘钥
cd /etc/wireguard wg genkey > private.key # 生成私钥 wg pubkey < ./private.key > public.key # 通过私钥生成公钥 -
配置 A(client)
新建网卡
ip link add dev wg0-client type wireguard # wg0-client 为网卡名 ip addr add dev wg0-client 10.1.0.1/24 # 设置 10.1.x.x 网段下的 IP ip link set wg0-client up # 启动网卡新建 wg0-client.conf, 内容:
[Interface] ListenPort = 11111 PrivateKey = <A的秘钥> [Peer] PublicKey = <B的公钥> AllowedIPs = 0.0.0.0/0 Endpoint = <B的eth0网卡IP>:<B的wg0-server.conf写的ListenPort,即22222> PersistentKeepalive = 15使用 wg0-client.conf 这个配置
wg setconf wg0-client ./wg0-client.conf -
配置 B(server)
新建网卡
ip link add dev wg0-server type wireguard ip addr add dev wg0-server 10.1.0.2/24 # 设置 10.1.x.x 网段下的 IP ip link set wg0-server up新建 wg0-server.conf, 内容:
[Interface] ListenPort = 22222 PrivateKey = <B的秘钥> [Peer] PublicKey = <A的公钥> AllowedIPs = 0.0.0.0/0 Endpoint = <A的eth0网卡IP>:<A的wg0-client.conf写的ListenPort, 即11111> PersistentKeepalive = 15使用 wg0-server.conf 这个配置
wg setconf wg0-server ./wg0-server.conf -
测试 A -> B 可以连通
# A ping 10.1.0.2 # B tcpdump -i wg0-server能看到 A 正常 ping 通, B tcpdump 有 request / reply, 反之也可以连通
-
配置 B(client)
新建网卡
ip link add dev wg1-client type wireguard ip addr add dev wg1-client 10.2.0.1/24 # !!! 注意这是新的网段 10.2.x.x 下的 IP ip link set wg1-client up # 启动作为客户端的网卡新建 wg1-client.conf, 内容:
[Interface] ListenPort = 23333 PrivateKey = <B的秘钥> [Peer] PublicKey = <C的公钥> AllowedIPs = 0.0.0.0/0 Endpoint = <C的eth0网卡IP>:<C的wg0-server.conf写的ListenPort,即33333> PersistentKeepalive = 15使用 wg1-client.conf 这个配置
wg setconf wg1-client ./wg1-client.conf -
配置 C(server)
新建网卡
ip link add dev wg0-server type wireguard ip addr add dev wg0-server 10.2.0.2/24 # 设置 10.2.x.x 网段下的 IP ip link set wg0-server up新建 wg0-server.conf, 内容:
[Interface] ListenPort = 33333 PrivateKey = <C的秘钥> [Peer] PublicKey = <B的公钥> AllowedIPs = 0.0.0.0/0 Endpoint = <B的eth0网卡IP>:<B的wg1-client.conf写的ListenPort, 即23333> PersistentKeepalive = 15使用 wg0-server.conf 这个配置
wg setconf wg0-server ./wg0-server.conf -
测试 B -> C 可以连通
-
实现 A -> C
设置 A:
ip route add <C所在的网段即10.2.0.0>/24 via <wg0-server网卡的ipv4地址>设置 C:
ip route add <A所在的网段即10.1.0.0>/24 via <wg1-client网卡的ipv4地址>