4

When I connect a USB-to-Ethernet adapter (that has been previously used), I receive a MAC address, and then if there's something connected on the other end I receive also an IP address (192.168.1.1 in my case, configured in static in NetworkManager). On the other end there are various instruments that wait for the DHCP server running on that PC to give them an IP.

The problem is that the DHCP server refuses to start if the subnet is not found, so it cannot be launched before the connection is up. How can I do systemctl restart isc-dhcp-server.service after I receive the IP 192.168.1.1 from NetworkManager ?

I thought of using a udev rule with ATTR{address}=="f0:1f:af:3d:6c:e7" RUN+="systemctl restart isc-dhcp-server.service" but it runs before the IP is assigned. Is there a udev option I can use or do I have to use another method via systemd or NetworkManager or other ?

4
  • are you going to server dhcp service on this interface ? – EchoMike444 Jun 20 '20 at 18:59
  • this ip 192.168.1.1 is a static ip ? – EchoMike444 Jun 20 '20 at 18:59
  • which version of ubuntu ? – EchoMike444 Jun 20 '20 at 19:00
  • Latest LTS version, so 18.04, soon to change to 20.04 IIRC. Yes, static IP and I want to serve dhcp. – dargaud Jun 22 '20 at 5:20
0

UDEV triggering a systemd service is the way to do this.

you can match your usb adapter by usb id , its too late when you can see a mac address.

this post has the instructions

https://unix.stackexchange.com/questions/229987/udev-rule-to-match-any-usb-storage-device

1
  • I don't want to match a USB adapter. I want to match receiving an IP address from the system. Is there some kind of SUBSYSTEM="network" for that ? Can't find anything relevant. – dargaud Jun 19 '20 at 15:32
0
+50

Your system is configured with systemd .

So one solution is to add a dependency between isc-dhcp-server and the usb device .

  1. identify the name of device under systemd system
systemctl | grep -i sys-subsystem-net-devices.*6c.*e7

ps 6c & e7 are the last 2 bytes of your mac from you usb device

you must have something like

 sys-subsystem-net-devices-enxf01faf3d6ce7.device
  1. amend the unit file for isc-dhcp-server
export EDITOR=emacs # <= change to what you want 
systemctl edit isc-dhcp-server.service

and add these lines

[Unit]
After=sys-subsystem-net-devices-enxf01faf3d6ce7.device
Wants=sys-subsystem-net-devices-enxf01faf3d6ce7.device
  1. reboot

  2. check status and configuration of isc-dhcp-server by running these commands

systemctl show --property=After isc-dhcp-server.service 

systemctl status isc-dhcp-server.service
5
  • I can't test this before the bounty expires, but it looks like it does what I want. And I learnt some more about the mysterious systemd in the process. – dargaud Jun 23 '20 at 9:20
  • I was able to test it only now, and it uses the presence of the MAC address to restart the DHCP server. That's not it, I want the restart to happen when I obtain an IP address. As such, it does not work, sorry. – dargaud Nov 3 '20 at 12:22
  • adding a dependcy on network.online is not enougth ? – EchoMike444 Nov 3 '20 at 23:53
  • After=network-online.target and Wants=network-online.target – EchoMike444 Nov 3 '20 at 23:53
  • can you see what is you list of depencies ? systemctl cat isc-dhcp-server | grep -e ^After -e ^Want – EchoMike444 Nov 4 '20 at 0:03
0

Finally spent some time on this problem and solved it with a script I named restart-dhcpd.sh and placed in /etc/NetworkManager/dispatcher.d/. (Note: you may need to systemctl restart network-manager.service and/or systemctl daemon-reload afterwards):

#! /bin/bash
IF=$1
STATUS=$2
case "$2" in
    dhcp4-change|up)
            logger -s "NM Script $STATUS $IF triggered"
            if [ "$1" = "enx6cf0496c6947" ]; then
                    /bin/systemctl restart isc-dhcp-server.service
            fi
    ;;
esac

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.