使用wireguard组网

首先说一下我的使用的设备,一台阿里云的轻量服务器(debian内核、有公网ip),一台MacBook,一个位于校园网内的debian小主机

校园网

首先配置校园网内的debian小主机

  • 下载wireguard
1
apt install wireguard
  • 在/etc/wireguard生成公钥私钥
1
2
3
4
5
#生成私钥
$ wg genkey > privatekey

# 生成公钥
$ wg pubkey < privatekey > publickey
  • 编辑配置文件

配置文件可以放在任何路径下,但必须通过绝对路径引用。默认路径是 /etc/wireguard/wg0.conf

配置文件的命名形式必须为 就行。

配置文件内容:

1
2
3
4
5
6
7
8
9
10
[Interface]
PrivateKey = 本机私钥
Address = 10.6.0.6/24
DNS = 1.1.1.1

[Peer]
PublicKey = 阿里云服务器公钥
AllowedIPs = 10.6.0.2/32
Endpoint = 1panel.daoxuan.cc:51820 # 阿里云服务器域名+端口
PersistentKeepalive = 25

阿里云服务器

下载流程和密钥生成流程同上

开启IP地址转发:

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

下面是配置文件内容:

1
2
3
4
5
6
7
8
9
[Interface]
Address = 10.6.0.2/24
ListenPort = 51820
PrivateKey =
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
[Peer]
PublicKey =
AllowedIPs = 10.6.0.6/32

这里的eth0需要根据自己的默认网络出口网卡进行配置

启动

配置结束之后通过:

1
wg-quick up wg0

启动!!

启动之后可以通过wg show查看连接状态

可以通过ping 内网地址查看是否互通

至此实现校园网内网主机与阿里云轻量服务器组网