How can I check if my program is running with root permission. Does Pathlib offer the possibility to query the USER-ID under Linux
You can access username with standard library package getpass:
import getpass
print(getpass.getuser())
When code is executed with sudo or as root, the user is the string root.
Use os.geteuid() to check if the effective UID is zero.
import os
if os.geteuid() == 0:
print("Running as root")
getpass.getuser() is not a reliable solution for a few reasons:
root the check will fail.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With