How to run python script in PyCharm with sudo privileges

+1 vote

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, 2019 in Cyber Security & Ethical Hacking by Jishan
13,120 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.

Hope this helps!!

If you need to know more about Python, join Python certification course today.

Thanks!

answered Feb 6, 2019 by Omkar
• 69,150 points

Related Questions In Cyber Security & Ethical Hacking

0 votes
1 answer
0 votes
1 answer
+1 vote
0 answers

How can I encryption/decryption in Rijndael using python

I found this https://github.com/moeenz/rijndael ,but does not ...READ MORE

Sep 28, 2019 in Cyber Security & Ethical Hacking by Ahmed
• 310 points
2,066 views
0 votes
1 answer

SIOCSIFFLAGS: Operation not permitted

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

answered Feb 6, 2019 in Cyber Security & Ethical Hacking by Omkar
• 69,150 points
1,116 views
0 votes
2 answers
+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, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,265 views
0 votes
0 answers
+1 vote
1 answer

Not able to use nmap in python.

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

answered Jan 28, 2019 in Cyber Security & Ethical Hacking by Omkar
• 69,150 points
1,121 views
+1 vote
1 answer

How to find IP address of nodes in my network?

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

answered Feb 9, 2019 in Cyber Security & Ethical Hacking by Omkar
• 69,150 points
636 views