29

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)?

orschiro
  • 12,987
  • 16
  • 82
  • 157
  • Have a look at this on stack exchange [link](http://unix.stackexchange.com/questions/90778/how-to-bring-up-a-wi-fi-interface-from-a-command-line) – Nick Sillito Aug 13 '16 at 08:24
  • 3
    Possible duplicate of [How to disconnect from wireless connection manually using the command line without using network manager?](http://askubuntu.com/questions/276822/how-to-disconnect-from-wireless-connection-manually-using-the-command-line-witho) – Anwar Aug 13 '16 at 08:40

4 Answers4

38

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
Sergiy Kolodyazhnyy
  • 103,293
  • 19
  • 273
  • 492
  • 4
    `.service` is implied, so can be omitted `sudo systemctl restart NetworkManager` – Zanna Aug 13 '16 at 08:42
  • 1
    @Zanna TIL... I have been typing `.service` this whole time. – edwinksl Aug 13 '16 at 09:10
  • Worked like a charm for me. – Josh Oct 14 '17 at 07: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
11

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
mattias
  • 679
  • 6
  • 17
5

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

 systemctl restart NetworkManager.service
GAD3R
  • 2,821
  • 1
  • 18
  • 30
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

Sergiy Kolodyazhnyy
  • 103,293
  • 19
  • 273
  • 492
Abhijith.s
  • 51
  • 2