机器上有两个网卡,有个有线网卡eth0, 一个无线网卡wlan0。
两个网卡都开启了DHCP获取IP地址。
有时候eth0当了默认出口,有时候wlan0当了默认出口。
我不想换成静态IP地址, 因为机器随时可能带走, 不同的环境, 配置静态IP很不方便。
我想一直使用 wlan0 作为网关出口, 不要搞一会儿是eth0当出口, 一会儿wlan0当出口。
是我在玩电脑, 不是电脑玩我。
设置 dhcpclient hook
1. 解决 nameserver 不可用
有的时候:
wlan0 先启动: 此时 /etc/resolv.conf 已被 wlan0 重置
eth0 后启动: 此时 /etc/resolv.conf 已被 eth0 重置
结果: /etc/resolv.conf 保留是 eth0 从 dhcp server 获取的信息
有的时候:
eth0 先启动: 此时 /etc/resolv.conf 已被 eth0 重置
wlan0 后启动: 此时 /etc/resolv.conf 已被 wlan0 重置
结果: /etc/resolv.conf 保留是 wlan0 从 dhcp server 获取的信息
在更改/etc/resolv.conf之前, 重新定义make_resolv_conf
注意: 在dhcp操作完成之后
1 2 3 4 5 6
| root@localhost:~ make_resolv_conf(){ : } root@localhost:~ root@localhost:~
|
2. 只允许 wlan0 接口作为默认网关, 并重置系统 nameserver
注意: 在dhcp操作完成之后
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| root@localhost:~# cat /etc/dhcp/dhclient-exit-hooks.d/wlan0 if [ "$interface" != "$(basename $1)" ] then exit 0 fi
if [ "$reason" = "RENEW" ] || [ "$reason" = "BOUND" ] || [ "$reason" = "REBOOT" ] then route del -net 0.0.0.0/0 route add -net 0.0.0.0/0 gw $new_routers echo "nameserver $new_domain_name_servers" > /etc/resolv.conf fi root@localhost:~#
root@localhost:~# chmod +x /etc/dhcp/dhclient-exit-hooks.d/wlan0
|
测试
1 2 3 4 5 6 7 8
| Debian/Ubuntu root@localhost:~# systemctl restart networking
RadHat/CentOS root@localhost:~# systemctl restart network
Reboot root@localhost:~# reboot
|
验收
1 2 3 4 5 6 7 8 9 10 11 12 13
| root@localhost:~# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 wlan0 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan0 192.168.88.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 192.168.173.0 0.0.0.0 255.255.255.0 U 0 0 0 VPN root@localhost:~# root@localhost:~# root@localhost:~# cat /etc/resolv.conf nameserver 192.168.1.1 root@localhost:~#
|