2

When my system boots it should check for a given interface (e.g. eth0) whether a DHCP configuration is possible respectively if DHCP server exists within the network. If yes, DHCP should be used. If not, a pre-defined static IP address should be used.

  • I know how to execute scripts at startup
  • I know how to specify interfaces in /etc/network/interfaces

I tried playing around with dhclient, e.g.

dhclient -1 # Try once to get a lease. One failure, exit with code 2. 

See http://www.computerhope.com/unix/dhclient.htm

However, dhclient returns always '0' even if there is not connection avilable at all.

What would be the correct approach?

1 Answer 1

0

A simple option, if your static setting isn't going to potentially conflict with anything, would be to setup a network alias. For the non-alias connection, you leave it configured for DHCP, but then you configure the alias connection with your static connection. In this way if DHCP is available, technically both connections are active, presumably only the DHCP one really "works" the network you are using. If DHCP isn't available, it self assigns a 169 address and doesn't work, but the static alias does. Don't forget to reboot or restart the service after editing.

/etc/network/interfaces Example

auto eth0
allow-hotplug eth0
iface eth0 inet dhcpauto eth0
allow-hotplug eth0
iface eth0 inet dhcp

auto eth0:1
iface eth0:1 inet static
name Ethernet alias LAN card
address 192.168.1.7
netmask 255.255.255.0
broadcast 192.168.1.255
network 192.168.1.0

Details available here: https://www.cyberciti.biz/faq/linux-creating-or-adding-new-network-alias-to-a-network-card-nic/

You must log in to answer this question.

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