Ubuntu – Restart network with a command without need to provide password

network-managersuspend

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.

Best Answer

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.

Related Question