Debian Linux reload changes to interfaces file without a network restart

debianlinux-networking

I am new to Linux server administration and I am try out some network commands.
I wish to change the network settings by modifying the /etc/network/interfaces file and want the changes to take effect immediately as possible. But I do not wish to restart the networking service, as it will cause disaster. We also want to avoid rebooting the system as much possible.
How to do it? I have used the ip commands and the ifup/ifdown commands, but for some strange reason, they sometimes fail. Where is any documentation or books to study this?
Sorry for English.

Best Answer

The problem with ifup/ifdown is that it reads the current config to bring the interface up and down. So if you had, e.g.:

iface eno1 inet static
    address 1.1.1.1
    netmask 24

and you ifup eno1, what really happens is some ip addr and ip route commands executed. Among them you'll have ip addr add 1.1.1.1/24 dev eno1. If now you modify the file:

iface eno1 inet static
    address 2.2.2.2
    netmask 24

and you ifdown eno1, it will not execute ip addr del 1.1.1.1/24 dev eno1 but ip addr del 2.2.2.2/24 dev eno1, hence the errors when you modify the config while an interface is up.

To work around this, backup the old configuration file before modifying it:

cp /etc/network/interfaces /etc/network/interfaces.bak

and after you modified the configuration, use the backup for the ifdown command:

ifdown -i /etc/network/interfaces.bak <your_interface>; ifup <your_interface>

If you are connecting to the machine remotely, a good idea would be to reverse the operation after a timeout:

(sleep 180; ifdown <your_interface>; ifup -i /etc/network/interfaces.bak <your_interface>)&

If you lose connectivity, it should be restored after 180 seconds, otherwise you can just kill the job.