Nothing of what I am about to tell you is persistent across reboots, which I presume is what you wish because, apparently, you have an unstable situation. If not, let me know.
I assume you do not have a static IP (again, if so, pls let me know). Does the DHCP server of Provider 1 stay on when the line is down? If not, then
sudo ifdown eth0
sudo ifup eth0
will give you the new configuration. But, if instead the DHCP server of Provider 1 does stay on, then you will have to do it manually.
Change route:
sudo ip route del default
sudo ip route add default via 192.168.1.121 dev eth0
where I assumed you are connected via ethernet, and that the IP address of the second modem is 192.168.1.121, if not change accordingly.
Change DNS, simplest is: edit (as sudo) /etc/resolv.conf
, delete the lines with the previous DNSes, then add the two following lines:
nameserver 1.2.3.4
nameserver 9.8.7.6
(substitute with your values).
If you find this annoying, you can automate it as follows: create an executable (!) file, call it line2
, with the following content:
#!/bin/bash
sudo ip route del default
sudo ip route add default via 192.168.1.121 dev eth0
sed -i --follow-symlinks 's/First.Old.DNS.IP./First.New.DNS.IP/' /etc/resolv.conf
sed -i --follow-symlinks 's/Second.Old.DNS.IP./Second.New.DNS.IP/' /etc/resolv.conf
and now executing line2
will give you the new connection.
There are other, more elegant ways to change DNSes (using resolvconf
, writing to /etc/resolvconf.d
) but since you seem to have a frequent problem nothing is as easy as this.
Neither of the commands you displayed changes the default route, nor the DNS. In order to do that, you will have to specify both such things yourself. What is it that you are trying to do, exactly? – MariusMatutiae – 2015-12-17T12:58:54.180
@MariusMatutiae Thanks.. I have two DSL modems from two different ISPs and each one provide a set of DNS server addresses. One is the main ISP and the other is a fallback ISP with a lower bandwidth. WHen the main ISP is down, I am trying to change the default route to the IP address of the DSL modem of the backup ISP and the DNS servers which that backup ISP provides. Changing the relevant entries in the two files work after reboot. I want to avoid that. I can use ip route command also to dynamically change the default route.Is that persistent across boots? How about changing DNS settings? – Sunny – 2015-12-17T13:08:18.893
@MariusMatutiae If my approach is wrong, please let me know the correct way to handle such situation. – Sunny – 2015-12-17T13:30:17.953
Pls read my answer. Took me a little time to write it up. – MariusMatutiae – 2015-12-17T13:36:25.303