树莓派设置双网卡一个外网一个内网
背景
树莓派3B自带一个有线口,一个无线网卡,需要使用有线口上外网,无线连接另一个局域网
试错过程
设置rc.local无效
通过等待一分钟删除和添加相关路由的方式无法实现 开机后依然是无线作为出口网卡
sleep 60 && route del -net 0.0.0.0/0 gw 172.18.1.254 2>/dev/null && route del -net 0.0.0.0/0 gw 172.18.2.254 2>/dev/null && route del -net 0.0.0.0/0 gw 172.18.3.254 2>/dev/null && route add -net 0.0.0.0/0 gw 172.18.1.254 metric 0 dev eth0 2>/dev/null && route del -net 0.0.0.0/0 gw 172.17.1.254 2>/dev/null && route del -net 0.0.0.0/0 gw 172.17.2.254
对应帖子是 https://www.jianshu.com/p/5d07d46cd434
正确方式
在/etc/network/interfaces设置有线网卡静态IP和网关,无线网卡自动获取IP地址即可,重启多次树莓派都满足需求。
pi@oldpi:~ $ sudo cat /etc/network/interfaces # interfaces(5) file used by ifup(8) and ifdown(8) # Please note that this file is written to be used with dhcpcd # For static IP, consult /etc/dhcpcd.conf and man dhcpcd.conf # Include files from /etc/network/interfaces.d: source-directory /etc/network/interfaces.d auto eth0 allow-hotplug eth0 #iface eth0 inet dhcp iface eth0 inet static address 172.18.1.100 netmask 255.255.255.0 gateway 172.18.1.254 auto eth1 allow-hotplug eth1 iface eth1 inet dhcp auto eth2 allow-hotplug eth2 iface eth2 inet dhcp auto wlan0 allow-hotplug wlan0 iface wlan0 inet dhcp wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
有多个有线网卡的我还没有测试过,不过理论上可以的。 路由设置也正常,如下,
pi@oldpi:~ $ route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.18.1.254 0.0.0.0 UG 0 0 0 eth0 172.18.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 172.17.1.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan0 pi@oldpi:~ $ pi@oldpi:~ $ pi@oldpi:~ $ date Tue, 02 Nov 2021 09:13:48 +0800