Skip to content

Instantly share code, notes, and snippets.

@thebouv
Created October 8, 2014 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thebouv/5e53f322d5044b7e8ced to your computer and use it in GitHub Desktop.
Save thebouv/5e53f322d5044b7e8ced to your computer and use it in GitHub Desktop.
fix wifi sharing on mac
sudo lsof -i udp:67 | grep bootpd |awk '{ print $2 }'| while read pid; do echo killing $pid; sudo kill -9 $pid&&echo "successfull..."; done
@thebouv
Copy link
Author

thebouv commented Oct 8, 2014

https://discussions.apple.com/thread/5485657?start=15&tstart=0

But I took a look into the console output and found the following errors after turning on connection sharing:

[...]
bind(bootps 67): Address already in use
error creating internal interface for devname bridge0@0: Address already in use
[...]

Issuing the following command revealed the process already binding udp port 67:

sudo lsof -i upd:67

Obviously bootpd already (still) uses this port. I assume this binding origins from a previous connection sharing which has not been properly shut down.

I tried to stop the bootpd daemon by issuing:

sudo /bin/launchctl unload -w /System/Library/LaunchDaemons/bootps.plist

But that returned:
launchctl: Error unloading: com.apple.bootpd

The lsof command issued previously revealed the process-id of bootpd, so I tried to kill that process:

sudo kill

Unfortunately that did not kill the process. A brief check with lsof reveiled that bootpd is still running with the same process-id. I guess a simple "kill" command failes for the same reason as launchctl.
Because bootpd can not be shut down in a normal way I killed it "the hard way":

sudo kill -9

A quick lsof afterwards showed that no process is now using port 67!

Now I was able to stop my connection sharing (I forgot to stop it before killing bootpd) and start it - and it works perfect again, without any reboot! :-)

I assume that there is a problem when the Mac enters standby mode with active connection sharing. It seems to fail to resume it properly afterwards. :-/

To quickly resolve this issue if it occurs again, I wrote a script containing the following line which automates searching the process id of bootpd and killing it:

sudo lsof -i udp:67 | grep bootpd |awk '{ print $2 }'| while read pid; do echo killing $pid; sudo kill -9 $pid&&echo "successfull..."; done

@juanfal
Copy link

juanfal commented Apr 3, 2018

Definitive workaround to wifi-sharing constant breaking down in MacosX

I have found a system that finally works and when, less frequently wifi sharing breaks, it manages to recover it automatically in a minute.

The solution is a ~/Library/LaunchAgents/com.me.wifisharingup.plist daemon with the next contents:

<?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>Label</key>
  <string>com.juanfal.wifisharingup</string>

  <key>ProgramArguments</key>
  <array>
    <string>/Users/mi/bin/wifisharingup.sh</string>
  </array>
  <key>Nice</key>
  <integer>1</integer>

  <key>StartInterval</key>
  <integer>60</integer>

  <key>RunAtLoad</key>
  <true/>

  <key>StandardErrorPath</key>
  <string>/Users/me/Library/Logs/wifisharingup.err</string>

  <key>StandardOutPath</key>
  <string>/Users/me/Library/Logs/wifisharingup.out</string>
</dict>
</plist>

You can see, each minute it runs the simple script that follows. Be careful making the previous plist be owned by the root and launch it with:

sudo chown root com.me.wifisharingup.plist
sudo launchctl load /Users/me/Library/LaunchAgents/com.me.wifisharingup.plist

The script it launches each minute (don't forget to make it executable) is:

#!/bin/sh

if [[ ! `ipconfig getifaddr en1` ]]; then
    /usr/sbin/networksetup -setairportpower en1 off
    /usr/sbin/networksetup -setairportpower en1 on
    echo `date` >> "/Users/me/Library/Logs/wifisharingup.err"
else
    touch "/Users/me/Library/Logs/wifisharingup.out"
fi

I think the simple periodically (each minute) call to ipconfig getifaddr en1 refreshes something in what is the wifi sharing daemon. Whatever it is, any moment the wifi sharing fails, it looses the self assigned IP address, and then, ipconfig getifaddr en1 fails, so my script totally resets wifi, making it rebuild its previous status and recovering the wifi-sharing.

It has been working for days so far inside a MacMini without keyboard, mouse or monitor, but only plugged into the Ethernet and giving my wifi gadgets access to the world.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment