Launch Pycharm as root from the launcher

How can I launch Pycharm from a launcher with root privileges?

I can do this from a terminal window using sudo ./pycharm.sh , but I would like to do the same directly from the launcher.

+10
source share
4 answers

I came across another way to solve this problem, so I decided to share it (this answer is more like an alternative to other answers).

It is worth mentioning here that this solution “attacks” the problem by executing only a specific Python script (in the pycham IDE) in root mode, and not the entire pycharm application.

1) Disable the password to start Python:

This will be achieved by editing the /etc/sudoers.d/python file. What we need to do is add an entry to this 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: hostname

2) Create a "sudo script": the purpose of this script is to give the python user privilege to run as root.

Create a script called python-sudo.sh and add the following to it:

 !#/bin/bash sudo /usr/bin/python " $@ " 

Note, again, that the path is the path to your Python, as in the previous step.

Remember to give permission to execute this script with the command: chmod , i.e.

chmod +x python-sudo.sh

3) Use the python-sudo.sh script as the Pycharm interpreter:

Within Pycharm, go to: File --> Settings --> Project interpreter

At the top right, click the Settings icon and click Add Local.

In the browser option, select the python-sudo.sh script that we created earlier. This will give Pycharm the privilege of running the Python script as root.

4) Debugging the test: all that remains to be done is debugging a specific Python script in the pycharm IDE. This can be easily done by right-clicking the script for debugging → by clicking "Debug sample_script_to_debug.py"

Hope this was helpful and let me know if there are any errors in this approach.

Cheers

Guy.

+7
source

Try: gksudo ./path/to/pycharm/executable

More on gksudo

If you are on ubuntu and don't have gksudo , install it using:

 apt-get install gksu 

Here is an example launch 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 the community version, yours may be different.
+10
source

Starting from this post (June 28, 2018), I have been working with Pycharm-2018.1.4 on Ubuntu 18.04 Bionic Beaver. The solution that helped 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

eg:

guya surface-pro = (root) NOPASSWD /usr/bin/python3.6

+2
source

I needed to run the script from PyCharm as root, as OP, but the accepted answer did not help me, because 1.) I installed PyCharm via flatpak and 2.) The gksu command gksu not available on newer versions of Ubuntu and Mint.

I couldn’t find a way to make everything work consistent with installing flatpak, so I uninstalled the PyCharm flat package and then reinstalled PyCharm in the “normal” way. The accepted answer is based on the gksu , which is not available on my OS (Mint 19.2). Fortunately, pkexec is a suitable alternative and was already available on my system. Then I updated my startup 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 pop up a window asking you for a password every time you start PyCharm through the .desktop file.

+1
source

Source: https://habr.com/ru/post/1246813/


All Articles