Skip to content

Instantly share code, notes, and snippets.

@zmarffy
Created November 28, 2020 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zmarffy/0fbac1df3f2adaa4819da799a2a0508a to your computer and use it in GitHub Desktop.
Save zmarffy/0fbac1df3f2adaa4819da799a2a0508a to your computer and use it in GitHub Desktop.
A Python function that sets the user of itself if run with `sudo` to the user who used `sudo`
import os
import pwd
def demote_to_caller():
if os.geteuid() == 0:
d = pwd.getpwnam(os.environ["SUDO_USER"])
os.setgid(d.pw_gid)
os.setuid(d.pw_uid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment