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.



P.S. My friend is actively seeking opportunities in Product Management, Marketing Operations, or Data Analytics.

He brings strong expertise in data analytics, mathematics, and finance. Currently based in Bangalore, India, he is open to both remote and in-office opportunities.

If you're looking for a driven professional with strong analytical skills and quick learning ability, please reach out to him directly:


#macos  #networking  #script  #terminal  #command 

Suggested Reading

  • * How to install Cursor AI IDE on Fedora Asahi Linux Arm64

    * From Chaos to Control: Using Direnv to Manage Multiple GitHub Accounts

    * AWS Cloudwatch Monitoring & Alerts using Bash Scripts

    * Converting Markdown to Epub or Mobi

    * Yabai - Efficient window management on macOS

  • Share this: