107

I think I do not have problem with hardware. Sometimes the WiFi connection simply disconnects, apparently. So this is not necessarily the same as this case, I might not need to reload any modules.

But how to just restart wlan0? I tried restart network-manager, but this seems to leave wlan0 alone.

wifan
  • 1,171

8 Answers8

165

You just need to restart Network Manager:

sudo service NetworkManager restart

Before Ubuntu 20.10:

sudo service network-manager restart
Sand Of Vega
  • 107
  • 6
Radu Rădeanu
  • 174,089
  • 51
  • 332
  • 407
35

These don't need root, in case you are scripting:

nmcli networking off 
nmcli networking on

For more do: man nmcli

EDIT:

As these guys are saying in the comments, for WI-FI only:

nmcli radio wifi off
nmcli radio wifi on
Iacchus
  • 1,392
22

Try this:

sudo ifconfig wlan0 down
sudo ifconfig wlan0 up
Marc
  • 823
V-X
  • 321
13

"Reload the Driver"

Find the module name

Let's find the name of the kernel module for your wireless connection:

sudo hwinfo --network

(Install package hwinfo if you don't have it.)

Look for the module name in the "Driver" line.

Reload the module

Now unload then re-load the module. For example, my module name is iwlwifi

You might get lucky with no error message, in which case you can immediately reload it with

$ sudo modprobe iwlwifi

but most probably you will get this failure message:

$ sudo modprobe -r iwlwifi
modprobe: FATAL: Module iwlwifi is in use.

So we go looking for other modules using iwlwifi:

$ lsmod |grep iwlwifi
iwlwifi               241664  1 iwldvm
cfg80211              765952  4 iwldvm,iwlwifi,mac80211,rtl8187

On the left is the module name, and on the right are the other modules using it. So let's try disabling iwldvm first:

$ sudo modprobe -r iwldvm

If this works, then we can now successfully disable iwlwifi

$ sudo modprobe -r iwlwifi

And now re-enable both modules in the reverse order:

$ sudo modprobe iwlwifi
$ sudo modprobe iwldvm

Done!

This is the only procedure that worked for me in resetting low level settings (frag, rate) that I had set using iwconfig.

What it does effectively is "reload the driver".

Rolf
  • 2,229
7

You could try killing the power to your device. Assuming you are unable/unwilling to physically disconnect the device, you should run (as root): iwconfig wlan0 txpower off. I would then wait 10-15 seconds to make sure whatever hardware issue has caused the problem has been stopped, then: iwconfig wlan0 txpower auto.

Or, you can simply run rfkill and block/unblock your device. To do so, run rfkill block wifi, followed by rfkill unblock wifi. This second option should be faster, since you only need to wait 2-3 seconds between commands, as opposed to 10-15 seconds. In fact, on my machine, I don't need to wait at all, although I suspect this depends on your WiFi hardware. This option can also be done as a regular user, no root needed.

You can also restart NetworkManager. If you use systemctl as your init system (as is the case with newer versions of Ubuntu), you can use systemctl restart NetworkManager. Otherwise, you can use sudo initctl restart network-manager. If you don't know what init system you use, try both commands and see what works.

TSJNachos117
  • 1,532
  • 2
  • 16
  • 20
3

Created a script based on prior link advice with some mixing & matching of prior links. This works for me running under Mint Linux 17.3.

The file below does not require root access. It also only restarts wifi only if it is already down. Now I just need to add this script to a cron job to check my wifi connection every 15 minutes or so.

#!/bin/bash

wlan=$(/sbin/ifconfig wlan0 | grep inet\ addr | wc -l)
if [ $wlan -eq 0 ]; then
nmcli nm wifi on
else
echo "interface is up"
fi
derHugo
  • 3,376
  • 5
  • 34
  • 52
grynt
  • 31
1

As @TSJNachos117 mentions in their comment, for versions from 15.04 onwards, Ubuntu switched to systemd as the service manager. So, one can use the following command, equivalent to the one in Radu Rădeanu's answer, to restart the Network Manager service:

sudo systemctl restart NetworkManager
0

The workaround using "systemctl restart NetworkManager" works for me on two different notebooks with Broadcom and Atheros WiFi under Debian Buster and Ubuntu 19.04 - where the problem with "wifi won't wake up on resume" happens on every fourth resume or so (= it typically works just fine.) I've first tried creating a desktop launcher to invoke the wifi reset manually, which works, and requires a password - but then I found several notes by people putting the reset curse into places in the system that run scripts after resume. Namely, /lib/systemd/system-sleep/ looks like a good place to put your script. And, the script should better test some conditions (obtained via cmdline arguments) to know that it's the right time to reset the NetworkManager. Apologies for linking instead of cutting and pasting - I haven't asked this particular author's permission, and he may enjoy upvotes too, for his YouTube contribution (straight to the point, and well narrated).

Other than that, I've noticed some very simple and direct solutions to the original problem from Ubuntu 16.04: wifi.scan-rand-mac-address=no in NetworkManager.conf or even just apt-get update && apt-get upgrade . Those are the optimal solution to the particular bug in 16.04. They possibly are not a solution to other misc problems of this kind, that can be worked around by the heavy-handed (but fairly swift) restart of NetworkManager on every resume from suspend.

frr
  • 139