Macos – How to run a DHCP server on an ethernet port of a MacBook successfully

dhcpmacosraspberry piunix

I aim to have an ethernet switch on my MacBook ethernet port from which I will have several Raspberry Pi's connected getting their IP via DHCP, each will have a VNC server running on them for remote access and I want my internet to be shared to them from my MacBook's WiFi connection.

In order to do this I have made use of OSX's inbuilt DHCP server as follows:

  • Edited bootpd.plist in /etc/, this configures the DHCP server for the
    192.168.2.0 network

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
    <key>Subnets</key>
    <array>
        <dict>
        <key>_creator</key>
        <string>com.apple.InternetSharing</string>
        <key>allocate</key>
        <true/>
        <key>dhcp_domain_name_server</key>
            <array>
                <string>192.168.2.1</string>
            </array>
        <key>dhcp_router</key>
            <string>192.168.2.1</string>
        <key>lease_max</key>
            <integer>36000</integer>
        <key>lease_min</key>
            <integer>36000</integer>
        <key>name</key>
            <string>192.168.2/24</string>
        <key>net_address</key>
            <string>192.168.2.0</string>
        <key>net_mask</key>
            <string>255.255.255.0</string>
        <key>net_range</key>
            <array>
                <string>192.168.2.2</string>
                <string>192.168.2.254</string>
            </array>
        </dict>
    </array>
    <key>bootp_enabled</key>
        <false/>
    <key>detect_other_dhcp_server</key>
        <true/>
    <key>dhcp_enabled</key>
        <array>
            <string>bridge0</string>
        </array>
    <key>use_server_config_for_dhcp_options</key>
        <false/>
    </dict>
    </plist>
    
  • Edited/created bootptab in /etc/, this assigns static IP's to the
    Raspberry Pi's

%%
# hostname      hwtype  hwaddr              ipaddr          bootfile
Raspi1          1       aa:bb:cc:dd:ee:f1   192.168.2.2
Raspi2          1       aa:bb:cc:dd:ee:f2   192.168.2.3
Raspi3          1       aa:bb:cc:dd:ee:f3   192.168.2.4
Raspi4          1       aa:bb:cc:dd:ee:f4   192.168.2.5
  • Run/Stop the DHCP server using the following commands, created an
    alias in my ~/.bash_profile:
alias dhcp-start='sudo /bin/launchctl load -w /System/Library/LaunchDaemons/bootps.plist'
alias dhcp-stop='sudo /bin/launchctl unload -w /System/Library/LaunchDaemons/bootps.plist'

Great I'm able to connect to each Pi remotely, but I have the following problems:

  1. Unable to connect to internet from each Pi, given I have shared the internet connection from WiFi to the ethernet port within OSX's sharing menu:

    Internet Sharing in OSX

  2. Upon reset of the MacBook the bootpd.plist file is being deleted

Any help in solving the above two issues appreciated!

Best Answer

As I understand it, your Internet sharing might be interfering with your bootd configuration, specially given that you're assigning static addresses to the Raspberry πs.

Once you have the dhcp server running, you can create routes for them. If your wifi is on en1, and we assume your gateway for en1 is 172.16.0.1, you could do something like

sudo route add 192.168.2.0/24 172.16.0.1

which will cause packets coming from 192.168.2.0 to 192.168.2.255 to go through your wifi's gateway.

See, for instance, Add a permanent static route to Mac OS X on how to automate it.

Related Question