Running Pycharm as root from launcher - linux

How is it possible to run Pycharm from the launcher with root privileges?
I can do that from the terminal window, with sudo ./pycharm.sh, but I'd like to do the same directly from the launcher.

I have encountered another way to solve this issue so I thought to share it (this answer is more like an alternative for the other answers).
It is worth to mention that this solution "attacks" the problem by running only a certain Python script (within the PyCharm IDE) in root mode , and not the entire PyCharm application.
1) Disable requiring password for running Python:
Recommend using the visudo program to edit the file to ensure syntax is correct. Otherwise you may be locked out of using sudo.
visudo /etc/sudoers.d/python
What we need to do is add an entry in that file as follows:
user host = (root) NOPASSWD: full_path_to_python , for example:
guya ubuntu = (root) NOPASSWD: /usr/bin/python
NOTES:
user can be detected by the command: whoami
host can be detected by the command: hostname
2) Create a "sudo script": The purpose of this script is to give python privilege to run as root user.
Create a script called python-sudo.sh , and add the following into it:
#!/bin/bash
sudo /usr/bin/python "$#"
Note, again, that the path is the path to your Python as the previous phase.
Don't forget to give execution permissions to this script using the command: chmod, i.e.-
chmod +x python-sudo.sh
3) Use the python-sudo.sh script as your PyCharm interpreter:
Within PyCharm go to: File --> Settings --> Project interpreter
At the right top hand side click the "setting" icon, and click "Add local".
In the browser option choose the python-sudo.sh script we have created previously. This will give PyCharm the privilege to run a python script as root.
4) Debug the test: All there is left to do is actually debug the specific Python script in the PyCharm IDE. This can be done easily via Right-click on the script to debug --> hit "Debug sample_script_to_debug.py"

Try: gksudo ./path/to/pycharm/executable
More about gksudo
If you're on ubuntu and don't have gksudo install it using:
apt-get install gksu
Here is an example launcher configuration (under: ~/.local/share/applications/jetbrains-pycharm-ce.desktop):
[Desktop Entry]
Version=1.0
Type=Application
Name=PyCharm Community Edition
Icon=/home/YOUR_USER/pycharm/bin/pycharm.png
Exec=gksudo -k -u root "/home/YOUR_USER/pycharm/bin/pycharm.sh" %f
Comment=Develop with pleasure!
Categories=Development;IDE;
Terminal=false
StartupWMClass=jetbrains-pycharm-ce
ce indicates community edition, yours may differ.

As of this post (June 28 2018) I am running Pycharm-2018.1.4 on Ubuntu 18.04 Bionic Beaver. The solution that worked for me was to simply edit the sudoers.d file and add the following to the last line:
user host = (root) NOPASSWD: full_path_to_python
for example:
guya surface-pro = (root) NOPASSWD /usr/bin/python3.6

I had the need to run a script from PyCharm as root like the OP, but the accepted answer didn't work for me because 1.) I had installed PyCharm via flatpak and 2.) The gksu command isn't available on newer versions of Ubuntu and Mint.
I couldn't find a way to make things work consistently with the flatpak install so I uninstalled the PyCharm flatpak and then reinstalled PyCharm the "normal" way. The accepted answer relies on the gksu command which is not available on my OS (Mint 19.2). Thankfully pkexec is a suitable alternative and was already available on my system. I then updated my launcher file (~/.local/share/applications/PyCharm.desktop) as follows. The important bit is the Exec line:
[Desktop Entry]
Name=PyCharm
Exec=pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY /opt/pycharm-community-2019.2.2/bin/pycharm.sh
Comment=PyCharm
Terminal=false
Icon=/opt/pycharm-community-2019.2.2/bin/pycharm.png
Type=Application
The pkexec command will cause a popup to appear prompting you for your password each time you launch PyCharm via the .desktop file.

Here's a sample git repo that is a proof-of-concept for how to do this. Should be as simple as following the instructions in the README.md. My team needed to do this for one of our products and we used that as the workflow.
Hope this helps! https://github.com/samayer12/sudome
This answer builds upon the description here that was first pointed out by Ed Jones.

I have Pycharm-community 2020.
I edited the
/var/lib/snapd/desktop/applications/pycharm-community_pycharm-community.desktop
file with nano
just modified the Desktop Entry as follow:
[Desktop Entry]
X-SnapInstanceName=pycharm-community
Version=1.0
Type=Application
Name=root PyCharm Community Edition
Icon=/snap/pycharm-community/222/bin/pycharm.png
Exec=sudo env BAMF_DESKTOP_FILE_HINT=/var/lib/snapd/desktop/applications/pycharm-community_pycharm-community.desktop /snap/bin/pycharm-community %f
Comment=root Python IDE for Professional Developers
Categories=Development;IDE;
Terminal=false
StartupWMClass=jetbrains-pycharm-ce
StartupNotify=true
that is: added the sudo command in front of the EXEC item, put "root" in front of couple of fields (see above) and saved with a different name:
/var/lib/snapd/desktop/applications/root_pycharm-community_pycharm-community.desktop
now I have two icons in the launcher, one that is the standard and the other one with "root" as leading word in the description and runs as root.
Note:
the original desktop launcher (which is still there):
[Desktop Entry]
X-SnapInstanceName=pycharm-community
Version=1.0
Type=Application
Name=PyCharm Community Edition
Icon=/snap/pycharm-community/222/bin/pycharm.png
Exec=env BAMF_DESKTOP_FILE_HINT=/var/lib/snapd/desktop/applications/pycharm-community_pycharm-community.desktop /snap/bin/pycharm-community %f
Comment=Python IDE for Professional Developers
Categories=Development;IDE;
Terminal=false
StartupWMClass=jetbrains-pycharm-ce
StartupNotify=true

Related

How to run a script on a terminal at startup?

I'm working on a project in which I'm using the Raspberry Pi 3B+ has the latest Raspbian GNU/Linux 10 (buster). At present, my code (shell script) is running in the crontab (with #reboot command). As we all know that the script added in the crontab runs in the background, hence, I can't see the debug messages it is printing. Therefore, I'm looking for a feature or function, which should be similar to the process in which I run the code in the terminal.
By doing some research online, I found many references related to this kind of requirements. The most common method was making a .desktop file and add into ~/.config/autostart
[Desktop Entry]
Exec=lxterminal -e "sudo sh /home/pi/Desktop/main_pro/keeper_sh.sh"
Type=Application
But unfortunately, there is no autostart folder in my ./config directory. could someone help me in achieving this task?

Not able to run the jar file during startup of system

I have RedHat linux. Intention is to start the java based GUI app after linux system finishes boot up. I have edited /etc/rc.local and kept
/usr/java/jre1.7.0_60/bin/java -jar /home/rfgwtest/Desktop/CEM/CEM_v2.0_Beta08/CEM_v2.0_Beta08/PME_CEM.jar &
at the end of the file. I am able to run this file for testing by executing the file on prompt. no errors of path or so.
what is missing as part of this? i have found this tip from the RedHat website
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Installation_Guide/s1-boot-init-shutdown-run-boot.html
is there a common way to do this across the different kind of distribution ?
Maybe, creating a desktop entry will do it.
To do that,
$ cd
$ nano .config/autostart/javaApp.desktop
now, write this inside it,
[Desktop Entry]
Type=Application
Exec=usr/java/jre1.7.0_60/bin/java -jar /home/rfgwtest/Desktop/CEM/CEM_v2.0_Beta08/CEM_v2.0_Beta08/PME_CEM.jar
Hidden=false
NoDisplay=false
Name[en_IN]=StartJAR
Name=StartJAR
Comment[en_IN]=Jar comment
Comment=Jar comment
save the file and then, restart or re-login to the system.

LXDE .desktop file permission issue

I’m trying to run a Python script via a desktop icon/shortcut/launcher on a Raspberry Pi using Raspbian and LXDE. I have to use a desktop launcher since the script has to be started via a tiny touchscreen only accepting left-clicks and without a keyboard.
I think that I have a permission issue since the script fails at a point where it should execute some system commands which require root.
The script works if I do the following:
Open a terminal
Enter sudo python3 program.py
I tried to replicate this behavior with a .desktop file using the following config:
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Exec=sudo python3 program.py
Terminal=true
Icon=path/to/icon.png
Name=Program Launcher
I suppose I missed something obvious, but since I don’t work with Linux usually I’m a bit lost here.
Edit: The problem was not a missing permission but an incorrectly assumed working directory and is now solved. Sorry for my confusion.
In detail: during development the script was started from its own folder while the desktop launcher used /home/pi as working directory. In general that's not a problem, but in the script a hardcoded path was used and a required file not found when using the launcher. The real problem was sloppy swallowed and the status code of a permission issue returned. So it was simply a bad code issue.
As docs for desktop entries says:
The Exec key must contain a command line. A command line consists of
an executable program optionally followed by one or more arguments.
The executable program can either be specified with its full path or
with the name of the executable only. If no full path is provided the
executable is looked up in the $PATH environment variable used by the
desktop environment. The name or path of the executable program may
not contain the equal sign ("="). Arguments are separated by a space.
It leads to conclusion, that maybe python3 is not within $PATH used by the desktop environment. Try to check full path of your python3 and sudo with:
whereis python3
whereis sudo
on my Archlinux it gives me /usr/bin/python3 and /usr/bin/sudo. Try to modify your Dekstop entry to something like:
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Exec=/usr/bin/sudo /usr/bin/python3 program.py
Terminal=true
Icon=path/to/icon.png
Name=Program Launcher
Let me know if this helps.

run jedit single command from terminal linux

I'm trying to run jedit from the terminal on fedora. its installed to the path /usr/local/share....
How do I get it to run "jedit" from the terminal rather than switching to the directory and typing java -jar jedit.jar to execute the program.
I created a sh file in that same direectory with "java -jar jedit.jar" and added that directory to ~/.bashrc. I'm aware that I need to type "sh file.sh" to run that file but I know there is a gap in my knowledge somewhere.
i want to use jedit as a commit editor for git and be able to change core.editor='jedit -w' in git config
someone please help
I assume you used the Java installer?
If this is the case, the installer should also have placed a jedit launch script in /usr/local/bin which should be in your PATH by default, so calling jedit from a terminal should instantly work after installation.
If it does not work, then please check whether the file jedit is available in /usr/local/bin and whether /usr/local/bin is in your PATH environment variable.

Debugging in pyCharm with sudo privileges?

I've tested code that requires root access in pyCharm by running sudo pycharm.sh but this is not the way I would recommend of doing so.
I know it's possible to debug with sudo privileges by running the python interpreter as sudo in pyCharm but how do we do this?
Create a shell script that does "sudo python" and forwards the arguments, and configure that script as a Python interpreter in PyCharm.
Name of this shell script should start with python (source: http://forum.jetbrains.com/message/PyCharm-424-3).
In PyCharm new version, it has a configure to run Python interpreter in root, no need workaround. See picture below. Check to checkbox: Execute code using this interpreter with root privileges via sudo
For what it's worth, I've managed run a python script with sudo priviledges (on Ubuntu 16.04) like this:
In the very first line in the script, define the interpreter like this:
#!/usr/bin/sudo python
Make the script executable:
chmod +x myscript.py
Run the script directly, without specifying the python interpreter yourself:
./myscript.py
The script will ask for sudo password and continue running with elevated priviledges.
I solved this problem by copying /usr/bin/python3 in my home, then setting cap_net_bind_service capability:
cp /usr/bin/python3 ~/python35-setcap
sudo setcap 'cap_net_bind_service=+ep' ~/python35-setcap
And then using ~/python35-setcap as python interpreter in pycharm.
This way, you can bind lower ports, but not any python 3 program can do it, and pycharm can still kill your script. You could also restrict execute permission to yourself if you want more security.
I have encountered the same problem trying to debug Bluetooth related code on a Raspberry Pi. I suppose, since you're doing remote debug on the device, that the device is for development use only. In such a case, in my humble option, you should permit ssh root login, so you can configure PyCharm to use the root user and you don't need to sudo. That's the solution I have chosen.
The following instructions are for a Raspberry Pi, but the procedure is the same for any Linux distribution:
First of all, add your public key to the authorized_keys:
cat ~/.ssh/id_rsa.pub | ssh pi#raspberrypi "mkdir -p ~/.ssh && cat >>
~/.ssh/authorized_keys"
Then login into the Raspberry Pi:
ssh pi#raspberrypi
Once you have a console copy your key into the root directory:
sudo mkdir /root/.ssh
sudo cp authorized_keys /root/.ssh/
Finally edit sshd_config adding PermitRootLogin without-password:
sudo vim /etc/ssh/sshd_config
Use your preferred editor.
Now you are able to ssh inside the Raspberry Pi as root:
ssh root#raspberrypi
Using root instead or pi user, give you the ability to run your code, even remotely, with root privileges, as
required by BlueZ.
I have encounter another way to solve this issue so I thought to share it (this answer is more like an alternative for the other answers).
It is worth to mention here that this solution "attacks" the problem by running only a certain Python script (within the pPyCharm IDE) in root mode , and not the entire PyCharm application.
1) Disable requiring password for running Python:
This will be achieved by editing the /etc/sudoers.d/python file. What we need to do is to add an entry in that file as follows:
user host = (root) NOPASSWD: full_path_to_python, for example:
guya ubuntu = (root) NOPASSWD: /usr/bin/python
NOTES:
user can be detected by the command: whoami
host can be detected by the command: hostname
2) Create a "sudo script": The purpose of this script is to give Python privilege to run as root user.
Create a script called python-sudo.sh , and add the following into it:
#!/bin/bash
sudo /usr/bin/python "$#"
Note again that the path is the path to your Python as the previous phase.
Also, this path is the path to Python2 on the system.
Don't forget to give execution permissions to this script using the command: chmod
chmod +x python-sudo.sh
3) Use the python-sudo.sh script as your PyCharm interpreter:
Within PyCharm go to: File --> Settings --> Project interpreter
At the right top hand side click the "setting" icon, and click "Add local".
In the browser option choose the python-sudo.sh script we have created previously. This will give PyCharm the privilege to run a Python script as root.
4) Debug the test: All there is left to do is actually debug the specific Python script in the PyCharm IDE. This can be done easily via Right-click on the script to debug --> hit Debug sample_script_to_debug.py
For those looking for a cleaner solution and don't mind entering a password each time.
Go to your Run Configuration > Edit Configurations
Under 'Execution', check the Emulate terminal in output console option.
This will allow you to debug a Python script while maintaining your current user and giving elevated sudo privileges to the script when it's needed. It also makes it easier to maintain different virtual environments if you work across multiple projects.
Terminal:
sudo ./Pycharm
this way you can start PyCharm as SuperUser
I follow the instructions here and success. But there is a problem that the PYTHONPATH is not valid when you use sudo. So when you edit with
sudo visudo -f /etc/sudoers.d/python
add that:
user host = (root) NOPASSWD:SETENV: /home/yizhao/anaconda3/bin/python
also your script should be:
#! /bin/bash
sudo PYTHONPATH=$PYTHONPATH /home/name/anaconda3/bin/python "$#"
Similar to what #Richard pointed out, the answer posted here worked for me
sudo /Applications/PyCharm.app/Contents/MacOS/pycharm on MacOS

Resources