Keyboard shortcut to a sudo-script
Solution 1
sudo requires a terminal to get the password from. Use gksudo instead; it asks for the password via a gui dialog box.
gksudo -- script
Solution 2
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.
Solution 3
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.
Related videos on Youtube
Igor Shalyminov
Updated on September 18, 2022Comments
-
Igor Shalyminov over 1 year
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.
-
pl1nk almost 12 yearsPossible duplicate: askubuntu.com/questions/116052/…
-
pt123 almost 12 yearsalso when calling scripts you need to use bash scriptname, than sh scriptname
-
Eliah Kagan almost 12 yearsThis is incorrect. You only need to run a script with
bash
if it uses the extended functionalitybash
provides. When a script has a.sh
extension, that indicates (by convention) that it can be run by the system's defaultsh
, even if that is notbash
. (Most scripts with no extension can be run bysh
too--if a script needsbash
, then its documentation should indicate this, or it should have a hashbang line at the top for it, which if you're manually invokingsh
orbash
you can check first.)
-
-
Eliah Kagan over 11 years@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 withsudo
are permitted (in thesudoers
file) to be run without requiring password authentication. -
goo over 8 yearsThis 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. -
Mohamad Khani over 8 yearswaltinator You're right.It's for crazy and lazy users.
-
Nishant over 8 years
gksudo
is not recommended anymore.(askubuntu.com/questions/284306/…). I suggest to usepkexec ./script
-
SaTa over 4 yearsI have not been able to add pkexec to a keyboard shortcut... It simply doesn't do anything.