1. Overview
After we wake up a Linux machine from sleep mode, we expect every service that was stopped to reduce power consumption to resume functioning correctly. However, we might sometimes face networking issues in the form of the Wi-Fi connection failing to reconnect.
In this tutorial, we’ll discuss the most reliable solutions for reconnecting Wi-Fi after a system suspend.
2. Understand the Problem
Before diving into solutions, it’s important to understand some reasons for Wi-Fi connection issues occur after suspending the system:
- Power management settings: When the system suspends, it cuts power to most components, including network devices, to save energy, meaning the Wi-Fi adapter might stay turned off when the system resumes.
- Driver issues: Outdated or incompatible drivers can cause connectivity problems.
- Hardware incompatibility: Certain WiFi adapters may have compatibility issues with the system’s hardware or firmware.
Figuring out and patching the root cause of these issues can be a bit tricky. However, there are plenty of workarounds to get the Wi-Fi back up without rebooting the system.
3. Avoid Suspend Mode
Practically speaking, if suspend mode causes Wi-Fi problems, we have a couple of options. We can either stop the system from going into sleep mode altogether or make sure the wireless device stays unaffected when it does.
3.1. Prevent System Suspend
Linux systems cut power and enter sleep mode in four states:
- sleep
- suspend
- hibernate
- hybrid
So, if we decide to go this route, it’s best to disable all of them.
We can use systemctl to stop the system from entering any of the suspend modes we mentioned:
$ sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
Created symlink /etc/systemd/system/sleep.target → /dev/null.
Created symlink /etc/systemd/system/suspend.target → /dev/null.
Created symlink /etc/systemd/system/hibernate.target → /dev/null.
Created symlink /etc/systemd/system/hybrid-sleep.target → /dev/null.
Here, we use the mask option to link the sleep.target, suspend.target, hibernate.target, and hybrid-sleep.target unit files to /dev/null. This prevents the system from activating the selected units. Ultimately, this prevents it from entering any suspend state and thereby keeps the Wi-Fi active.
Conversely, the unmask option does the opposite and re-activates the selected unit file.
3.2. Turning off Wireless Power Management
Another way to address Wi-Fi connection issues after a suspend operation is by adjusting the power save settings in NetworkManager.
First, let’s open the default-wifi-powersave-on.conf configuration file and set the value of wifi.powersave as 2 to disable Wi-Fi power saving for NetworkManager:
$ cat /etc/NetworkManager/conf.d/default-wifi-powersave-on.conf
...
[connection]
wifi.powersave = 2
Here, sudo might be necessary to edit this system configuration file. If we ever need to restore the default settings, we can simply change wifi.powersave back to 3.
Finally, to apply the changes, we restart NetworkManager or reboot the system. Notably, these changes are persistent across reboots.
3.3. Excluding Wireless Devices
Alternatively, we can be specific and use the iwconfig command to turn off power management for the working wireless device.
Let’s list the available network devices:
$ iwconfig
enp0s3 no wireless extensions.
wlx00 IEEE 802.11abgn ESSID:"SSID"
Mode:Managed Frequency:2.462 GHz Access Point: 00:00:00:00:00:00
Bit Rate=24 Mb/s Tx-Power=22 dBm
Retry short limit:7 RTS thr:off Fragment thr:off
Power Management:off
Link Quality=42/70 Signal level=-68 dBm
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:2 Invalid misc:18 Missed beacon:0
lo no wireless extensions.
Now let’s choose a wireless device like wlx00 and turn off its power-saving feature:
$ sudo iwconfig wlx00 power off
This command prevents wlx00 from getting turned off during system suspend. However, this change is lost upon reboot. To make it persistent, we can add the above command to the .bashrc or similar initialization file.
4. Manual Wi-Fi Re-Activation
Of course, disabling suspend or power saving mode is rarely the ideal solution. Instead, we can restart the network service or the Wi-Fi device; there are many ways to do so. While it’s beyond the scope of this article to list them all, we can focus on the most common ones.
4.1. Using nmcli With NetworkManager
The nmcli command can control NetworkManager’s behavior. This means we can use it to reload network devices and restore Wi-Fi connectivity.
Let’s use its networking control command to turn off and on all interfaces managed by NetworkManager:
$ nmcli networking off
$ nmcli networking on
Alternatively, we can use the radio transmission control command to restart the Wi-Fi:
$ nmcli radio wifi off
$ nmcli radio wifi on
We can also be more specific and target a Wi-Fi interface to force the manager to re-read its connection profile:
$ nmcli connection down wlx00
$ nmcli connection up wlx00
This command is particularly useful if we have multiple Wi-Fi interfaces and want to restart only the faulty one.
4.2. Using ip link for Systemd-Networkd
For systems managed by systemd-networkd, we can use ip link to disable and enable the Wi-Fi interface:
$ sudo ip link set wlx00 down
$ sudo ip link set wlx00 up
As before, the above commands first bring the wlx00 interface down, then bring it back up.
4.3. Restarting the Networking Service
The most common solution to almost all network connection issues is to stop and start the entire networking service.
Since most contemporary Linux systems run on systemd to manage services, we can use the systmctl command to restart the operating network service.
So, let’s restart NetworkManager:
$ sudo systemctl restart NetworkManager.service
Similarly, we can the same in systems managed by the networkd daemon:
$ sudo systemctl restart systemd-networkd
Additionally, regardless of the network management service, we can restart the network by re-applying the current netplan configuration:
$ sudo netplan apply
This way, we fully refresh the network configuration and reconnect to the Wi-Fi.
4.4. Reloading the Wi-Fi Driver
Restarting the network might not prove effective at all times. Sometimes it may seem that the only way to restore Wi-Fi connectivity is by rebooting the system.
However, another useful workaround is to target the Wi-Fi device’s driver. Specifically, we identify the Wi-Fi hardware and the associated driver or module. Afterward, we remove and add it back again.
First, let’s list the network controllers with the lspci command to identify the Wi-Fi hardware and the driver in use:
$ lspci -knn
...
03:00.0 Network controller: Intel Corporation Centrino Wireless (rev 34)
Subsystem: Intel Corporation Centrino Wireless-N 6205 AGN
Kernel driver in use: iwlwifi
Kernel modules: iwlwifi
...
Here, the command lists all PCI devices and shows the kernel driver handling each device.
We can notice from the above output that the Wi-Fi controller uses the iwlwifi driver. So the next step is to remove this module with rmmod and then reload it with modporbe:
$ sudo rmmod iwlwifi
$ sudo modprobe iwlwifi
Reloading the driver should restore the Wi-Fi interface working without needing to reboot the system.
5. Automatic Wi-Fi Reactivation
Rather than manually entering commands to restart the Wi-Fi, we can create a simple script or service to trigger this action automatically after a suspend.
5.1. Create a Hook Script
We can add a script in /usr/lib/systemd/system-sleep/ directory to run commands after the system resumes.
Let’s start by creating a file named wifi-reload with touch and make it executable with chmod +x:
$ sudo touch /usr/lib/systemd/system-sleep/wifi-reload
$ sudo chmod a+x /usr/lib/systemd/system-sleep/wifi-reload
Next, we write a shell code to ensure the system executes a command, such as sudo netplan apply, after waking from a suspend:
$ cat /usr/lib/systemd/system-sleep/wifi-reload
#!/bin/sh
case "${1}" in
post)
sudo netplan apply
;;
esac
Here, the post argument indicates the event of the system resume. In that event, we add the sudo netplan apply line to auto-reload the network configuration whenever the system wakes from a suspend.
5.2. Create a Wi-Fi Restarting Service
Rather than creating a shell script, we can write a custom systemd service to handle executing a Wi-Fi restarting command. This method is kind of similar to the previous one, but more controlled, specific, and intuitive.
To begin, we create a unit file named wifi-reload.service in the /etc/systemd/system/ directory. Then, we add to it the necessary configurations to auto-restart the network at system resume:
$ cat /etc/systemd/system/wifi-reload.service
[Unit]
Description=Restart Wi-Fi
After=suspend.target
After=hibernate.target
After=hybrid-sleep.target
[Service]
Type=oneshot
ExecStart=/usr/sbin/netplan apply
[Install]
WantedBy=suspend.target
WantedBy=hibernate.target
WantedBy=hybrid-sleep.target
Under the [Unit] section, we use the After= directive to specify which unit event to trigger the wifiresume.service. Additionally, we use the ExecStart= directive to define the action of the service, which in this case is netplan apply. Notably, we must write the full path to the command. Lastly, under the [Install] section, we use the WantedBy= directive to select the targets that the service should be linked to when enabled.
Finally and importantly, let’s activate the new service we created using systemctl enable:
$ sudo systemctl enable /etc/systemd/system/wifi-reload.service
At this point, this new systemd service should start invoking netplan apply whenever the system resumes from a suspend. This ensures that the Wi-Fi connection is properly restored and working again without manual intervention.
6. Conclusion
In this article, we laid out the possible causes of Wi-Fi issues after a suspend and studied different ways to correct or work around them. Specifically, we covered methods ranging from disabling power-saving modes to issuing commands for restarting network configurations. Moreover, we explored how to create a custom service to automatically restart the Wi-Fi whenever the system wakes from a suspend.
In conclusion, what causes the Wi-Fi to stop working after a suspend can be ambiguous and hard to patch. However, there are numerous workarounds to restore connectivity without rebooting. Among these, it’s always best to choose a method that automatically reloads the network upon system resume to eliminate the need for constant user intervention.