OSX – Disconnect/Disassociate wifi on boot

So I don’t really expect very many people to care about this, but I finally solved an OSX annoyance that’s been bugging me for a while.

My scenario is this, I use OSX on a desktop with both ethernet and wifi. I prefer to use my ethernet connection, but OSX automatically connects to wifi on boot/wake if I have any saved networks. This causes some problems with my computer being assigned multiple IP’s, trying to register multiple hostnames, etc.

Now the simplest solution would be to simply turn off wifi, and only turn it on when I need it, but that just seems kludgy to me. I want to leave the physical wifi card enabled, but just not connect to wifi on startup.

And it turns out that isn’t possible, but you can force airport/wifi to disconnect from the command line, provided you enter your admin/sudo password. Every. Single. Time.

sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -z

If you try to run that without “sudo” it will tell you:

root required to disassociate

At first, I just set up and alias to this, but it’s still not perfect, since typing a password every time the computer boots, or wakes from sleep is a bit annoying.

This morning I finally found a workaround.

This is a two part fix, first we need to add an entry to our /etc/sudoers file to allow the airport-z command to run as root, without a password.

sudo visudo

And under the line:

%admin ALL=(ALL) ALL

Add the following:

%admin ALL=(root) NOPASSWD: /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -z

This line allows users in the admin group to run the airport command to disassociate, without a password. You can further refine this by replacing “%admin” with your username, so only your user account can run the command without a password.

And a natural extension of this, is that we can now use launchd to automate this at boot.

Donwload this plist file and place it in /Library/LaunchDaemons, repair permissions, and reboot. Whenever you login, this will run, and automatically disconnect wifi. You can further automate this with a tool like sleepwatcher to automatically disconnect on wake from sleep too.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s