1

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.

2 Answers2

1

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.

Oli
  • 293,335
0

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

/etc/network/interfaces

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
address 192.168.1.2
gateway 192.168.1.1
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255

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
address 192.168.2.2
netmask 255.255.255.0
gateway 192.168.2.1

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

sudo /etc/init.d/networking restart