How to restart Mac OS Network from Terminal

The Mythical Engineer


banner

Apparently, Mac OS has some issues with Wi-Fi. It doesn’t work after it wakes up from sleep.

You have to turn wifi on and off to make it work again.

Usually my terminal is open all the time and I prefer to use keyboard for most of the operations on my setup.

You can use ifconfig utility to restart the network

sudo ifconfig en0 down
sudo ifconfig en0 up

But this command requires sudo, and writing long passwords is cumbersome.

Fortunately there’s a networksetup utility in macos, which can do it without sudo.

networksetup -setairportpower en0 off
networksetup -setairportpower en0 on

You can add these commands as alias in ~/.zshrc or ~/.bashrc file for faster access.

alias netoff='networksetup -setairportpower en0 off'
alias neton='networksetup -setairportpower en0 on'
alias netrestart='networksetup -setairportpower en0 off && networksetup -setairportpower en0 on'

Remember to source the .rc file for accessing the newly defined aliases.

source ~/.zshrc # If you use zsh as your shell
# OR
source ~/.bashrc # If you use bash as your shell

Now you just need to run netrestart on your terminal and your wifi will restart.


#macos  #networking  #script  #terminal  #command 

Suggested Reading

  • * AWS Cloudwatch Monitoring & Alerts using Bash Scripts

    * Converting Markdown to Epub or Mobi

    * Yabai - Efficient window management on macOS

  • Share this: