1

I am using Ubuntu 18.04. I have installed XAMPP and created a .desktop file to run it.

[Desktop Entry]
Type=Application
Name=XAMPP Control Panel
Version=7.3.0.0
Exec=sudo -i python /opt/lampp/share/xampp-control-panel/xampp-control-panel.py
Encoding=UTF-8
Terminal=false
Comment=Start and Stop XAMPP
Icon=/home/lee/.local/favicon.ico

But, for some reason it won't run. The "Exec" line runs fine on the CL. I have it set to execute. Any ideas?

Kulfy
  • 17,696
Lee
  • 677
  • 2
  • 9
  • 14

2 Answers2

6

To run applications in distributions like Ubuntu where root account is disabled by default, sudo is used. When sudo with a command is run in Terminal, it asks for the password. But when it is used in Desktop entry, there is no way by default to provide that. However, to run the application as superuser using a Desktop entry, below methods can be adopted.

  1. Using Terminal Key:

    The Terminal key allows the application to execute in Terminal,i.e., the command line used in Exec would run as if it was executed in terminal opening a new terminal window and allowing user to enter the password. The key should be appended like this:

    Terminal=true
    

    But this method has an issue. Since it opens an extra application, i.e. the terminal window, it results in extra RAM and CPU cycles consumption.

  2. Using pkexec:

    Another way is to use pkexec. pkexec opens a graphical window to provide password. But it doesn't allow to run X11 applications as another user since the $DISPLAY and $XAUTHORITY environment variables are not set (Reference: Radu Rădeanu's answer).

    However the variables won't be expanded in desktop entry since it ain't a shell. Therefore, you can run the command using shell, i.e.

    Exec=sh -c 'pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY /path/to/executable'
    

    You can use bash or any supported shell instead of bash.

    But this would create an sh or bash process which would wait for the application to exit, thus consuming some memory.

  3. Editing /etc/sudoers:

    Some applications such as XAMPP needs to be run as superuser. You can allow some specific applications to run without even asking for password by editing /etc/sudoers and adding the application there.

    • To allow all sudoers to run the application without password, append the below line to /etc/sudoers.

      %sudo ALL = (root) NOPASSWD: /path/to/application
      
    • To allow specific user in sudoer to run the application without password, append

      USER ALL = (root) NOPASSWD: /path/to/application
      

      for example:

      kulfy ALL = (root) NOPASSWD: /opt/lampp/manager-linux-x64.run
      

    To append through command line, you can use tee with option a. Example:

    echo 'kulfy ALL = (root) NOPASSWD: /opt/lampp/manager-linux-x64.run' | sudo tee -a /etc/sudoers
    

    Warning: Don't overwrite the content of /etc/sudoers, otherwise you won't be able to perform sudo actions. Only append the above line. It is recommended to have a backup of that file.


Footnote: To use XAMPP Control Panel manager-linux-x64.run should be executed instead of xampp-control-panel.py. The earlier is an executable binary file and responsible for showing the GUI while the Python script is a part it, i.e. it is not the complete Control Panel.

Kulfy
  • 17,696
  • According to the information here, there is a special way to edit the /etc/sudoers file. I can't seem to figure it out. – Lee Nov 01 '19 at 20:40
  • @Lee That's one of the method to edit files with sudo privileges. It's basically Vi/Vim with sudo. If you are familiar with Vi, you can use that. Personally I use sudoedit which uses EDITOR which is set to nano by default. Or simply sudo nano. – Kulfy Nov 02 '19 at 11:01
1

Here is the solution, with thanks to @Kulfy

#!/usr/bin/env xdg-open
[Desktop Entry]
Name=XAMPP
Type=Application
Exec=sh -c "pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY sudo /opt/lampp/manager-linux-x64.run"
Terminal=false
Icon=/opt/lampp/htdocs/favicon.ico
Terminal=false
Lee
  • 677
  • 2
  • 9
  • 14