21

I have created a desktop icon for the gddccontrol, but to run it I need to execute it from sudo. Is there a way to run a command from the desktop icon like,

sudo -pPASSWORD gddccontrol

so it won't ask me for the password in the terminal?

I just want click and run it with sudo permissions.

[Desktop Entry]
Type=Application
Name=GDDCControl
Terminal=false
Comment=gddccontrol ddccontrol
Exec=sudo -pPASSWORD gddccontrol
Icon=~/Pictures/10429.png

When I run the icon from the terminal with the following Exec command it works good, but it doesn't work in Launcher :(

Exec=echo mypasswd | sudo -S gddccontrol
Eugene
  • 321
  • 1
  • 2
  • 11

4 Answers4

23

NEVER EVER use your password, stored in a .desktop file. It is terribly unsafe.

Furthermore, you cannot run complicated commands in a .desktop file just like that, but that is another subject.

What to do

You have a few options:

  1. Add gddccontrol to the sudoers file, as explained e.g. here. This can be done if you are sure the application cannot be used to do maliceous things.

    Then use

    Exec=sudo gddccontrol
    

    in the Exec=-line

  2. Another option is to replace the Exec= line by:

    Exec=gksu gddccontrol
    

    In this case, once you click the icon, you will be prompted to enter your password via gui.
    You possibly need to install gksu:

    sudo apt-get install gksu
    
Jacob Vlijm
  • 83,767
  • no other ways without editing sudoers and gksu ? because gksu any ways asking password but when im laying on the sofa in the evening time i would like just to run gddccontrol with the mouse decrease brightness/contrast and keep watching movie without coming to my keyboard.

    i would better give my user access to the $ gddccontrol dev:/dev/i2c-4: Permission denied

    i just want an easy way without headache to change brightness and contrast in the evening time :(

    – Eugene Aug 20 '16 at 08:32
  • 1
    @Eugene I can imagine. I would go for the sudoers file in that case. – Jacob Vlijm Aug 20 '16 at 08:37
  • 2
    Prefer pkexec over gksu; I believe there was a meta post on this. – cat Aug 20 '16 at 14:31
  • @cat pkexec isn't running on gui applications just like that. It is more complicated to set up, while gksu works perfectly. – Jacob Vlijm Aug 20 '16 at 18:36
  • But I don't need to aditionally install pkexec on 16.04. – Valentas Feb 05 '17 at 18:04
  • Just wondering if you might consider updating your answer for modern times where gksu is very messy to setup. – WinEunuuchs2Unix Sep 23 '18 at 20:34
  • 2
    gksu is deprecated. It is removed from Debian, Ubuntu 18.04 and other newer Linux distribution version. – Cagy79 Jan 02 '19 at 23:06
4

Using sudo will by definition prompt a password request. What you need to do is to grant execute permissions to your user. I'm not sure where that program is located, but for the sake of this answer, let's assume it is /usr/bin/.

In a terminal run this:

sudo chmod o+x /usr/bin/gddccontrol

That command is granting other users (like you) the right to execute the program without need for permission.

Then, in the .desktop file, just use:

Exec=gddccontrol
1

Open your terminal and run this command

cd /usr/share/applications

After that run this command

sudo gedit application.desktop

and copy and paste the following for x64 bit machines

#!/usr/bin/env xdg-open
[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Icon[en_US]=/opt/lampp/htdocs/favicon.ico
Exec=sudo /opt/lampp/manager-linux-x64.run
Comment[en_US]=start xampp manager
Name[en_US]=xampp
Name=xampp
Comment= start xampp application

Replace

Exec=sudo /opt/lampp/manager-linux-x64.run

with the path of the application you want to launch for my case it is xampp manager

Replace

Icon[en_US]=/opt/lampp/htdocs/favicon.ico

with the path of your application icon.

Save the file and run this command below

sudo visudo

Add the following line at the end of your file

<your-username> ALL = NOPASSWD: <path of application>

Type your user name and paste the path of your application without including the angle brackets like this

nelson ALL = NOPASSWD: /opt/lampp/manager-linux-x64.run

Save the file and check your applications it's there.

Hope this helps.

0

I am against hard-coding password in a plain text file so I can think of two possible solutions:

  1. make a system-wide edit that every command issued by your user with sudo is ran as superuser without prompting for password. You can find out how in this answer

  2. If you dont want sudo commands to always be executed with password prompt, best practice use gksu before your command to prompt for password in a graphic window rather than writing your raw password in a file.