6

The wireless network on my dell laptop goes away many a times when resuming from 'suspend'. Once I run 'sudo service network-manager restart', it starts to work.

I was wondering if there was a way where I could restart the network while resuming from suspend and only if the wireless network was not up! What would be the best way to be able to restart network without having to enter a password?

I don't want to manually do it by going over to network icon and then taking some mouse actions. I did rather have a command which I could set up as a shortcut. I tried creating a bash executable with content 'service network-manager restart' and setting setuid on the executable as well as giving root the ownership of it but that didn't work.

I have Ubuntu 14.04 Trusty OS.

Champ
  • 219

5 Answers5

5

You can make a change to sudoers to allow your user account to execute the necessary commands without password.

Warning: Be sure to not delete anything from sudoers without exactly knowing what its for! You could potentially loose all admin privileges.

  1. Open sudo visudo
  2. In the section headed "Cmnd alias specification" add

    Cmnd_Alias NETWORK = /usr/sbin/service network-manager restart
    

    NETWORK is just an alias for a group of commands. Give it a different name if you prefer!

  3. At the end of the file, append the statement

    user_name ALL = (ALL) NOPASSWD: NETWORK
    

    where you substitute your user account for user_name. Also replace NETWORK with whatever name you've given the alias. One could also do without the alias and simply replace it by the command, but I prefer it this way. I find it keeps things more organized.

  4. Safe the file and exit the editor. Check with sudo -l that you are now indeed allowed to issue the command.

You still need to prepend the command with sudo, but you won't be prompted for a password anymore.

Nephente
  • 5,595
  • 1
  • 17
  • 23
5

Another possibility is to use the command line interface of Network Manager (it's not so "strong" as restarting the daemon, but it worked for me). In this case, the operation is exactly the same as if you interact with the applet, so you do not need any privilege.

To restart the wifi interface, use this code:

nmcli nm wifi off
sleep 5
nmcli nm wifi on

I had similar problems with the network dropping (it was a faulty router in my case), so I used this script added to the startup jobs:

#!/bin/bash
PINGTEST=192.168.1.1 # my router, change here. google.com should work anytime ;-)
while /bin/true; do
    if ! [ "$(ping -c 1 $PINGTEST)" ]; then
        echo "Warning: connection lost at $(date) -- restart" 1>&2  
        nmcli nm wifi off
        sleep 5
        nmcli nm wifi on
        sleep 60
        if ! [ "$(ping -c 1 $PINGTEST)" ]; then
             echo "Waiting for connection going up at $(date)" 1>&2
             sleep 60
        else 
             echo "Connection on at $(date)" 1>&2
        fi
#    else
#        echo "Connection OK on $(date)" 1>&2
    fi
sleep 60
done

it checks the connection every minute and if it does not work, it restarts the wifi.

If this is not sufficient, you probably have to use stronger weapons, like starting/stopping Network Manager. Even that sometime fails on me, and I have to resort to unload (sudo rmmod) and then reload (sudo modprobe) the wifi card driver module.

Rmano
  • 31,947
  • Some times, I am able to use 'nmcli nm wifi on' successfully and other times I have to restart the network-manager! :( – Champ Sep 29 '15 at 18:27
  • @Champ yes, it happens. I added a note about that in the answer. ath9k sometime got so messed up you have to remove/reload it... – Rmano Sep 29 '15 at 18:45
2

You should be able to add something similar to this:

YourUserName ALL=NOPASSWD: service network-manager restart

to your /etc/sudoers file and be able to restart network-manager with your script.

0

On Ubuntu MATE 20.10 I'm using:

nmcli n off
sleep 5
nmcli n on

Here is my "one-liner" to monitor and restart networking after 6 consecutive router pings failed (sometime connection restores on itself after some failed attemts):

cc=0; date; echo "monitoring network connection"; while true ; do ( ! ping -c1 192.168.88.1 >/dev/null ) && { date; cc=$((cc+1)); echo "connection lost times: $cc"; } || { [ $cc -ne 0 ] && { date; echo "connection ok"; }; cc=0; }; [ $cc -gt 5 ] && { notify-send "network restarting"; nmcli n off; sleep 5; nmcli n on; cc=4; sleep 10; }; sleep 10; done

Note: The 192.168.88.1 is the IP address of my router. Modify for your needs (8.8.8.8 should be enough if you don't know router IP address).

Note: To figure out your routers IP address see How to show (just) the IP address of my router? (the "one-line" script can be upgraded using this to automatically get te routers address)

0

I finally ended up writing the script /etc/pm/sleep.d/0000_custom:

#!/bin/sh
# /etc/pm/sleep.d/0000_custom
# Action script to lock screen and resume wifi properly
PATH=/sbin:/usr/sbin:/bin:/usr/bin
case "$1" in
    suspend|hibernate)
        #do nothing
    ;;
    resume|thaw)
        X_USER=`who| grep ' :0'|cut -f1 -d' '`
# lock screen when resuming from suspend
        sudo -u $X_USER xscreensaver-command -lock
# turn wifi on when resuming from suspend
        nmcli nm wifi on
    ;;
esac
exit 0

Thanks to @RMano and @Nephente for their answers.

Champ
  • 219