I want to run a program inside a container with a specific user. By default, when using lxc-attach, the user is root, but I don't want to execute the program as root.

Command I want to execute:

lxc-attach -n container -- python3 some_program.py

When attached to the container, I want the user to be uid=1000 not uid=0 (root)

I know it's possible with lxc-execute, with lxc.init_uid and lxc.init_gid [src: LXC.CONTAINER.CONF(5)], but with lxc-execute I don't have network connection (because the container is not running?).


You have to use su to change the user:

$ sudo lxc-attach -n test -- su ubuntu -c 'whoami'

Your command would look like this (if you don't know the username):

lxc-attach -n container -- 'su $(getent passwd 1000| cut -f1 -d:) -c "python3 some_program.py"'
  • The command took the user from the host, I fixed the quotes. – ctx Mar 20 '17 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.