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.

Igor Shalyminov
  • 247
  • 1
  • 3
  • 8
  • Possible duplicate: http://askubuntu.com/questions/116052/custom-keyboard-shortcut-to-lauch-a-terminal-and-run-a-command-in-unity – pl1nk Jun 02 '12 at 19:08
  • also when calling scripts you need to use bash scriptname, than sh scriptname – pt123 Jun 02 '12 at 22:00
  • 1
    This is incorrect. You only need to run a script with bash if it uses the extended functionality bash provides. When a script has a .sh extension, that indicates (by convention) that it can be run by the system's default sh, even if that is not bash. (Most scripts with no extension can be run by sh too--if a script needs bash, then its documentation should indicate this, or it should have a hashbang line at the top for it, which if you're manually invoking sh or bash you can check first.) – Eliah Kagan Jun 03 '12 at 17:02

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
  • 46,101
  • 1
    gksudo is not recommended anymore.(http://askubuntu.com/questions/284306/why-is-gksu-no-longer-installed-by-default). I suggest to use pkexec ./script – Nishant Oct 27 '15 at 08:50
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
  • I have not been able to add pkexec to a keyboard shortcut... It simply doesn't do anything. – SaTa Oct 03 '19 at 12:48
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
    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. This is probably a Bad Idea(tm). Also, reboot is not required. Logout/Login will suffice. – waltinator Aug 20 '15 at 00:07
  • waltinator You're right.It's for crazy and lazy users. – Mohamad Khani Aug 25 '15 at 09:15
-2

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

  • 5
    Have you ever tried this? It will work when running from a terminal, but what about running from a shortcut, or from Alt-F2? I don't think it will work. – Marty Fried Jun 02 '12 at 19:29
  • 3
    @MartyFried Indeed, this will not work, since sudo needs to read password input from the command-line. This would only work in the specific situation where the commands being run with sudo are permitted (in the sudoers file) to be run without requiring password authentication. – Eliah Kagan Sep 13 '12 at 19:39
-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>