In Ubuntu: "service networking restart" and "resolvconf restart" does not take effect until reboot

4

2

I have a situation where I am using two ISPs, I have a program that periodically pings an address and if it is down assumes that the ISP has a problem and changes the default route and DNS server entries in /etc/network/interfaces and /etc/resolvconf/resolv.conf.d/tail files, respectively. It changes these settings to be pointed to the backup ISP. I then run

sudo service networking restart 
sudo /etc/init.d/resolvconf restart. 

However, this does not change the default route (as displayed by ip route ls) or the DNS servers in /etc/resolv.conf. It requires a reboot. How can I get these settings activated without a reboot?

Sunny

Posted 2015-12-17T12:43:32.587

Reputation: 331

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

Answers

5

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.

  1. 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.

  2. 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.

MariusMatutiae

Posted 2015-12-17T12:43:32.587

Reputation: 41 321

Thanks. Upvoted. I am NOT using DHCP. I understand the ip route commands and they do change the default route without the need to reboot. My problem can now be narrowed to changing nameservers without reboot. Actually I do not even mind if the changes do not persist across a reboot. Merely changing the entries in the DNS config files do not automatically change the DNS servers. How do I get the DNS entries in the config file to take effect without a reboot? That is now my narrow problem. Ubuntu does not recommend changing /etc/resolv.conf directly. – Sunny – 2015-12-17T13:54:45.247

2@Samir You change DNSes like I described above, and the changes take place immediately, without doing anything else. The reason why Linux (not just Ubuntu) does not recommend changing DNSes directly in the file is that your Network Manager will overwrite it. But since you are not calling network manager here, nothing wrong will happen to you. – MariusMatutiae – 2015-12-17T14:00:52.620

1@Samir you can check the set of DNS you are using with nmcli dev list iface eth0. – MariusMatutiae – 2015-12-17T14:13:50.077

You are right about editing /etc/resolv.conf directly in the absence of network manager. Tried it. Works well. I have accepted your answer. Solves my problem. By the way, I am curious what /etc/init.d/resolvconf restart does? What is this resolvconf service listed by service --status-all? – Sunny – 2015-12-17T14:14:13.717

The nmcli command is part of Network Manager and I am not a fan of Network Manager and have removed it. – Sunny – 2015-12-17T14:34:14.663

1@Samir resolvconf is a package which ahndles automatic setup of DNS servers, mostly from network manager, but also from openvpn, ppp, and so on. IMHO, it is an inconvenience in fast-changing situations like yours – MariusMatutiae – 2015-12-17T15:13:36.253