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
515 views

1 answer to this question.

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
• 67,120 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
• 67,120 points
54 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
• 67,120 points
55 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
• 67,120 points
44 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
• 67,120 points
93 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 128 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
• 67,120 points
45 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
• 67,120 points
34 views