28

Occasionally, my Wi-Fi connection does no longer work for various reasons. Disabling and re-enabling Wi-Fi through the graphical interface of the network indicator does not resolve the problems in these cases.

How can I completely restart my Wi-Fi connection from the command-line without having to restart my machine (which fixes these problems)?

35

nmcli is very useful command-line utility for interacting with Network Manager. Use this command in Ubuntu 16.04 LTS

nmcli radio wifi off && sleep 5 && nmcli radio wifi on

For versions prior to 15.10 ( i.e. before transition to systemd ) the command would be slightly different:

nmcli nm wifi off && sleep 5 && nmcli nm wifi on

Good thing about it - this doesn't require root powers.

Restarting network manager itself is a good idea as well.

For 16.04 LTS:

sudo systemctl restart NetworkManager

and for 14.04 LTS:

sudo service network-manager restart

And if we really wanted to, we could even automate it with a script that will restart your wifi.

#!/bin/bash
# replace wlan0 with your device name
# as given by ip addr or ifconfig
while true 
do
    # keep checking if we have ip address    
    wifi_info=$(ip -4 -o addr  show wlan0 )
    while [ -n "$wifi_info" ];
    do
       wifi_info=$(ip -4 -o addr  show wlan0 )
       sleep 0.25
    done

    # We get here only if IP address is lost
    # which means we're off-line
    # restart wifi 
    nmcli radio wifi off && sleep 5 && nmcli radio wifi on
done
  • 4
    .service is implied, so can be omitted sudo systemctl restart NetworkManager – Zanna Aug 13 '16 at 8:42
  • 1
    @Zanna TIL... I have been typing .service this whole time. – edwinksl Aug 13 '16 at 9:10
  • Worked like a charm for me. – Josh Oct 14 '17 at 7:44
  • Thanks, excellent. Why are there 2 different commands? – neverMind9 Apr 17 '18 at 21:50
  • 1
    @TechLord Basically, it's because since the 15.04 version, Ubuntu has switched to systemd init system - the stuff that actually brings up basic services and daemons when you boot. The problem is that unlike older stuff, systemd tries to not just start stuff, but also manage the stuff. This lead to nmcli being rewritten to match with systemd. At least this is my understanding. – Sergiy Kolodyazhnyy Apr 17 '18 at 22:02
10

Two alternatives I'm thinking about follows,

First alternative, bring down/up the interface

ifconfig wlan0 down ## assumes your wlan is named wlan0
ifconfig wlan0 up

Second alternative, restart the entire network-manager. This assumes you have network-manager installed. If not, install it with the following cmd in your terminal sudo apt-get install network-manager.

sudo service network-manager restart
5

On Ubuntu 15.10 and 16.04 LTS you can use the systemd feature:

 systemctl restart NetworkManager.service
4

Use ifdown + interface name to disable network

ifdown IFACE

Use ifup + interface name to enable it

ifup IFACE

Replace IFACE with your device name as given by ifconfig

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.