I'm writing a script to control my network interfaces. I've three network interfaces viz eth0
, ppp0
, wlan0
. I want such a configuration that when there is no internet available on eth0
, it should switch to ppp0
and when ppp0
is also down, it should switch to wlan0
.
Whenever I need to switch from eth0
to ppp0
, I write following command:
sudo route add default gw "IP of ppp0"
And it gets connected. But the problem is there while switching between eth0
to wlan0
and vice-versa.
Sometimes while using sudo route add default gw 192.168.1.254 eth0
, I get following response:
SIOCADDRT: File exists
And interface do not switch.
I check for the route
command and I got following result:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.1.254 0.0.0.0 UG 202 0 0 eth0
default 192.168.43.141 0.0.0.0 UG 303 0 0 wlan0
10.64.64.64 0.0.0.0 255.255.255.255 UH 304 0 0 ppp0
192.168.0.0 0.0.0.0 255.255.252.0 U 202 0 0 eth0
192.168.43.0 0.0.0.0 255.255.255.0 U 303 0 0 wlan0
Then I've to remove all interfaces by using following command multiple times:
sudo route del default
Then after checking route
again, I successfully deleted existing gateways and get this:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.64.64.64 0.0.0.0 255.255.255.255 UH 304 0 0 ppp0
192.168.0.0 0.0.0.0 255.255.252.0 U 202 0 0 eth0
192.168.43.0 0.0.0.0 255.255.255.0 U 303 0 0 wlan0
But the problem I faced is that, after 4-5 minutes, these gateways appears again without any changes made by me. Help me to make me understand the working of linux here. How gateway IPs are configured automatically and how can I prevent it to do so.
/etc/resolv.conf
contains# Generated by resolvconf
- this is normal for virtually all dhcp clients. You can change it but it will be overwritten! What you (probably) want can be done withdhcpcd
- see How to set up networking/WiFi NOTE having 2 default routes is a recipe for disaster! – Milliways Dec 17 '19 at 05:32