I often have the need to connect to devices directly through the ethernet port on my MacBook.
-
When I am connected to a wireless network, I can share my connection to ethernet. However, the second, I loose the wireless connection, I loose connectivity to the devices.
-
I can set up a static IP on both the MacBook and the Device, but this is not very easy because I still have to make an initial connection to setup the static IP.
Is there any way to setup a DHCP server on the ethernet port that works even if I am not connected to a network?
Best Answer
Based on my answer here: Using Server 5.0.15 to share internet WITHOUT internet sharing I provide a possibility to share internet with PF and dnsmasq (i.e. without Apple's OS X Server):
To get NAT working without using Internet Sharing you have to use a pf rule and create a plist to enable forwarding and load the pf rule. Additionally you have to set up a DNS/DHCP server: dnsmasq.
Below I assume en0: the interface connected to the internet or a router and en1: the interface connected to the LAN. The router has the IP 192.168.0.1 and the netmask 255.255.255.0.
Use
ifconfig
to get the device names.Prepare the Mac gateway:
Set up the two interfaces en0 and en1 with fixed IPs and netmasks
Example:
en0: IP: 192.168.0.2 Netmask: 255.255.255.0 Gateway: 192.168.0.1 DNS: 8.8.8.8 and 127.0.0.1 Search Domains: home.org
en1: IP: 192.168.1.1 Netmask: 255.255.255.0
Disable System Integrity Protection if El Capitan is installed
Install dnsmasq:
Set up and configure dnsmasq
open /usr/local/etc/dnsmasq.conf with an editor and modify at least the following lines:
You may configure much more - just check the config file and its descriptions.
Create a file named nat-rules in /private/etc/ with the following content
Create a shell script named nat-pf.sh enabling forwarding and loading the pf rule. I saved it in /usr/local/bin:
Create a plist named org.user.natpf.plist with the following content and save it in /Library/LaunchDaemons/ to execute the above shell script at start-up:
All three files need a trailing empty line so don't simply copy the above code/lines.
Modify ownership and file modes:
Load the launch daemon:
Reboot your gateway Mac. If everything works fine enable SIP again.
The file /tmp/org.user.natpf.stderr contains error messages. You may add a similar key to the file /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist to get potential error messages:
Prepare your Internet Router (if you have one)
Prepare your Raspberry
After setting all things up successfully you should have a reliable LAN with NAT, DHCP and DNS. You may even enter
ping raspberry
with a proper result.If you run into problems leave acomment.