Web lists-archives.com

Re: ifconfig/ ifupdown/ ip -

On Mon, Jul 09, 2018 at 11:52:36AM +1000, Zenaan Harkness wrote:
So I change between two internet connections from time to time.

I use /etc/network/interfaces ("/e/n/i")

When I modify /e/n/i , I then run a little "reset" script, like so:

ifdown $dev
ifconfig $dev down
ifup $dev

Here and there I've had problems.

Recently I discovered the ip command.

Apparently, after reconfiguring as above, two IP addresses end up
attached to eth0 - one for each (staticly configured in /e/n/i)
ISP network connection.

So now I am manually running something like:

ifconfig eth0 down
ip address del dev eth0
ip address del dev eth0
ip address del fe80::f2de:f1ff:fef7:ea96/64 dev eth0
ifup eth0

But this (atm) is a very manual process, and it seems to me that I am
not taking down eth0 properly, and that I should not have to
introduce IP address awareness into my eth reset script, just to
properly reset my eth0 static configuration.

Any pointers of what I need to read/ what I am missing, would be
really appreciated.

ifup and ifdown read the contents of /e/n/i to determine what tasks to perform. So, in particular, ifdown reads that file to determine what addresses to remove from the interface.

That being said, you might find it more suitable to change your script to:
 1. ifdown the interface
2. Change the network parameters (either by using sed etc to edit the file or to move 'canned' files in/out of /etc/network/interfaces.d)
 3. ifup the interface

What I would do, in your situation is to have a set of files in /etc/network/interfaces.d called, for example "!home", "!work", "!travel" or whatever makes sense for your locations. The leading "!" character makes the configuration invalid to ifupdown, so will be ignored. Your script can then rename any files that DON'T have a "!" in front to do so, then rename the requested file to NOT have a "!" in front e.g.

 ifdown eth0
 prename 's/^/!' /etc/network/interfaces.d/[^\!]*
 mv /etc/network/interfaces.d/\!$1 /etc/network/interfaces.d/$1
 ifup eth0

For more information, please reread.

Attachment: signature.asc
Description: PGP signature