Unable to start XAMPP using sudo in Desktop Entry

Solution 1:

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.