I"m trying to determine if the user is root or not then prompt to become root user;

if os.geteuid() != 0:              # If not root user...
   os.system("gksudo crack.py")    # Open root password window
else:
   # Do Nothing

if os.geteuid() == 0:              # If you are root user...     
   os.system("You are now root!")  
   # Insert code requiring root privileges here
else:                                        
   print("You are still not root.  Did you enter the correct password?")

The thing is,

print os.getuid()

always returns 1000? What value should a root user be and what value should a non root user be?

The answer depends on what OS you are running, but hopefully all OS's won't let you determine anything for any other user, especially root, except if you are already root. So the only way you can determine if there is a different user is if you run the program as root (or su, sudo, etc.).

In Ubuntu, root is indeed uid 0. Did you try executing your script as root? or did you leave it to python to elevate your privileges? You can just prompt the user to run your script as root:

user = os.getuid()
if user != 0:
    print "This program requires root privileges.  Run as root using 'sudo'."
    sys.exit()

If you really want python to do it for you, you can try 'gksudo ls'

user = os.getuid()
if user != 0:
    os.system('gksudo ls')
    os.system('clear')
os.system('sudo command_requiring_root')

This second example will prompt for a password upon execution of the ls command, but subsequent commands requiring sudo will not. Hope this helps.

Actually, that's one step too many. Forget the 'gksudo ls' and change 'clear' to 'gksudo clear'

Thanks, I will try that out!

I want the script to check for elevated privileges as the software I am designing may run for some time and Ubuntu drops elevated privileges after a pre-determined about of time thus there is a need to check if the user is root otherwise the software wont work as intended.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.