Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I was wondering about how one could change a remote host primary IP address without getting disconnected at all (without being in a "no IP addr" state).

The matter is poorly discussed on Internet (according to my research). The best resource I found is a little bit tricky.

EXAMPLE : change 10.0.0.11/24 to 10.0.0.15/24

1. ssh root@10.0.0.11
2. ip addr add 10.0.0.15/24 dev eth0 
3. logout

4. ssh root@10.0.0.15
5. ip addr del 10.0.0.11/24 dev eth0 

Problem: The last command removes both IP addresses and the connection is lost because 10.0.0.11 is primary, and it removes its secondary addresses (to which 10.0.0.15 belongs) when deleted.

I know I could "cheat" by adding 10.0.0.11/25 (instead of 24). However, I think it is theorically possible to do this properly.

What do you think?

share|improve this question
it is no cheat if you use /32 mask... – eicto Mar 13 at 1:57
but it would not work because a /32 cannot communicate with anything but itself... – Totor Mar 13 at 10:35
ok, but /25 will also not work than. – eicto Mar 13 at 10:41

2 Answers

up vote 5 down vote accepted

You need to set the promote_secondaries option on the interface, or on all interfaces:

echo 1 > /proc/sys/net/ipv4/conf/eth0/promote_secondaries

or

sysctl net.ipv4.conf.eth0.promote_secondaries=1

Change eth0 to all to have it work on all interfaces.

This option has been in since 2.6.16 or so: http://www.mail-archive.com/netdev@vger.kernel.org/msg58796.html

I tested this with a dummy interface and it worked there.

share|improve this answer
Exactly what I needed, thanks! See the original commit. – Totor Jan 13 at 4:26

If you want set primary address, you have to set this first.

As if you want do that remotely you may be in a strange situation...

As commands like ip may hang if network connexion is loose and/or break if STDOUT terminate (close). Simply bundling ip a del && ip a add on same commad line would not be enough. You have to ensure sanity of command's environment.

You could try something like this:

ssh root@10.0.0.11
echo 'sleep 1;ip a d 10.0.0.11/24 dev eth0;ip a a 10.0.0.15/24 dev eth0' |
    sh >/tmp/mycmd.log 2>/tmp/mycmd.err & exit

ssh root@10.0.0.15
ip a s dev eth0

Maybe the little function nohup could help you, but I'm not conviced about the need of them:

echo 'sleep 1;ip a d 10.0.0.11/24 dev eth0;ip a a 10.0.0.15/24 dev eth0' |
    nohup sh >/tmp/mycmd.log 2>/tmp/mycmd.err & exit

In this way, you will properly disconnect from old ip, for re-connecting via new ip.

CRON Alternative

If at and cron is installed on remote (target) host, you could;

echo 'ip a d 10.0.0.11/24 dev eth0;ip a a 10.0.0.15/24 dev eth0' |
    ssh root@10.0.0.11 batch
share|improve this answer
This is not "without getting disconnected at all"... I know I could also use screen to avoid stdout closing problems you describe, but it is not what I am looking for. – Totor Jan 12 at 22:24
In your sample, You disconnect from first IP too !? – F. Hauri Jan 12 at 22:25
Yes! In this way, you will not get disconnected as you disconnect youself! – F. Hauri Jan 12 at 22:30
Well, if I had found a way to make 10.0.0.15 the "primary" address, my command #5 would not disconnect my second SSH session. By not getting disconnected at all, I mean having both IP addresses on the remote host for some time, then delete one of them without having the remote host in a "no IP" state, even for a single millisecond. – Totor Jan 12 at 22:40
Also please explain: You could publish yourself's answer! – F. Hauri Jan 12 at 22:47
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.