python run command as normal user in a root script

2024/11/15 1:34:28

I have a python script that is launched as root, I can't change it.

I would like to know if it's possible to exectute certain lines of this script (or all the script) as normal user (I don't need to be root to run this).

The reason is, I use notifications, and python-notify don't work in all machines in root (looks like this bug)

So ,do you know if it's possible to change it, with a subprocess, or other?

Thanks

Answer

I would like to know if it's possible to exectute certain lines of this script (or all the script) as normal user

Yes, it's possible—and a good idea.

Python's os module has a group of functions to set the real, effective, and saved user and group id, starting with setegid. What exactly each of these does is up to your platform, as far as Python is concerned; it's just calling the C functions of the same names.

But POSIX defines what those functions do. See setuid and seteuid for details, but the short version is:

  • If you want to switch to a normal user and then switch back, use either seteuid or setreuid, to set just effective, or real and effective, but not saved UID. Then use the same function again to set them back to root.
  • If you want to run the whole script as a normal user and make sure you can't get root back, use setresuid instead, to set all three.

If you're using Python 3.1 and earlier, you don't have all of these functions. You can still use seteuid to switch effective ID back and forth, but setuid will… well, it depends on your platform, but I think most modern platforms will change saved as well as real, meaning you can't get root back. If you read the linked POSIX doc, there are a bunch of caveats and complexities in the POSIX documentation. If you only care about one platform, you probably want to read your local manpages instead, rather than reading about all of the cases and then trying to figure out which one covers your platform.

So ,do you know if it's possible to change it, with a subprocess, or other?

That isn't necessary (at least on a conforming POSIX system), but it can make things easier or safer. You can use subprocess, multiprocessing, os.fork, or any other mechanism to launch a child process, which immediately uses setuid to drop privileges—or even setresuid to give up the ability to ever restore its privilege. When that child process is done with its task, it just exits.

https://en.xdnf.cn/q/71419.html

Related Q&A

Compare values of two arrays in python

How can i check if item in b is in a and the found match item in a should not be use in the next matching? Currently this code will match both 2 in b.a = [3,2,5,4] b = [2,4,2]for i in b:if i in a:prin…

How to count the number of digits in numbers in different bases?

Im working with numbers in different bases (base-10, base-8, base-16, etc). Im trying to count the number of characters in each number. ExampleNumber: ABCDEF Number of digits: 6I know about the method …

Pandas KeyError using pivot

Im new to Python and I would like to use Python to replicate a common excel task. If such a question has already been answered, please let me know. Ive been unable to find it. I have the following p…

Not found: Container localhost does not exist when I load model with tensorflow and flask

I am a newbie research Deeplearning. I load a saved model with tensorflow and made a API with flask but I get error Container localhost does not exist. when I predict, please help me fix it. Thank you.…

Python: simplifying nested FOR loop?

I am wondering if there is a way to simplify the nested loop below. The difficulty is that the iterator for each loop depends on things from the previous loops. Here is the code:# Find the number of co…

NLTK Data installation issues

I am trying to install NLTK Data on Mac OSX 10.9 . The download directory to be set, as mentioned in NLTK 3.0 documentation, is /usr/share/nltk_data for central installation. But for this path, I get …

Why does the simplest requests_mock example fail with pytest?

I have a peculiar problem with requests_mock. I want to use it with pytest to test my API wrapper library.Ive tried to use the first example in the requests_mock docs, except I put it in a test_mock()-…

Error installing PyCurl

I tried installing pycurl via pip. it didnt work and instead it gives me this error.running installrunning buildrunning build_pyrunning build_extbuilding pycurl extensiongcc-4.2 -fno-strict-aliasing -f…

Serializing objects containing django querysets

Django provides tools to serialize querysets (django.core.serializers), but what about serializing querysets living inside other objects (like dictionaries)?I want to serialize the following dictionar…

How do I list my scheduled queries via the Python google client API?

I have set up my service account and I can run queries on bigQuery using client.query(). I could just write all my scheduled queries into this new client.query() format but I already have many schedule…