2

I have created pyenv environment (/home/username/python_env/) with some additional libraries (like PyYaml, Pillow etc). Everything is working fine until I need to change /etc/resolv.conf from inside of my tool. Additional part of code which I need to run is os.chroot and I can't do it without root permissions.

All operations are made for current user (which can change) so if I use sudo to execute my tool like sudo python src/tool-name/main.py then everything will be executed for root user instead for username.

Using sudo will affects:

  • error while starting my tool because sudo will force to use system python known to root user (instead of sources earlier venv)
  • after I source virtual environment as a root and execute my tool all files which I will create will have root permissions

Is there a way to request sudo just for small part of code and after that return to user permissions while my script is running?

Joshua Shew
1,0611 gold badge9 silver badges32 bronze badges
asked Jul 28, 2023 at 8:43
3
  • Up to my knowledge no. you cann call a subprocess with sudo, that just performs part of the work and then execute the rest in your script. Not very elegant though Or you could have two processes that communicate with each other one with sodu the other without, but also not so easy and if you don't pay attention a potential security risk Commented Jul 28, 2023 at 12:13
  • Are you sure you need to run os.chroot? That does not change your user to root, it changes the root for absolute paths. In my experience most people who say they need os.chroot do so because they misunderstand what it does. Commented Jul 28, 2023 at 20:08
  • I'm changing path from current folder to jail in created partition so... yes :( Commented Jul 31, 2023 at 12:26

1 Answer 1

0

Running Python code with root permissions inside a virtual environment can be tricky, as you need to maintain the environment variables of the virtual environment while executing the code with elevated privileges. Here's a possible approach to achieve this:

  1. Activate the virtual environment in your user context.
  2. Use sudo -E to run the Python code with elevated privileges while preserving the environment variables.

Step 1: Activate the virtual environment (as a regular user):

$ source /home/username/python_env/bin/activate

Step 2: Run your Python code with sudo -E:

$ sudo -E python /path/to/your/code.py

The -E option tells sudo to preserve the user's environment, including the virtual environment variables set during activation.

With this approach, your Python code should be executed with root permissions while still using the virtual environment's Python interpreter and libraries.

answered Jul 28, 2023 at 15:12
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.