Skip to content

Instantly share code, notes, and snippets.

@jyore
Last active January 2, 2024 15:18
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jyore/aae1d0e6e482b4d152a4bcf5b5749eed to your computer and use it in GitHub Desktop.
Save jyore/aae1d0e6e482b4d152a4bcf5b5749eed to your computer and use it in GitHub Desktop.
Keep Your MacBook Network Connections Alive As It Sleeps!

MacBookNetworkAliveOnSleep

As someone that regularly has multiple SSH/VPN sessions open, it can be a huge inconvience to lose all the connections when I lock my screen to get up to go to a meeting, lunch, etc. This is especially true for those connections that require two factor authentication. So, how can I tell my MacBook to keep those connections alive, even though my screen is locked?

Well, turns out that it is pretty simple. All that is needed is to run the following command.

cd /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources
sudo ./airport en0 prefs DisconnectOnLogout=NO

NOTE: It's a good idea to run ifconfig to check which network interface you are currently using. en0 is more times than not the correct one, but you might be on a different interface.

To verify that your command options took effect, you can run the following command

./airport en0 prefs

This should give you a printout with information similar to the following:

AirPort preferences for en0:
DisconnectOnLogout=NO
Unable to retrieve JoinMode
JoinModeFallback=DoNothing
RememberRecentNetworks=YES
RequireAdminIBSS=NO
RequireAdminNetworkChange=NO
RequireAdminPowerToggle=NO
WoWEnabled=YES

The DisconnectOnLogout option should be set to NO.

@azfarhashmi
Copy link

will this setting persist over reboot?

@cauli
Copy link

cauli commented Sep 19, 2019

Oneliner that will get the correct Wi-Fi entry from networksetup as input:

sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport $(networksetup -listallhardwareports | grep -C 3 Wi-Fi | grep Device: | sed 's/Device: //g') prefs DisconnectOnLogout=NO

@shiypc
Copy link

shiypc commented Dec 15, 2020

i don't think this works on big sur, i keep getting a help menu every time...

edit: i was using vhc128 instead of en0, my bad lol

@bobstuart
Copy link

I like your one-liner but for me it got en6 and en0 I changed the grep -C 3 to a grep -A 2 since you only really want the lines after wifi anyway.

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