Run "sudo arduino" with .desktop file

To the close voters: seemingly a dupe, but not quite, at least not of the suggested one: in the linked question, the commands can be added safely to the sudoers file. In case of an IDE, it is a bad idea.

So I have installed arduino IDE from the software center. Here is the thing, I need to run the command "sudo arduino" from the terminal, for arduino IDE to work correctly for me. Don't get me wrong, whenever I run the arduino from its normal icon, it works also fine. I am just using a specific library which does not compile correctly if I don't use the "sudo arduino" command to open my IDE (not sure why).

Since I need to always open the terminal, I was wondering if it is possible to create a .desktop file (or a .sh maybe?) that I can just click and will run the "sudo arduino" IDE. I want to avoid opening the terminal every time, if possible.

Thanks in advance!

Update 1 ---------------------------------------

I am not interested in just avoiding using the sudo command when launching the IDE from the terminal (so instead of writing "sudo arduion" to write "arduino"). What I want is to avoid using the terminal all together, and have an icon on my desktop that when clicked will run the arduino IDE similarly to going in the terminal and writing "sudo arduino".


Solution 1:

Suggesting this is a dupe assumes adding Arduino to the sudoers file is a good idea, which it isn't. Never add an application that potentially ruins your system, or edits code to ruin your system to the sudoers file, simply because it as a security risk.

Only add applications or scripts to the sudoers file if:

  • the code cannot be edited (stored in a location that requires administrator's permission to write on), and

  • the application does not have the potention to edit code of other applications or scripts.

What to do in this case

  1. Copy the existing .desktop file to ~/.local/share/applications:

    cp /usr/share/applications/arduino.desktop ~/.local/share/applications
    
  2. Open the file with gedit:

    gedit ~/.local/share/applications/arduino.desktop
    

    and add a section at the bottom of the file:

    Actions=Arduino IDE with sudo;
    
    [Desktop Action Arduino IDE with sudo]
    Name=Arduino IDE with sudo
    Exec=gksu arduino
    OnlyShowIn=Unity;
    

Now arduino runs "normally" when you click on it, but has the right-click option to run with sudo:

enter image description here

Notes

  • If you don't have gksu installed (on 16.04, it is pre-installed), you need to run first:

    sudo apt-get install gksu
    
  • You probably need to log out /in for the edited launcher to be effective.

Alternatively

If you don't want the Arduino icon locked to the launcher, add the command as a keyboard shortcut: choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:

gksu arduino

to a shortcut of your choice.

Solution 2:

Copy the .desktop file and edit the copy with gedit (or any text editor), i.e. by opening the editor and dropping the file into the editors window. You will see a line beginning with Exec=, something like Exec=/path/arduino. This line needs to be modified accordingly, then save the changes.

If you want to read more about desktop files I recommend:

Question: Making .desktop files - Just a few questions
Specification of Desktop files: https://standards.freedesktop.org/desktop-entry-spec/latest/index.html

Start with sudo rights by typing a password

Install gksu using the command sudo apt-get install gksu. Now, use gksu instead of sudo (Exec=gksu /path/arduino) and you will be prompted with a password input field like gparted does, when you are starting via the desktop file.