Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm relatively inexperienced with networking, but I know what I want to achieve in this case. I have a computer that I physically move between two separate local networks, but when the computer is on one of those networks I want to assign a different static IP address to the machine.

One internal network is 192.168.1.0, and the other is 192.168.2.0. Therefore, I want one static IP address to be 192.168.1.x and the other to be 192.168.2.x.

Is there a way of assigning two static IPs, but with only one taking effect depending on the network its connected to? Can this be done purely in /etc/network/interfaces, for example?

I may well have misunderstood the situation, so if there's a different way of achieving what I want then I'm happy to hear the solution.

share|improve this question
    
Did you try network manager? –  Mitch Aug 16 '12 at 10:28
    
I should probably mention that it's a server, so it's headless - no GUIs for me :) –  joonty Aug 16 '12 at 10:30
    
You can use: Network Manager, or if you want to do it by /etc/network/interfaces than check out about virtual hosts –  arhimed Aug 16 '12 at 10:30
    
Take a look at Network Configuration –  Mitch Aug 16 '12 at 10:32
    
Sorry, I'm not prof but what I know that modern card could have more than one ip address and I'm sure that if you need to set several ip-addresses than you can do it by bash script(ifconfig eth0:0 192.168.1.2,ifconfig eth0:1 192.168.2.2) and I think it can be done by /etc/network/interfaces –  arhimed Aug 16 '12 at 10:39
show 1 more comment

1 Answer 1

up vote 1 down vote accepted

There may be better solutions (at least ones closer to what you're asking for) but here's what springs to my mind:

  • Configure the DHCP servers at each location to assign your MAC address a static IP, and on the laptop, just have it automatically grab the IP from DHCP. This is by far the easiest if you have router access at each place.

  • The next three are essentially the same solution as each other - split the connections up:

    • Add and use a different network card for one location.
    • Leave USB network cards on the two ethernet cables (slower than native gigabit).
    • Use two wireless connections. Network Manager can do the hard stuff automatically so you just roam from one place to another.
  • Script something for when the interface comes up. I don't know what the event would really be called - but you essentially want to know when the cable is plugged in and the whole thing is ready to go. You could just have a static setup for one network and wait for when that comes up.

    • Bind in using upstart if you know the connection is there on boot, or consider this Call script after connecting to a wireless network if you make the connection after boot.

    • When the network is up, do some service discovery. Is the gateway where you expect it? Are there other known servers you should be able to ping?

    • If everything is correct, leave the configuration as it is, otherwise switch to the alternative network configuration. You can either manually set this up using ifconfig or you can have two connections set up in Network Manager and just use nmcli to switch which one you're on.

Out of the three, DHCP is the easiest.

share|improve this answer
    
I agree with your conclusion - do it at the router level. Thanks for your input! –  joonty Aug 16 '12 at 15:59
add 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.