Настройте две возможные конфигурации сети, но используйте только одну

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?

1
задан 7 December 2016 в 14:47
3 ответа

В конце концов, я изменил ответ Натха (спасибо!). Мой файл интерфейсов теперь выглядит так:

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

iface eth0 inet manual

И мой dhcpcd.conf добавил это:

# 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

Арпинги выполняются в поисках подходящего сервера (обратите внимание, что это может быть любой постоянно включенный ip, а не только DHCP-сервер ) в локальной сети, если он найден, он использует соответствующий профиль. Если профиль не найден, он работает по стандартному DHCP. Если в сети нет DHCP-сервера (технически сбой DHCP), он применяет резервный профиль.

ВАЖНЫЕ ЗАМЕЧАНИЯ:

  • Для этого требуется пакет dhcpcd5 (не dhcpcd), который входит в стандартную комплектацию raspbian Jessie, но не Wheezy (I пришлось установить на Wheezy)

  • iputils-arping конфликтует с arping, используемым в dhcpcd.conf, если он у вас есть, вам нужно будет удалить и выполнить чистую установку dhcpcd5.

3
ответ дан 3 December 2019 в 17:03

DHCP-сервер можно настроить для предоставления одного и того же IP-адреса одному и тому же MAC-адресу Ethernet. Это должно позволить вам каждый раз получать один и тот же IP-адрес.

1
ответ дан 3 December 2019 в 17:03

Я думаю, что лучше всего выполнить статическое назначение DHCP при подключении к сети, а затем использовать dhcpcd.conf резервный профиль возможность, чтобы клиент dhcp генерировал статический IP-адрес только в том случае, если он не может получить его от DHCP, это довольно распространенная конфигурация для Raspberry Pi, см. https://raspberrypi.stackexchange.com/questions/37920/how- do-i-set-up-network-wifi-static-ip для получения более подробной информации об использовании резервного профиля

1
ответ дан 3 December 2019 в 17:03

Теги

Похожие вопросы