3

How do I create a keyboard shortcut to a script that needs sudo privileges? I tried to create shortcuts via Ubuntu Keyboard Settings GUI to scripts

"sh script.sh" "sudo sh script.sh"

They don't work.

5 Answers5

7

sudo requires a terminal to get the password from. Use gksudo instead; it asks for the password via a gui dialog box.

gksudo -- script
geirha
  • 47,279
3

gksudo is not recommended anymore.(see here)

Use pkexec ./script. By default, it will pop up a screen to enter credentials. See its man page for more info.

Nishant
  • 131
  • 2
1

In Ubuntu if You dont want enter your password any time use shortcut you can run this commands:

sudo visudo

This cause nano open in terminal.

In nano add username ALL=(ALL) NOPASSWD: ALL bottom of file replace username with your ubuntu username and save by ctrl + o.

After logout and login your sudo command wouldn't get password.

Notice:

If you need security don't apply the change to your ubuntu. This will allow ANY person, cat or UFO alien that gets access to your system as you, via the network or keyboard, to execute commands as root, at any time.

-2

Put the sudo commands in the script on the lines you need privileges to run. Then run normally.

paradrupal
  • 307
  • 1
  • 2
  • 6
-3

This Only work for kde desktop.

If You dont want enter your password any time use shortcut you can Use this:

echo <password> | sudo -S <script>