Wireguard+OSPF多地内网互联

拓扑

实现多地内网IPv4/6互联,通过OSPF维护动态路由表可形成备用路径,并在VPS-B和Openwrt-A开启2个服务端口用于外部终端的接入。

准备工作

Linux 开启ipv4/6转发

#vi /etc/sysctl.conf
net.ipv4.ip_forward = 1      #启用ipv4转发
net.ipv6.conf.all.forwarding = 1 #启用ipv6转发
#sysctl -p  #立即生效

安装wireguard #搭建VPN隧道

Linux安装Wireguard  https://www.wireguard.com/install/
Openwrt安装wireguard https://openwrt.org/docs/guide-user/services/vpn/wireguard/

安装bird2 #运行OSPF 动态路由协议

Centos

#yum install bird2

Openwrt

#opkg update
#opkg install bird2 bird2c bird2cl

配置防火墙与接口和服务自启

1.openwrt 建立wg区域,wg接口加入wg区域,允许wg到lan区域双向的in/out/forward。
2.Linux wg接口上配置PostUP/DOWN实现iptables 流量转发,无需单独修改防火墙
3.wg接口自启,openwrt默认自启,Linux #systemctl enable wg-quick@wg0
4.bird服务自启,openwrt默认自启,Linux #systemctl enable bird

注意项

1.根据实际情况调整OSPF接口cost值。
2.隧道内允许所有设备互联网段和内网段。
3.隧道允内许ipv4/6 ospf组播地址 224.0.0.5/6 ff02::5/6
4.隧道内允许fe80::本地链路地址
5.OSPF配置wg接口类型为ptp

维护管理

Bird命令

1.bird -c /etc/bird.conf #指定运行配置文件
2.进入bird命令行  #birdc
3.查看ospf进程状态 >show ospf
4.查看ospf接口状态 >show ospf int
5.查看ospf邻居状态 >show ospf nei
6.更新bird配置文件 >config

Wigreguard 命令

1.配置文件 /etc/wireguard/wg*.conf
2.wg生成密钥对 #wg genkey | tee /etc/wireguard/A_privatekey | wg pubkey > /etc/wireguard/A_publickey
3.wg生成预共享密钥 #wg genpsk > /etc/wireguard/preshared_key
4.wg接口状态查看 #wg
5.wg接口启动和关闭 #wg-quick up/down wg*
6.新增wg接口和指定配置文件
# ip link add dev wg1 type wireguard
#wg setconf wg1 /etc/wireguard/wg1.conf

配置文件

Openwrt-A

Openwrt-B

VPS-B

VPS-A

结果验证

VPS-A

VPS-B

Openwrt-A

Openwrt-B

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注