Command Line (CLI) Command for disconnecting wifi from current network
I'm looking for a way to disconnect from my current WIFI network via CLI. There is a way to do that, but it's a GUI way.
Any ideas how to accomplish that?
Thanks!
Note: I don't want to disable Wi-Fi because I need Airdrop to be function so I just want to disconnect it from the existing network.
EDIT: Why do I want this?
At work we connect out MBPs to Displays w/ ethernet attached. Also we heavily use AirDrop functionality. The aim is to write a script, that automatically disconnects from WIFI networks, when ethernet cable is plugged in, so that we can still use other WIFI features like AirDrop & Apple Watch unlocking.
Use networksetup
.
There are several ways to disconnect from a Wi-Fi network via Terminal:
-
Disable the Wi-Fi service. This will require root privileges. The radio will remain on, but it won't connect to anything.
$ sudo networksetup -setnetworkserviceenabled Wi-Fi off
-
Turn off the airport power (this is like clicking the (GUI) Wi-Fi icon and selecting "turn off Wi-Fi". You don't need
sudo
and you should know which interface (en0
,en1
, etc.) is your Airport adapter. If you don't try the service and macOS will attempt to guess which one it is.$ networksetup -setairportpower Wi-Fi off
In the specific case where you don't want auto join, just set the network to not auto join and then toggle the network status using networksetup
.
If you just want to disconnect from a WiFi network, disable the adapter:
$ sudo networksetup -setnetworkserviceenabled Wi-Fi off && sleep 15 && sudo networksetup -setnetworkserviceenabled Wi-Fi on
(You can issue the command without sudo
but you will get a popup prompting you for a password for each networksetup
invocation)
You can use this one (found on the internet and tried myself):
Instead of "DEVICE_NAME" use your interface (i.g.: en0)
sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport "DEVICE_NAME" -z