lxc-attach
start a process inside a running container.
- Provided by: lxc (Version: 1.0.10-0ubuntu1.1)
- Report a bug
start a process inside a running container.
lxc-attach
{-n name} [-a arch] [-e] [-s namespaces] [-R]
[--keep-env] [--clear-env] [-- command]
lxc-attach runs the specified command inside the container specified by name. The container has to be running already.
If no command is specified, the current default shell of the user running lxc-attach will be looked up inside the container and executed. This will fail if no such user exists inside the container or the container does not have a working nsswitch mechanism.
You may specify privileges, in case you do not want to elevate all of them, as a pipe-separated list, e.g. CGROUP|LSM. Allowed values are CGROUP, CAP and LSM representing cgroup, capabilities and restriction privileges respectively.
Warning: This may leak privileges into the container if the command starts subprocesses that remain active after the main process that was attached is terminated. The (re-)starting of daemons inside the container is problematic, especially if the daemon starts a lot of subprocesses such as cron or sshd. Use with great care.
Important: This option implies -e.
Please see the Notes section for more details.
This option will be ignored if one tries to attach to the mount namespace anyway.
These options are common to most of lxc commands.
Note that this option is setting the priority of the events log in the alternate log file. It do not have effect on the ERROR events log on stderr.
To spawn a new shell running inside an existing container, use
lxc-attach -n container
To restart the cron service of a running Debian container, use
lxc-attach -n container -- /etc/init.d/cron restart
To deactivate the network link eth1 of a running container that does not have the NET_ADMIN capability, use either the -e option to use increased capabilities, assuming the ip tool is installed:
lxc-attach -n container -e -- /sbin/ip link delete eth1
Or, alternatively, use the -s to use the tools installed on the host outside the container:
lxc-attach -n container -s NETWORK -- /sbin/ip link delete eth1
Attaching completely (including the pid and mount namespaces) to a container requires a kernel of version 3.8 or higher, or a patched kernel, please see the lxc website for details. lxc-attach will fail in that case if used with an unpatched kernel of version 3.7 and prior.
Nevertheless, it will succeed on an unpatched kernel of version 3.0 or higher if the -s option is used to restrict the namespaces that the process is to be attached to to one or more of NETWORK, IPC and UTSNAME.
Attaching to user namespaces is supported by kernel 3.8 or higher with enabling user namespace.
The Linux /proc and /sys filesystems contain information about some quantities that are affected by namespaces, such as the directories named after process ids in /proc or the network interface information in /sys/class/net. The namespace of the process mounting the pseudo-filesystems determines what information is shown, not the namespace of the process accessing /proc or /sys.
If one uses the -s option to only attach to the pid namespace of a container, but not its mount namespace (which will contain the /proc of the container and not the host), the contents of /proc will reflect that of the host and not the container. Analogously, the same issue occurs when reading the contents of /sys/class/net and attaching to just the network namespace.
To work around this problem, the -R flag provides the option to remount /proc and /sys in order for them to reflect the network/pid namespace context of the attached process. In order not to interfere with the host's actual filesystem, the mount namespace will be unshared (like lxc-unshare does) before this is done, essentially giving the process a new mount namespace, which is identical to the hosts's mount namespace except for the /proc and /sys filesystems.
The -e and -s options should be used with care, as it may break the isolation of the containers if used improperly.
lxc(7), lxc-create(1), lxc-destroy(1), lxc-start(1), lxc-stop(1), lxc-execute(1), lxc-console(1), lxc-monitor(1), lxc-wait(1), lxc-cgroup(1), lxc-ls(1), lxc-info(1), lxc-freeze(1), lxc-unfreeze(1), lxc-attach(1), lxc.conf(5)
Daniel Lezcano <daniel.lezcano@free.fr>