[lxc-users] short question: can a user know he is inside a container ?

Alvaro Miranda Aguilera kikitux at gmail.com
Sun Dec 29 05:36:59 UTC 2013


Hello,

For non root user, I check for /dev/lxc/console

[grid at ol6 ~]$ [ -c /dev/lxc/console ] && echo "inside lxc container"
inside lxc container

Alvaro.




On Thu, Dec 26, 2013 at 9:52 AM, Stéphane Graber <stgraber at ubuntu.com>wrote:

> On Wed, Dec 25, 2013 at 10:44:21PM +0200, David Shwatrz wrote:
> > Hello, lxc users,
> >
> > Is there a way for a user who is inside some shell in a container
> > to know that he is inside a container? I am not talking about setting
> > specific different host names to containers, but the question is: is
> > there something general/inherent to containers,  which indicates that
> > a user is  inside a container ?
> >
> > Best,
> > DavidS
>
> Yes, there are a few ways to do so.
>
> If on Ubuntu, the easiest way is to call "running-in-container" which
> will return 0 if you are and 1 if you're not.
> On Ubuntu you can then read /run/container_type to know what technology
> is used (we detect lxc, libvirt-lxc, openvz and vserver).
>
> If not on Ubuntu, you can basically use the same trick we use on Ubuntu
> to detect containers which is roughly:
>
> cat /proc/1/environ | tr '\0' '\n' | grep ^container
>
> Which wiill return "container=lxc" in a LXC container. For other
> container types we need a few more tricks, here are the ones we
> currently use:
>
>     # Detect old-style libvirt
>     if [ -z "$container" ]; then
>         [ -n "$LIBVIRT_LXC_UUID" ] && container=lxc-libvirt
>     fi
>
>     # Detect OpenVZ containers
>     if [ -z "$container" ]; then
>         [ -d /proc/vz ] && [ ! -d /proc/bc ] && container=openvz
>     fi
>
>     # Detect vserver
>     if [ -z "$container" ]; then
>         VXID="$(cat /proc/self/status | grep ^VxID | cut -f2)" || true
>         [ "${VXID:-0}" -gt 1 ] && container=vserver
>     fi
>
>
> Between the container env variable and those, you should be able to
> detect pretty much all kind of containers (unless they are tweaked to
> hide those information from you).
>
> --
> Stéphane Graber
> Ubuntu developer
> http://www.ubuntu.com
>
> _______________________________________________
> lxc-users mailing list
> lxc-users at lists.linuxcontainers.org
> http://lists.linuxcontainers.org/listinfo/lxc-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linuxcontainers.org/pipermail/lxc-users/attachments/20131229/7858ab7c/attachment.html>


More information about the lxc-users mailing list