18

I've recently began using ConfigParser() for my python scripts to add some functionality to them for config files. I know how to use it but I have a problem. My script needs to run as the root user, using sudo. The config files are in ~/.config/scriptconfig/ but when you run a script as sudo it temporarily changes users to root, so it doesn't find the config files. What I want to do is get the config file of the effective user so it grabs /home/myuser/.config/scriptconfig/config.cfg instead of /root/.config/scriptconfig/config.cfg, which doesn't exist.

I need the script to be able to run on different machines, not just mine. So I need to get the home directory of the effective user

Here is an example of the code I'm trying to use:


import os, ConfigParser
config = ConfigParser.RawConfigParser()
homepath = os.path.expanduser("~/")
configpath = homepath + ".config/scriptconfig/config.cfg"
config.read(configpath)
get = config.get('Config', 'Example')
print get
It should print the value of example from the config file but when ran as sudo, the path is /home/root so it doesn't find the config file.
Jmariz
  • 235
  • 1
  • 3
  • 8
  • Are you using `os.getuid()` to get the real user (instead of the effective user) and work out the home directory from that? Or are you simply using `os.path.expanduser()`? – S.Lott Apr 19 '11 at 19:24
  • I'm using `os.path.expanduser("~/")`. I use `os.getuid()` because the script must be run as root. So it checks to make sure you're root and if you're not it spits an error message and exits. – Jmariz Apr 19 '11 at 19:29
  • @JMariz: Since expand user doesn't work, why are you using it? – S.Lott Apr 19 '11 at 19:52
  • Well lets say I'm the user `myuser2` on a different machine. I want it to be able to run on other peoples computers to. So it needs to find ~/ directory. So it needs to be able to automatically detect the users home directory. But as sudo it can't do that since the user is changed to root. – Jmariz Apr 19 '11 at 19:54
  • Not really. The concept is the same. I just need to find the home path for the effective user, which is what the original question asks. – Jmariz Apr 19 '11 at 20:22
  • Its still the same concept. I need the script to find the effective users home path so it can be run on other peoples machines. – Jmariz Apr 19 '11 at 20:24
  • I don't see why it even needs updating. I just need to get the home directory for the effective user running the script. – Jmariz Apr 19 '11 at 20:29
  • @JMariz: I apologize. Some of us aren't able to work through all the comments and come up with one set of requirements. You can refuse to update the question. In which case, some of us will simply ignore it because it requires too much work to integrated the thread of comments. – S.Lott Apr 19 '11 at 20:32
  • @JMariz: Thank you. Now. Can you also post the code you're using that doesn't work. I asked why you're using `expanduser()`, since it doesn't work, and I still can't understand what code you're using. I suspect this is simple, but I can't follow all the back-and-forth very well. Please post the code which doesn't work. – S.Lott Apr 19 '11 at 20:36
  • Edited. I put the code and what is supposed to happen. – Jmariz Apr 19 '11 at 20:42

3 Answers3

30

If you run your script with sudo (sudo myscript.py) then the environment variable $USER will be root and the environment variable $SUDO_USER will be the name of the user who executed the command sudo myscript.py. This following is simply a clarification of the previous post by Cédric Julien. Consider the following scenario:

A linux user bob is logged into the system and possesses sudo privileges. He writes the following python script named myscript.py:

    #!/usr/bin/python
    import os
    print os.getenv("USER")
    print os.getenv("SUDO_USER")

He then makes the script executable with chmod +x myscript.py and then executes his script with sudo privileges with the command:

sudo ./myscript.py

The output of that program will be (using python 2.x.x):

    root
    bob

If bob runs the program sans sudo privileges with

./myscript.py

he will get the following output:

    bob
    None
TheInderpreter
  • 441
  • 4
  • 5
7

if you want to get the user that was logged in before launching the sudo command, it is stored in the SUDO_USER environment variable.

import os
sudo_username = os.getenv("SUDO_USER")
home_dir = "/home/" + sudo_username

You also have the SUDO_UID and SUDO_GID for the user id and group id.

Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
  • 1
    macOS: `os.path.expanduser('~'+os.getenv("SUDO_USER"))` – Liviu Oct 24 '18 at 15:47
  • Often getting the username of the SUDO_USER is not enough and you need the home directory on both Mac and Linux as @Liviu mentions above. In case sudo is not used, you'd want the home directory of the User itself. The solution would be as in this post https://stackoverflow.com/a/42750492/796858 – AdamE Sep 09 '21 at 01:00
5

This doesn't work.

homepath = os.path.expanduser("~/")

So don't use it.

You want this.

username= os.environ["LOGNAME"]
homepath = os.path.expanduser("~"+username+"/")

http://docs.python.org/library/os.html#os.getlogin

Or perhaps this.

 username= pwd.getpwuid(os.getuid())[0]
 homepath = os.path.expanduser("~"+username+"/")
S.Lott
  • 384,516
  • 81
  • 508
  • 779