Skip to content
Advertisement

Root autentication on a Python program

I have the root password saved in a variable. How I can give the root authorization at my program?

This can be an example:

password = "mypassword"
a = BeRoot(password)
if a == True:
    print("now you have become root user")

Advertisement

Answer

It is impossible to do it in the way you try. Your program is already started under some non-root user and you cannot change it inside this program. You can spawn other process which will be run under the name of root using sudo.

import os
os.popen("sudo -S somecommand", 'w').write("mypassword")

See also this question (from where the code snipped is taken).

Advertisement