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?).

1 Answer 1


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"'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .