0

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")
Remi Guan
  • 21,506
  • 17
  • 64
  • 87

3 Answers3

1

You shouldn't.
A user with sufficient rights should run the program if it need's more power.
Hard-coding the root password directly in your file would be fatal, as everyone could read it. Sure you could restrict reading permissions to admins, but they could run the program with enough right's anyway.

Kimmax
  • 1,658
  • 21
  • 34
0

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).

Community
  • 1
  • 1
Ilya V. Schurov
  • 7,687
  • 2
  • 40
  • 78
0

Try configure your python user to do sudo without password

Add in /etc/sudoers

gandalf ALL=(ALL) NOPASSWD: ALL

An replace gandalf with the appropriate username

In python

import os
os.system("sudo echo 'I am a root'")

sudo before any command that you want to run as root