How to run python script in PyCharm with sudo privileges?

0 votes

I have written a macchanger script in PyCharm and I am using sudo command. When I run the script, the script pauses, and the Ubuntu terminal asks me for sudo password. I have 3 lines with sudo command and I will have to enter the password every time. Is there a way I can run the script with sudo password without the system asking me for password?

This is my code:

import subprocess

subprocess.call(["sudo","ifconfig","ens33","down"])
subprocess.call(["sudo","ifconfig","ens33","hw","ether","00:11:22:33:44:55"])
subprocess.call(["sudo","ifconfig","ens33","up"])
Feb 6 in Cyber Security & Ethical Hacking by Jishan
245 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

When you run a command with sudo, by default, the system will ask for a password. To avoid this, you will have to change the python interpreter used by PyCharm from default to a custom interpreter. Don't worry, you don't have to build a new interpreter, you will just have to create an intermediate. The solution to solve this is as follows:

First, open a terminal and run this command:

$ sudo visudo -f /etc/sudoers.d/python

Then enter the following line in this file (replace <username><hostname> with the username and hostname of your system respectively):

<username> <hostname> = (root) NOPASSWD: <path/to/python>

Replace <path/to/python> with the path where Python is installed (usually /usr/bin/python) and save this file.

Now you will have to create a sudo shell script. Run the following command in the terminal:

$ nano python-sudo.sh

and enter the following lines in it:

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

Save and exit and change the permission of this file:

$ chmod +x python-sudo.sh

Now go to PyCharm, go to File > Settings > Project Interpreter. Click on the settings icon and click on the green plus icon to add interpreter. Browse to the directory where you have saved the python-sudo.sh and select it. Save changes and exit. Now you won't be asked for passwords.

answered Feb 6 by Omkar
• 65,850 points

Related Questions In Cyber Security & Ethical Hacking

0 votes
1 answer

Not able to run nmap scan on router.

Might not be the actual router address. ...READ MORE

answered Jan 31 in Cyber Security & Ethical Hacking by Omkar
• 65,850 points
34 views
0 votes
1 answer

How to find MAC address using IP address?

To find the MAC address, run this ...READ MORE

answered Feb 9 in Cyber Security & Ethical Hacking by Omkar
• 65,850 points
43 views
0 votes
1 answer

How to attack RSA algorithm?

There are different approaches used to attack ...READ MORE

answered Feb 12 in Cyber Security & Ethical Hacking by Omkar
• 65,850 points
29 views
0 votes
1 answer
0 votes
1 answer

SIOCSIFFLAGS: Operation not permitted

You need sudo permissions to make changes to ...READ MORE

answered Feb 6 in Cyber Security & Ethical Hacking by Omkar
• 65,850 points
59 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
25 views
0 votes
1 answer

Not able to use nmap in python.

nmap module doesn’t have PortScanner attribute. The ...READ MORE

answered Jan 28 in Cyber Security & Ethical Hacking by Omkar
• 65,850 points
28 views
0 votes
1 answer

How to find IP address of nodes in my network?

The IP address of the nodes connected ...READ MORE

answered Feb 9 in Cyber Security & Ethical Hacking by Omkar
• 65,850 points
24 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.