Debugging in pyCharm with sudo privileges?
Asked Answered
P

11

26

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?

Prasad answered 13/1, 2013 at 0:41 Comment(1)
have u finally solve the problem? If i run sudo pycharm.sh, the pycharm will be re-installed and re-configured...Verruca
I
9

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).

Iveson answered 13/1, 2013 at 17:16 Comment(7)
Sorry, guys, can you tell me more about this solution? I thought that you mean sh script but PyCharm doesn't give possibility to use it as Python Interpreter. Thank you.Servomotor
Signals from PyCharm are lost... The stop button no longer works and you get zombiesEndogamy
Can you tell me how can I create a shell script? Should I store it as a .txt file and where should I store it?Verruca
Here is a full solution to a problemSit
As @AdamMills says, the shell script approach does not preserve signals. Running PyCharm as root (with the intermediate shell script removed) works.Wreathe
People forget about the shell script solution if you can overcome it with Linux Capabilities! Linux Capabilities has been specifically designed to address these kinds of problems. For example - squidarth.com/networking/systems/rc/2018/05/28/…Otisotitis
Use sudo with -E to preserve your PYTHONPATH environment, when working with a shellscript.. Here's my shell script: sudo -E python3 "$@". I use that as a python interpreter.. Works fine with debugger, stop button also works.Kibe
D
8

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

enter image description here

Danika answered 17/6, 2019 at 3:45 Comment(2)
This sadly only works for remote interpreters, not for local ones.Atropine
Another sad news is that it apparently has been removed from PyCharm 2022 (I am running 2022.3.1).Phew
J
7

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.

Jumbled answered 26/12, 2017 at 21:18 Comment(1)
This is really the only solution that works. People forget about the shell script solution if you can overcome it with Linux Capabilities! Linux Capabilities has been specifically designed to address these kinds of problems. For example - squidarth.com/networking/systems/rc/2018/05/28/…Otisotitis
K
5

For what it's worth, I've managed run a python script with sudo priviledges (on Ubuntu 16.04) like this:

  1. In the very first line in the script, define the interpreter like this:

    #!/usr/bin/sudo python

  2. Make the script executable:

    chmod +x myscript.py

  3. Run the script directly, without specifying the python interpreter yourself:

    ./myscript.py

  4. The script will ask for sudo password and continue running with elevated priviledges.
Kettle answered 2/6, 2017 at 8:22 Comment(0)
C
4

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

Cassirer answered 19/6, 2017 at 21:15 Comment(0)
H
4

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.

Hamadryad answered 31/7, 2018 at 15:46 Comment(0)
E
3

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.

enter image description here

Ebneter answered 20/2, 2020 at 23:47 Comment(3)
Not sure how this is solving the problem presented by the userHedvah
@Hedvah - This solves it directly, the User gets debug functionality while also being presented with the prompt to enter a sudo password without having to run PyCharm with sudo and losing all their config. Try it out.Ebneter
This doesn't seem to actually do anything. Still fails w/Operation Not Permitted because it requires sudo/root access ...Letty
R
2

Terminal:

sudo ./Pycharm

this way you can start PyCharm as SuperUser

Retirement answered 2/6, 2017 at 8:34 Comment(1)
This is the only method listed that really worked for me. The other answers do work, but they're clunky and more permanent than just running PyCharm as root. For a one-off instance of debugging a script that requires super-user privileges, this is the way to go.Haye
L
1

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 "$@"
Lhary answered 29/11, 2019 at 3:28 Comment(0)
A
1

Similar to what @Richard pointed out, the answer posted here worked for me

sudo /Applications/PyCharm.app/Contents/MacOS/pycharm on MacOS
Apologia answered 20/12, 2022 at 20:49 Comment(0)
Y
1

One solution to this problem is to set the sticky bit for the interpreter in the project's virtualenv. The command looks like this:

$ sudo chmod +s .venv/bin/python

This means that this specific python interpreter will always run as root when it is started. This can of course be a huge security issue, but if access to this file is limited and only used for debugging, I found this to be the most trivial and fool-proof approach to run the program as root. Of course, don't do this for a non-virtualenv interpreter, such as the one in /usr/bin/python or similar, as it would lead to all python applications to run as root on your system!

Yerkovich answered 1/6 at 18:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.