Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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, and the other is 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 :) – Jon Cairns 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 – Victor K 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,ifconfig eth0:1 and I think it can be done by /etc/network/interfaces – Victor K Aug 16 '12 at 10:39
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! – Jon Cairns Aug 16 '12 at 15:59

You should create a Virtual interface and assign an IP address to it.
If you want to configure more Static IP address you need to edit the


and you need to enter the following lines replace eth0 with your network interface card.

The primary network interface

auto eth0 
iface eth0 inet static

The virtual network interface

To assign a second IP address to your machine you need to edit again the /etc/network/interfaces file. The second IP is named eth0:0, the third one eth0:2 and so on... So just add them in the file:

sudo vi /etc/network/interfaces

auto eth0:0
iface eth0:0 inet static

After entering all the details you need to restart networking services using the following command

sudo /etc/init.d/networking restart

share|improve this answer

Your Answer


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.