1

I have an instrument which has a network interface via a raspberry pi running rasbian. I am trying to set up the interfaces file so it will automatically work in both our subnets with a static IP. Basically when in subnet 0 (or default) it should have the following:

auto eth0
iface eth0 inet static
    address 192.168.0.77
    netmask 255.255.255.0
    gateway 192.168.0.254

And when in the other subnet (1) it should have:

auto eth0
iface eth0 inet static
    address 192.168.1.77
    netmask 255.255.255.0
    gateway 192.168.1.254

My last attempt was the following:

# etc/networking/interfaces
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
    address 192.168.0.77
    netmask 255.255.255.0
    gateway 192.168.0.254
    up ip route add default via 192.168.1.254 table cbs
    up ip rule add from 192.168.1.77 table cbs
    up ip route add default via 192.168.0.254

auto eth0:0
iface eth0:0 inet static
    address 192.168.1.77
    netmask 255.255.255.0

The reason for not using DHCP is so that the instrument can be directly connected to a laptop (no router) and will still have a known IP. The problem with the current setup is when on subnet 0, any packets directed to subnet 1 will be from 192.168.1.77 and will then be directed to 192.168.1.254, which is only available via 192.168.0.254. What is the best way to get around this? Perhaps a script on startup which detects which network it is in and sets the default gateway and IP for that session? And how would I implement this?

Or is there a method of requesting a specific address via DHCP?

3 Answers 3

3

In the end I modified the answer from Nath (thanks!). My interfaces file now looks like this:

# etc/networking/interfaces
auto lo
iface lo inet loopback

iface eth0 inet manual

And my dhcpcd.conf has this appended:

# etc/dhcpcd.conf
interface eth0
arping 192.168.1.254
arping 192.168.0.254
fallback nicutpi2

profile 192.168.1.254
static ip_address=192.168.1.77
static routers=192.168.1.254
static domain_name_servers=192.168.1.254

profile 192.168.0.254
static ip_address=192.168.0.77
static routers=192.168.0.254
static domain_name_servers=192.168.0.254

profile nicutpi2
static ip_address=192.168.0.77

The arpings are run through looking for a matching server (note this can be any ip which is consistently on, not just the DHCP server) on the local network, if one is found it uses the corresponding profile. If no profile is found it runs on standard DHCP. If there is no DHCP server on the network (technically DHCP fails) it applies the fallback profile.

IMPORTANT POINTS:

  • This requires the dhcpcd5 (not dhcpcd) package, which comes standard with raspbian Jessie but not Wheezy (I had to install on Wheezy)

  • The iputils-arping conflicts with the arping used in the dhcpcd.conf, if you have this you will need to uninstall and do a clean install of dhcpcd5.

1

The DHCP Server can be configured to provide the same IP address to the same Ethernet MAC. That should allow you to get the same IP everytime.

1

I think your best bet is to do a static DHCP assignment when connected to the network and then use the dhcpcd.conf fallback profile option to have the dhcp client generate a static IP only when it can't get one from dhcp, this a pretty common config for a raspberry pi see https://raspberrypi.stackexchange.com/questions/37920/how-do-i-set-up-networking-wifi-static-ip for more detail on using the fallback profile

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .