How to restart Mac/macOS networking from the command line

I've been having a problem with my MacBook and my wireless router, so today I decided to write a script to restart the macOS wireless networking service from the command line.

Solution

In short, I use this macOS command to turn off the Mac networking service from the command line:

sudo ifconfig en0 down

and I use this command to turn the Mac wireless network service back on from the command line:

sudo ifconfig en0 up

(I found these commands on the website I linked to.)

As you can imagine, when I issue the first command, the wireless icon in the Mac menubar shows that the network is disabled, and when I issue the second command, if my Mac is able to connect to the wireless router, the icon is shown as enabled.

Notes

If for some reason the name en0 doesn’t work for you, you’ll need to run this command:

ifconfig -a

and then dig around through that output to find the right name. The key is to look through that output until you see a network address that matches the address scheme of your local area network (LAN). For instance, I know that all addresses on my LAN begin with 10.0.0, and I find an address like that under the en0 output.

Mac “restart wireless network” shell script

Also, I can never remember those commands, so I put them both in a shell script named restartNetwork.sh. Of course you can always call your script whatever you want, but I can always remember “restart,” so I put that file in my ~/bin directory with that name.

The contents of my shell script are simple:

#!/bin/sh
echo "Turning off Wi-Fi ..."
sudo ifconfig en0 down
sleep 3
echo "Turning Wi-Fi back on ..."
sudo ifconfig en0 up

In summary, if you wanted to see how to create a command-line shell script to restart your Mac/macOS wireless networking, I hope this solution is helpful.