📌 4 de Julho, 2024

LXD / Incus: Enter Container As User

Informática · ISP · Linux

📌 4 de Julho, 2024

LXD / Incus: Enter Container As User

Informática · ISP · Linux

In most situations, you would be entering your containers as root, and that’s perfectly reasonable. After all, an unprivileged container ensures that the root inside it will only have root privileges within that container and nowhere else. Sometimes, however, it might be useful to enter a container with a specific user.

Here’s how you do it:

$ lxc exec <container-name> --user <user-guid> /bin/bash 
$ lxc exec <container-name> -- su --shell /bin/bash --login <user-name>

Both are valid options to get to bash inside your container however on the first one you’ll need to know the GUID of the user.

For your convenience you can alias the last one in your host’s ~/.bashrc with something like:

lxcbash() { lxc exec "$1" -- sudo --login --user "$2"; }

Then you can enter containers with:

$ lxcbash <container-name> <user-name>

Easy isn’t it?