Linux 使用 sudo 权限在 pyCharm 中调试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14299509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Debugging in pyCharm with sudo privileges?
提问by Dayan
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.
我已经pyCharm
通过运行测试了需要 root 访问权限的代码,sudo pycharm.sh
但这不是我推荐的方式。
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?
我知道可以通过在 pyCharm 中将 python 解释器作为 sudo 运行来使用 sudo 权限进行调试,但是我们如何做到这一点?
采纳答案by yole
Create a shell script that does "sudo python" and forwards the arguments, and configure that script as a Python interpreter in PyCharm.
创建一个执行“sudo python”并转发参数的 shell 脚本,并将该脚本配置为 PyCharm 中的 Python 解释器。
Name of this shell script should start with python
(source: http://forum.jetbrains.com/message/PyCharm-424-3).
此 shell 脚本的名称应以python
(来源:http: //forum.jetbrains.com/message/PyCharm-424-3)开头。
回答by Stephan Henningsen
For what it's worth, I've managed run a python script with sudo priviledges (on Ubuntu 16.04) like this:
对于它的价值,我已经管理运行了一个带有 sudo 特权的 python 脚本(在 Ubuntu 16.04 上),如下所示:
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, withoutspecifying the
python
interpreter yourself:./myscript.py
- The script will ask for sudo password and continue running with elevated priviledges.
在脚本的第一行,像这样定义解释器:
#!/usr/bin/sudo python
使脚本可执行:
chmod +x myscript.py
直接运行脚本,无需
python
自己指定解释器:./myscript.py
- 该脚本将要求输入 sudo 密码并继续以提升的权限运行。
回答by Richard
Terminal:
终端:
sudo ./Pycharm
this way you can start PyCharm as SuperUser
这样你就可以以超级用户身份启动 PyCharm
回答by Guy Avraham
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 pycham IDE) in root mode , and not the entire pycharm application.
这里值得一提的是,该解决方案通过在 root 模式下仅运行某个 Python 脚本(在 pycham IDE 中)而不是整个 pycharm 应用程序来“攻击”问题。
1) Disablerequiring password for running Python:
1) 禁用运行 Python 时需要密码:
This will be achived by editing the /etc/sudoers.d/pythonfile. What we need to do is to add an entry in that file as follows:
这将通过编辑/etc/sudoers.d/python文件来实现。我们需要做的是在该文件中添加一个条目,如下所示:
user host = (root) NOPASSWD: full_path_to_python, for example:
用户主机 = (root) NOPASSWD: full_path_to_python,例如:
guya ubuntu = (root) NOPASSWD: /usr/bin/python
NOTES:
笔记:
user
can be detected by the command: whoami
user
可以通过命令检测: whoami
host
can be detected by the command: hostname
host
可以通过命令检测: hostname
2) Createa "sudo script": The purpose of this script is to give python privilege to run as root user.
2)创建一个“sudo脚本”:这个脚本的目的是赋予python以root用户身份运行的权限。
Create a script called python-sudo.sh , and add the following into it:
创建一个名为 python-sudo.sh 的脚本,并将以下内容添加到其中:
#!/bin/bash
sudo /usr/bin/python "$@"
Note again that the path is the path to your Python as the previous phase.
再次注意,路径是前一阶段的 Python 路径。
Don't forget to give execution permissions to this script using the command: chmod
不要忘记使用以下命令为此脚本授予执行权限: chmod
chmod +x python-sudo.sh
3) Usethe python-sudo.sh
script as your pycharm interpreter:
3)使用的python-sudo.sh
脚本作为pycharm解释:
Within pycharm go to: File --> Settings --> Project interpreter
在 pycharm 中转到: 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.
在浏览器选项中,选择我们之前创建的 python-sudo.sh 脚本。这将赋予 PyCharm 以 root 身份运行 python 脚本的权限。
4) Debugthe 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
4) 调试测试:剩下要做的就是在 pycharm IDE 中实际调试特定的 Python 脚本。这可以通过右键单击要调试的脚本轻松完成 --> 点击Debug sample_script_to_debug.py
Hope it was helpfull and let me know if there are any mistakes in this approach.
希望它有帮助,如果这种方法有任何错误,请告诉我。
Cheers,
干杯,
Guy.
伙计。
回答by Robin Thoni
I solved this problem by copying /usr/bin/python3
in my home, then setting cap_net_bind_service
capability:
我通过/usr/bin/python3
在家中复制解决了这个问题,然后设置了cap_net_bind_service
功能:
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.
然后~/python35-setcap
在pycharm中用作python解释器。
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.
这样,您可以绑定较低的端口,但不是任何 python 3 程序都可以做到,而且 pycharm 仍然可以杀死您的脚本。如果您想要更高的安全性,您还可以将执行权限限制为自己。
回答by noun
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.
我在尝试在 Raspberry Pi 上调试蓝牙相关代码时遇到了同样的问题。我想,由于您正在设备上进行远程调试,因此该设备仅供开发使用。在这种情况下,在我不起眼的选择中,您应该允许 ssh root 登录,这样您就可以将 PyCharm 配置为使用 root 用户,而无需 sudo。这就是我选择的解决方案。
The following instructions are for a Raspberry Pi, but the procedure is the same for any Linux distribution:
以下说明适用于 Raspberry Pi,但该过程适用于任何 Linux 发行版:
First of all, add your public key to the authorized_keys
:
首先,将您的公钥添加到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:
拥有控制台后,将密钥复制到root
目录中:
sudo mkdir /root/.ssh
sudo cp authorized_keys /root/.ssh/
Finally edit sshd_config
adding PermitRootLogin without-password
:
最后编辑sshd_config
添加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:
现在您可以以 root 身份在 Raspberry Pi 中 ssh:
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.
使用root
替代或pi
用户,使您能够按照 BlueZ 的要求,以 root 权限运行您的代码,甚至是远程运行。
回答by phapli
回答by Yi Zhao
I follow the instructions hereand success. But there is a problem that the PYTHONPATH is not valid when you use sudo. So when you edit with
我按照此处的说明操作并成功。但是有一个问题,就是使用 sudo 时 PYTHONPATH 无效。所以当你编辑时
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 "$@"
回答by HyperionX
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 consoleoption.
在“执行”下,选中输出控制台选项中的模拟终端。
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.
这将允许您调试 Python 脚本,同时维护当前用户并在需要时为脚本提供提升的 sudo 权限。如果您跨多个项目工作,它还可以更轻松地维护不同的虚拟环境。