MacOS – How to setup a MacBook Pro with DHCP Server over USB WiFi dongle

airportmacosNetworkwifi

I have a MacBook Pro running OSX Yosemite 10.10.5 and I'm trying to set it up as a server. I have a Realtek USB WiFi dongle (RTL8188cus) plugged in with the drivers installed, showing up as interface en6. I also have the built-in AirPort on interface en0. I don't care which interface is used, I'm just providing options.

Goal

The goal is to allow users to connect to my MBP via WiFi and get an IP address via DHCP. The reason being is that I am running a website on my MBP that I want to share only with people connecting to my MBP directly (via WiFi).

Bonus

A huge bonus would be having a way that I could assign a hostname to my MBP so that people could visit the website without having to type in the IP address of the gateway (my macbook).

Note

This is not internet sharing! I'm not interesting in forwarding any data or connections.

Best Answer

To get that working you have to be able to create an ad-hoc network with one of the two Wi-Fi interfaces. It works at least with Airport but only without any Wi-Fi security like WEP/WPA/WPA2. I don't have access to a RTL8188cus, so I don't know if its driver allows a "safer" wireless networking.

Additionally you have to install a lightweight DNS/DHCP service like dnsmasq.

  • Open Terminal.app
  • Install homebrew:

    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    
  • doctor homebrew:

    brew doctor
    
  • install dnsmasq:

    brew install dnsmasq
    
  • copy config and plist files:

    cp /usr/local/opt/dnsmasq/dnsmasq.conf.example /usr/local/etc/dnsmasq.conf
    sudo cp -fv /usr/local/opt/dnsmasq/*.plist /Library/LaunchDaemons
    sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
    
  • create some additional config files and folders:

    sudo mkdir /usr/local/etc/hosts
    sudo touch /usr/local/etc/hosts/hosts.conf
    sudo mkdir /var/lib/misc
    sudo touch /var/lib/misc/dnsmasq.leases
    
  • Create an ad-hoc network with your Airport Wi-Fi interface including a name.

  • Open System Preferences -> Network -> Airport Interface and configure an IP-address, a netmask and a DNS-server manually:

    IP-address: 192.168.2.10
    Netmask: 255.255.255.0
    DNS-server: 127.0.0.1
    
  • Modify /usr/local/etc/hosts/hosts.conf with nano:

    sudo nano /usr/local/etc/hosts/hosts.conf
    

    Content (append a trailing empty new line):

    127.0.0.1       localhost
    192.168.2.10    host.foo.bar www.foo.bar
    
  • Modify /usr/local/etc/dnsmasq.conf with nano:

    sudo nano /usr/local/etc/dnsmasq.conf
    

    Content:

    ...
    # Add other name servers here, with domain specs if they are for
    # non-public domains.
    server=/foo.bar/127.0.0.1
    addn-hosts=/usr/local/etc/hosts/hosts.conf
    ...
    # If you want dnsmasq to listen for DHCP and DNS requests only on
    # specified interfaces (and the loopback) give the name of the
    # interface (eg eth0) here.
    # Repeat the line for more than one interface.
    interface=en0 #modify the interface name if necessary
    ...
    # 3) Provides the domain part for "expand-hosts"
    domain=foo.bar
    ...
    # Uncomment this to enable the integrated DHCP server, you need
    # to supply the range of addresses available for lease and optionally
    # a lease time. If you have more than one network, you will need to
    # repeat this for each network on which you want to supply DHCP
    # service.
    dhcp-range=192.168.2.50,192.168.2.100,12h
    ...
    

    If you need further settings/options read the comments in the config file.

  • load the dnsmasq daemon:

    sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
    
  • Configure your web-server properly.
  • Start your web-server.

  • Connect the client hosts to your ad-hoc Wi-Fi network.
  • The clients have to get their IP-addresses on the Wi-Fi interface via DHCP. It usually doesn't work with manually configured IP-addresses (without changing them or only by accident!).
  • Open a browser on the client hosts and enter www.foo.bar.