Linux – How to open the console of a container as a non-root user in LXD 2.0

containerslinuxlxc

I'm new to container things and fiddling with LXD-2.0 on an ubuntu-16.04 host with a Alpine3.4 container. Questions:

  1. How to open the console of the container with a non-root user?
lxc exec mycontainer bash

This land me directly in the container as root. However I want to be a non-root user in the first place when I open the console.

  1. Is it a best practice to run applications as a non-root user inside the container, just as we normally do in a non-container environment? (even the container is launched in "unprivileged" mode).

Best Answer

  1. The exec will execute the command as root. But of course since as root you can "login" as any user via the su command, you could exec a su:

lxc exec mycontainer su - mortaluser

Another way could be to install sshd and use the ssh command to log in on the container, as you would any other server.

  1. It's indeed good practice to treat the container as a normal server and only elevate your rights when needed via for instance 'sudo'.
Related Topic