It would probably be better to use sudo for the user running the Python program. You can specify specific commands and arguments that can be run from sudo without a password. Here is an example:
There are many approaches, but I prefer one that assigns command sets to groups. Suppose we want to create a group that allows people to run tcpdump as root . So let's call this group tcpdumpers .
You must first create a group called tcpdumpers . Then change /etc/sudoers :
# Command alias for tcpdump Cmnd_Alias TCPDUMP = /usr/sbin/tcpdump
Now, any user added to the tcpdumpers group will be able to run tcpdump as follows:
% sudo tcpdump
From there, you can easily run this command as subprocess .
This eliminates the need to hard-code the root password in the program code and provides detailed control over who can perform any action with root privileges on your system.
source share