Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Python script as root (with sudo) - what is the username of the effective user?

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.
like image 809
Jmariz Avatar asked Apr 19 '11 19:04

Jmariz


People also ask

How do I run a python script in sudo?

From the terminal instead of doing python yourProgram.py , do sudo python yourProgram.py . It will ask for your password so type it and it should run.

What is sudo () in python?

python-sudoModular Python to execute any subprocess commands as another user (not necessarily superuser/root)

How do I use root in python?

Python math function | sqrt() sqrt() function is an inbuilt function in Python programming language that returns the square root of any number. Syntax: math.sqrt(x) Parameter: x is any number such that x>=0 Returns: It returns the square root of the number passed in the parameter.

Does sudo work python?

Starting with version 1.9, sudo plugins can be written in python.


2 Answers

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
like image 104
TheInderpreter Avatar answered Sep 18 '22 03:09

TheInderpreter


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.

like image 45
Cédric Julien Avatar answered Sep 21 '22 03:09

Cédric Julien