|
| 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 | +Both [subuid(5)](https://man.voidlinux.org/subuid.5) and |
| 23 | +[subgid(5)](https://man.voidlinux.org/subgid.5) need to have entries for the |
| 24 | +user who will be running unprivileged containers. That user will also need to |
| 25 | +have a `default.conf` file specifying use of the relevant subuids and subgids. |
| 26 | +`/etc/subuid` and `/etc/subgid` contain an entry for `root` by default, but |
| 27 | +entries for other users need to be added manually. |
| 28 | + |
| 29 | +Edit `/etc/subuid` and `/etc/subgid` as root to add the relevant entries: |
| 30 | + |
| 31 | +``` |
| 32 | +root:1000000:65536 |
| 33 | +<user>:2000000:65536 |
| 34 | +``` |
| 35 | + |
| 36 | +In each case, the entry specifies a base value, and the number of subuids |
| 37 | +available to that user starting from the base value. Thus, root will have |
| 38 | +subuids/subgids 1000000 to 1065535. |
| 39 | + |
| 40 | +If configuring a non-root user, edit `/etc/lxc/lxc-usernet` as root to specify a |
| 41 | +network device quota. For example, to allow the user named `user` to create up |
| 42 | +to 10 `veth` devices connected to the `lxcbr0` bridge: |
| 43 | + |
| 44 | +``` |
| 45 | +user veth lxcbr0 10 |
| 46 | +``` |
| 47 | + |
| 48 | +If configuring root, specify the subuid and subgid in `/etc/lxc/default.conf`: |
| 49 | + |
| 50 | +``` |
| 51 | +lxc.idmap = u 0 1000000 65536 |
| 52 | +lxc.idmap = g 0 1000000 65536 |
| 53 | +``` |
| 54 | + |
| 55 | +Otherwise, create `~/.config/lxc/default.conf`: |
| 56 | + |
| 57 | +``` |
| 58 | +$ mkdir ~/.config/lxc |
| 59 | +$ cp /etc/lxc/default.conf ~/.config/lxc/default.conf |
| 60 | +``` |
| 61 | + |
| 62 | +and edit the user's `default.conf` to include the relevant `lxc.idmap` entries. |
| 63 | + |
| 64 | +The user can now create and use unprivileged containers with the `lxc-*` |
| 65 | +utilities. |
| 66 | + |
| 67 | +## LXD |
| 68 | + |
| 69 | +LXD provides an alternative interface to LXC's `lxc-*` utilities. However, it |
| 70 | +does not require the configuration described in [the previous section](#lxc). |
| 71 | + |
| 72 | +Install the `lxd` package, and [enable](./services/index.md#enabling-services) |
| 73 | +the `lxd` service. |
| 74 | + |
| 75 | +LXD users must belong to the `lxd` group. |
| 76 | + |
| 77 | +Use the `lxc` command to manage instances, as described |
| 78 | +[here](https://linuxcontainers.org/lxd/getting-started-cli/#lxd-client). |
0 commit comments