0

I am working with a arm micro-controller. Currently the controller is configured as a dhcp client on eth0 (ethernet jack) and as a dhcp server on eth1. I want to configure it to also serve dhcp on eth0, so that it will be a client when it is connected to a router, or be a server when connected to a laptop/hub. I want to be able to tell the controller to be a server or be a client from the front end. I am not entirely sure how to go about doing that as I am fairly new to linux dev.

I have looked into the /etc/udhcpd.conf file but i'm not sure if I should add interface eth0 there. Also looked into the /etc/networking/interfaces file and the udhcpd.service files but I'm not sure what to modify. Could you please give me some direction as to how I can configure the controller to be a server or a client on demand on eth0?

Here are the contents of the files contents for more info:

/etc/udhcpd.config:

start       10.77.0.2
end         10.77.255.200
interface   eth1
opt dns 8.8.8.8 8.8.4.4 #public google dns servers
option  subnet  255.255.0.0
opt     router  10.77.0.1

/etc/network/interfaces:

auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp

pre-up /bin/grep -v -e "ip=[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+" /proc/cmdline > 
/dev/null
udhcpc_opts -R -b

iface eth1 inet static
address 10.77.0.1
netmask 255.255.0.0

udhcpd.service:

[Unit]
Description=udhcpd Service
After=startup.service

[Service]
ExecStartPre=/sbin/ifup eth1 
ExecStart=/usr/sbin/udhcpd -f -S /etc/udhcpd.conf
TimeoutSec=infinity

[Install]
WantedBy=multi-user.target
0

As far as I can see, a single udhcpd will only serve one network interface at a time, so you'll need to run a second copy of it with a separate configuration file whenever you want it to serve DHCP on eth0 too. That might be called /etc/udhcpd-eth0.conf.

You might also need two copies of /etc/network/interfaces at a separate location, one with the "DHCP client" configuration for eth0 and another for the "DHCP server" configuration. Let's call them /etc/network/interfaces.eth0client and /etc/network/interfaces.eth0server for example.

The next question is, do you want to regard "DHCP server on eth0" as a persistent state that will survive a reboot until explicitly switched back to "DHCP client on eth0", or do you want to treat "DHCP server on eth0" as a transient state that will be enabled on-demand and will last only until the next reboot?

You'll also want to define a separate udhcpd-eth0.service, which should be disabled (= never start automatically) if you're going for transient mode. If you want persistent mode, it should be enabled and started when switching to server mode, and stopped and disabled when switching to client mode.

The script to switch modes persistently would need to do the following (as pseudocode):

  1. systemctl stop udhcpd-eth0; systemctl disable udhcpd-eth0. Ignore errors here.

  2. Run ifdown eth0.

  3. Overwrite the current /etc/network/interfaces with either /etc/network/interfaces.eth0client or /etc/network/interfaces.eth0server depending on which state you're switching to.

  4. Run ifup eth0.

  5. If switching to DHCP server mode on eth0, then systemctl enable udhcpd-eth0.service && systemctl start udhcpd-eth0.service

  6. Done!

If you want it as a transient state, you might not need two copies of the /etc/network/interfaces file at all. In that case, your script would look like this:

  1. systemctl stop udhcpd-eth0.service. It may or may not be already stopped; ignore errors here.

  2. Run ifdown eth0 and ip link set eth0 down (or ifconfig eth0 down if using the older tools). One or the other of those commands will report an error; that is expected.

  3. If switching to DHCP client mode, run ifup eth0 and you're done. Else, first use ip or ifconfig commands to configure a static IP address for DHCP server-mode to eth0, then run systemctl start udhcpd-eth0.

  4. Done!

"Invoking the switching script from the front end is left as an exercise to the reader."

  • Thank you very much! It worked for me, I followed your guide. – Hermon Gebremariam Jun 18 '19 at 1:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.