|
| 1 | +# LXC |
| 2 | + |
| 3 | +The [Linux Containers project](https://linuxcontainers.org/) includes three |
| 4 | +subprojects: [LXC](https://linuxcontainers.org/lxc/introduction/), |
| 5 | +[LXD](https://linuxcontainers.org/lxd/introduction/) and |
| 6 | +[LXCFS](https://linuxcontainers.org/lxcfs/introduction/). The project also |
| 7 | +included the CGManager project, which has been deprecated in favor of the CGroup |
| 8 | +namespace in recent kernels. |
| 9 | + |
| 10 | +## Configuring LXC |
| 11 | + |
| 12 | +Install the `lxc` package. |
| 13 | + |
| 14 | +Creating and running privileged containers as `root` does not require any |
| 15 | +configuration; simply use the various `lxc-*` commands, such as |
| 16 | +[lxc-create(1)](https://man.voidlinux.org/lxc-create.1), |
| 17 | +[lxc-start(1)](https://man.voidlinux.org/lxc-start.1), |
| 18 | +[lxc-attach(1)](https://man.voidlinux.org/lxc-attach.1), etc. |
| 19 | + |
| 20 | +### Creating unprivileged containers |
| 21 | + |
| 22 | +User IDs (UIDs) and group IDs (GIDs) normally range from 0 to 65535. |
| 23 | +Unprivileged containers enhance security by mapping UID and GID ranges inside |
| 24 | +each container to ranges not in use by the host system. The unused host ranges |
| 25 | +must be *subordinated* to the user who will be running the unprivileged |
| 26 | +containers. |
| 27 | + |
| 28 | +Subordinate UIDs and GIDs are assigned in the |
| 29 | +[subuid(5)](https://man.voidlinux.org/subuid.5) and |
| 30 | +[subgid(5)](https://man.voidlinux.org/subgid.5) files, respectively. |
| 31 | + |
| 32 | +To create unprivileged containers, first edit `/etc/subuid` and `/etc/subgid` to |
| 33 | +delegate ranges. For example: |
| 34 | + |
| 35 | +``` |
| 36 | +root:1000000:65536 |
| 37 | +user:2000000:65536 |
| 38 | +``` |
| 39 | + |
| 40 | +In each colon-delimited entry: |
| 41 | + |
| 42 | +- the first field is the user to which a subordinate range will be assigned; |
| 43 | +- the second field is the smallest numeric ID defining a subordinate range; and |
| 44 | +- the third field is the number of consecutive IDs in the range. |
| 45 | + |
| 46 | +The [usermod(8)](https://man.voidlinux.org/usermod.8) program may also be used |
| 47 | +to manipulate suborinated IDs. |
| 48 | + |
| 49 | +Generally, the number of consecutive IDs should be an integer multiple of 65536; |
| 50 | +the starting value is not important, except to ensure that the various ranges |
| 51 | +defined in the file do not overlap. In this example, `root` controls UIDs (or, |
| 52 | +from `subgid`, GIDs) ranging from 1000000 to 1065535, inclusive; `user` controls |
| 53 | +IDs ranging from 2000000 to 2065535. |
| 54 | + |
| 55 | +Before creating a container, the user owning the container will need an |
| 56 | +[lxc.conf(5)](https://man.voidlinux.org/lxc.conf.5) file specifying the subuid |
| 57 | +and subgid range to use. For root-owned containers, this file resides at |
| 58 | +`/etc/lxc/default.conf`; for unprivileged users, the file resides at |
| 59 | +`~/.config/lxc/default.conf`. Mappings are described in lines of the form |
| 60 | + |
| 61 | +``` |
| 62 | +lxc.idmap = u 0 1000000 65536 |
| 63 | +lxc.idmap = g 0 1000000 65536 |
| 64 | +``` |
| 65 | + |
| 66 | +The isolated `u` character indicates a UID mapping, while the isolated `g` |
| 67 | +indicates a GID mapping. The first numeric value should generally always be 0; |
| 68 | +this indicates the start of the UID or GID range *as seen from within the |
| 69 | +container*. The second numeric value is the start of the corresponding range *as |
| 70 | +seen from outside the container*, and may be an arbitrary value within the range |
| 71 | +delegated in `/etc/subuid` or `/etc/subgid`. The final value is the number of |
| 72 | +consecutive IDs to map. |
| 73 | + |
| 74 | +Note that, although the external range start is arbitrary, care must be taken to |
| 75 | +ensure that the end of the range implied by the start and number does not extend |
| 76 | +beyond the range of IDs delegated to the user. |
| 77 | + |
| 78 | +If configuring a non-root user, edit `/etc/lxc/lxc-usernet` as root to specify a |
| 79 | +network device quota. For example, to allow the user named `user` to create up |
| 80 | +to 10 `veth` devices connected to the `lxcbr0` bridge: |
| 81 | + |
| 82 | +``` |
| 83 | +user veth lxcbr0 10 |
| 84 | +``` |
| 85 | + |
| 86 | +The user can now create and use unprivileged containers with the `lxc-*` |
| 87 | +utilities. To create a simple Void container named `mycontainer`, use a command |
| 88 | +similar to: |
| 89 | + |
| 90 | +``` |
| 91 | +lxc-create -n mycontainer -t download -- \ |
| 92 | + --dist voidlinux --release current --arch x86_64 |
| 93 | +``` |
| 94 | + |
| 95 | +You may substitute another architecture for `x86_64`, and you may specify a |
| 96 | +`musl` image by adding `--variant musl` to the end of the command. See the [LXC |
| 97 | +Image Server](http://images.linuxcontainers.org) for a list of available |
| 98 | +containers. |
| 99 | + |
| 100 | +By default, configurations and mountpoints for system containers are stored in |
| 101 | +`/var/lib/lxc`, while configurations for user containers and mountpoints are |
| 102 | +stored in `~/.local/share/lxc`. Both of these values can be modified by setting |
| 103 | +`lxc.lxcpath` in the |
| 104 | +[lxc.system.conf(5)](https://man.voidlinux.org/lxc.system.conf.5) file. The |
| 105 | +superuser may launch unprivileged containers in the system `lxc.lxcpath` defined |
| 106 | +in `/etc/lxc/lxc.conf`; regular users may launch unprivileged containers in the |
| 107 | +personal `lxc.lxcpath` defined in `~/.config/lxc/lxc.conf`. |
| 108 | + |
| 109 | +All containers will share the same subordinate UID and GID maps by default. This |
| 110 | +is permissible, but it means that an attacker who gains elevated access within |
| 111 | +one container, and can somehow break out of that container, will have similar |
| 112 | +access to other containers. To isolate containers from each other, alter the |
| 113 | +`lxc.idmap` ranges in `default.conf` to point to a unique range *before* you |
| 114 | +create each container. Trying to fix permissions on a container created with the |
| 115 | +wrong map is possible, but inconvenient. |
| 116 | + |
| 117 | +## LXD |
| 118 | + |
| 119 | +LXD provides an alternative interface to LXC's `lxc-*` utilities. However, it |
| 120 | +does not require the configuration described in [the previous section](#lxc). |
| 121 | + |
| 122 | +Install the `lxd` package, and [enable](./services/index.md#enabling-services) |
| 123 | +the `lxd` service. |
| 124 | + |
| 125 | +LXD users must belong to the `lxd` group. |
| 126 | + |
| 127 | +Use the `lxc` command to manage instances, as described |
| 128 | +[here](https://linuxcontainers.org/lxd/getting-started-cli/#lxd-client). |
0 commit comments