Ubuntu – Program connects via USB only if called with sudo

sudousb

I have downloaded JLink package and unpacked it to /home/mkru/ARM/JLink_Linux_V498a_x86_64. Then I have added this path to my PATH variable. When I try to execute JLinkExe I get the following error:

Can not connect to J-Link via USB.

When I try to execute it with sudo I get:

sudo: JLinkExe: command not found

The only way to execute it with success is:

  1. to change directory with cd /home/mkru/ARM/JLink_Linux_V498a_x86_64
  2. then run sudo ./JLinkExe

Why does it work like that and how can I change this behaviour ? I would like to use JLinkExe without sudo and without the need to be in this specific directory.

Best Answer

Certain features require root elevations. Low-level USB interaction is one of them, I experienced the same when I wanted to use my LEGO NXT robot.

First of all, if the program only works with sudo, move it away from your home. I suggest moving it to /opt/jlink/ or something like that

To keep having to cd to the executable directory, add it to the global bath in /etc/bash.bashrc, instead of your home bashrc. Also, define an alias which automatically executes the program with root elevations. The additions to the file may look like this:

export PATH=$PATH:/opt/jlink    #or wherever you moved it
alias JLinkExe='sudo /opt/jlink/JLinkExe'

Now you can execute the program just by entering JLinkExe (you could even assign a easier-to-enter name in the alias, e.g. simply alias jlink='sudo /opt/jlink/JLinkExe').

However, you'll still be asked for your password, as the program still requires root elevation. To circumvent that, you can create an entry in your sudo setup. For this, execute sudo visudo and add this line to the end of the file

mkru ALL=(ALL) NOPASSWD: /opt/jlink/JLinkExe

After saving this, you (and only you, i.e. user mkru) shouldn't be asked for giving the program root elevations any more. Keep in mind that it will still be running with elevated privileges.


NOTE: If the program has a GUI, create the alias as alias jlink='sudo gksudo /opt/jlink/JLinkExe' to prevent it from messing up ownerships in your home directory.

Related Question