Ubuntu LXC使用记录

概要

记录在Ubuntu上安装,使用lxc容器.

安装LXC

Ubuntu版本4.14.0.
安装LXC:

$sudo apt-get install lxc

检查是否支持LXC:

$sudo lxc-checkconfig
Kernel configuration not found at /proc/config.gz; searching...
Kernel configuration found at /boot/config-4.15.0-191-generic
--- Namespaces ---
Namespaces: enabled
Utsname namespace: enabled
Ipc namespace: enabled
Pid namespace: enabled
User namespace: enabled
Network namespace: enabled

--- Control groups ---
Cgroups: enabled

Cgroup v1 mount points: 
/sys/fs/cgroup/systemd
/sys/fs/cgroup/pids
/sys/fs/cgroup/cpuset
/sys/fs/cgroup/cpu,cpuacct
/sys/fs/cgroup/net_cls,net_prio
/sys/fs/cgroup/freezer
/sys/fs/cgroup/blkio
/sys/fs/cgroup/hugetlb
/sys/fs/cgroup/memory
/sys/fs/cgroup/devices
/sys/fs/cgroup/perf_event
/sys/fs/cgroup/rdma

Cgroup v2 mount points: 
/sys/fs/cgroup/unified

Cgroup v1 clone_children flag: enabled
Cgroup device: enabled
Cgroup sched: enabled
Cgroup cpu account: enabled
Cgroup memory controller: enabled
Cgroup cpuset: enabled

--- Misc ---
Veth pair device: enabled, loaded
Macvlan: enabled, not loaded
Vlan: enabled, not loaded
Bridges: enabled, loaded
Advanced netfilter: enabled, not loaded
CONFIG_NF_NAT_IPV4: enabled, loaded
CONFIG_NF_NAT_IPV6: enabled, not loaded
CONFIG_IP_NF_TARGET_MASQUERADE: enabled, loaded
CONFIG_IP6_NF_TARGET_MASQUERADE: enabled, not loaded
CONFIG_NETFILTER_XT_TARGET_CHECKSUM: enabled, loaded
CONFIG_NETFILTER_XT_MATCH_COMMENT: enabled, not loaded
FUSE (for use with lxcfs): enabled, not loaded

--- Checkpoint/Restore ---
checkpoint restore: enabled
CONFIG_FHANDLE: enabled
CONFIG_EVENTFD: enabled
CONFIG_EPOLL: enabled
CONFIG_UNIX_DIAG: enabled
CONFIG_INET_DIAG: enabled
CONFIG_PACKET_DIAG: enabled
CONFIG_NETLINK_DIAG: enabled
File capabilities: 

Note : Before booting a new kernel, you can check its configuration
usage : CONFIG=/path/to/config /usr/bin/lxc-checkconfig

输出都是enabled表示支持LXC容器.

创建LXC容器

创建容器需要使用模板(template).查看已有的模板:

$ ls /usr/share/lxc/templates/*
/usr/share/lxc/templates/lxc-busybox   /usr/share/lxc/templates/lxc-local
/usr/share/lxc/templates/lxc-download  /usr/share/lxc/templates/lxc-oci

busybox容器

可以创建基于busybox模板的容器:

$ sudo lxc-create -n test-busybox -t busybox

查看系统中的LXC容器:

$ sudo lxc-ls --fancy
NAME         STATE   AUTOSTART GROUPS IPV4 IPV6 UNPRIVILEGED 
test-busybox STOPPED 0         -      -    -    false 

系统当前有一个容器test-busybox,处于停止状态.
启动运行容器:

sudo lxc-start -n test-busybox

再查看容器状态:

$ sudo lxc-ls --fancy
NAME         STATE   AUTOSTART GROUPS IPV4 IPV6 UNPRIVILEGED 
test-busybox RUNNING 0         -      -    -    false       

状态为RUNNING表示容器已经运行.
查看容器的信息:

$ sudo lxc-info -n test-busybox
Name:           test-busybox
State:          RUNNING
PID:            6470
CPU use:        0.01 seconds
BlkIO use:      0 bytes
Memory use:     2.23 MiB
KMem use:       1.77 MiB
Link:           veth10CGKA
 TX bytes:      1.85 KiB
 RX bytes:      4.61 KiB
 Total bytes:   6.46 KiB

打开容器控制台:

$ sudo lxc-console -n test-busybox
Connected to tty 1
Type <Ctrl+a q> to exit the console, <Ctrl+a Ctrl+a> to enter Ctrl+a itself
test-busybox login: root
Password: 
Login incorrect

不知道缺省root密码?
修改root密码为test:

$ sudo chroot /var/lib/lxc/test-busybox/rootfs passwd root
passwd: no record of root in /etc/shadow, using /etc/passwd
Changing password for root
New password: 
Bad password: too short
Retype password: 
passwd: password for root changed by root

再打开控制台,输入root密码:

$ sudo lxc-console -n test-busybox
Connected to tty 1
Type <Ctrl+a q> to exit the console, <Ctrl+a Ctrl+a> to enter Ctrl+a itself
test-busybox login: root
Password: 
BusyBox v1.27.2 (Ubuntu 1:1.27.2-2ubuntu3.4) built-in shell (ash)
Enter 'help' for a list of built-in commands.
~ # ps
PID   USER     COMMAND
    1 root     init
    4 root     /bin/syslogd
    9 root     /bin/udhcpc
   11 root     init
   13 root     /bin/login -- root
   14 root     -sh
   15 root     {exe} ps
~ # 

输入Ctrl+a q退出容器.

Ubuntu容器

创建Ubuntu容器.
使用download模板下载创建容器:

sudo lxc-create -n test-ubuntu -t download
Setting up the GPG keyring
ERROR: Unable to fetch GPG key from keyserver
lxc-create: test-ubuntu: lxccontainer.c: create_run_template: 1617 Failed to create container from template
lxc-create: test-ubuntu: tools/lxc_create.c: main: 327 Failed to create container test-ubuntu

错误:
ERROR: Unable to fetch GPG key from keyserver
GPG key认证问题,可以不需要认证.修改download模板文件取消GPG认证,打开文件/usr/share/lxc/templates/lxc-download,把DOWNLOAD_VALIDATE=“true” 改为DOWNLOAD_VALIDATE=“false”.
重新创建容器:

sudo lxc-create -n test-ubuntu -t download
Downloading the image index
WARNING: Running without gpg validation!
---
DIST    RELEASE ARCH    VARIANT BUILD
---
almalinux       8       amd64   default 20231023_23:08
almalinux       8       arm64   default 20231023_23:08
almalinux       9       amd64   default 20231023_23:08
almalinux       9       arm64   default 20231023_23:08
alpine  3.15    amd64   default 20231023_13:00
alpine  3.15    arm64   default 20231023_13:01
alpine  3.16    amd64   default 20231023_13:00
alpine  3.16    arm64   default 20231023_13:00
alpine  3.17    amd64   default 20231023_13:00
alpine  3.17    arm64   default 20231023_13:01
alpine  3.18    amd64   default 20231023_13:00
alpine  3.18    arm64   default 20231023_13:00
alpine  edge    amd64   default 20231023_13:00
alpine  edge    arm64   default 20231023_13:00
alt     Sisyphus        amd64   default 20231024_01:17
alt     Sisyphus        arm64   default 20231024_01:17
alt     p10     amd64   default 20231024_01:17
alt     p10     arm64   default 20231024_01:17
alt     p9      amd64   default 20231024_01:17
alt     p9      arm64   default 20231024_01:17
amazonlinux     current amd64   default 20231024_05:09
amazonlinux     current arm64   default 20231024_05:09
archlinux       current amd64   default 20231024_04:18
archlinux       current arm64   default 20231024_04:18
busybox 1.34.1  amd64   default 20231024_06:00
busybox 1.34.1  arm64   default 20231024_06:00
centos  7       amd64   default 20231024_07:08
centos  7       arm64   default 20231024_07:08
centos  8-Stream        amd64   default 20231024_07:08
centos  8-Stream        arm64   default 20231024_07:08
centos  9-Stream        amd64   default 20231024_07:08
centos  9-Stream        arm64   default 20231024_07:08
debian  bookworm        amd64   default 20231024_05:24
debian  bookworm        arm64   default 20231024_05:24
debian  bullseye        amd64   default 20231024_05:24
debian  bullseye        arm64   default 20231024_05:24
debian  buster  amd64   default 20231024_05:24
debian  buster  arm64   default 20231024_05:24
debian  sid     amd64   default 20231024_05:24
debian  sid     arm64   default 20231024_05:24
devuan  beowulf amd64   default 20231023_11:50
devuan  beowulf arm64   default 20231023_11:50
devuan  chimaera        amd64   default 20231023_11:50
devuan  chimaera        arm64   default 20231023_11:50
devuan  daedalus        amd64   default 20231023_11:50
devuan  daedalus        arm64   default 20231023_11:50
fedora  37      amd64   default 20231023_20:33
fedora  37      arm64   default 20231023_20:33
fedora  38      amd64   default 20231023_20:33
fedora  38      arm64   default 20231023_20:33
funtoo  1.4     amd64   default 20231023_16:45
funtoo  1.4     arm64   default 20231023_16:45
funtoo  next    amd64   default 20231023_16:45
funtoo  next    arm64   default 20231023_16:45
kali    current amd64   default 20231023_17:14
kali    current arm64   default 20231023_17:14
mint    ulyana  amd64   default 20231024_08:51
mint    ulyssa  amd64   default 20231024_08:51
mint    uma     amd64   default 20231024_08:51
mint    una     amd64   default 20231024_08:51
mint    vanessa amd64   default 20231024_08:51
mint    vera    amd64   default 20231024_08:51
mint    victoria        amd64   default 20231024_08:51
nixos   current amd64   default 20231024_01:00
nixos   current arm64   default 20231024_01:00
openeuler       20.03   amd64   default 20231023_15:48
openeuler       20.03   arm64   default 20231023_15:48
openeuler       22.03   amd64   default 20231023_15:48
openeuler       22.03   arm64   default 20231023_15:48
openeuler       23.03   amd64   default 20231023_15:48
openeuler       23.03   arm64   default 20231023_15:48
opensuse        15.4    amd64   default 20231024_04:20
opensuse        15.4    arm64   default 20231024_04:20
opensuse        15.5    amd64   default 20231024_04:20
opensuse        15.5    arm64   default 20231024_04:20
opensuse        tumbleweed      amd64   default 20231024_04:39
opensuse        tumbleweed      arm64   default 20231024_04:20
openwrt 21.02   amd64   default 20231023_11:57
openwrt 21.02   arm64   default 20231023_11:57
openwrt 22.03   amd64   default 20231023_11:57
openwrt 22.03   arm64   default 20231023_11:57
openwrt 23.05   amd64   default 20231023_11:57
openwrt 23.05   arm64   default 20231023_11:57
openwrt snapshot        amd64   default 20231023_11:57
openwrt snapshot        arm64   default 20231023_11:57
oracle  7       amd64   default 20231024_07:46
oracle  7       arm64   default 20231024_08:12
oracle  8       amd64   default 20231024_07:46
oracle  8       arm64   default 20231024_08:13
oracle  9       amd64   default 20231024_07:46
oracle  9       arm64   default 20231024_08:23
plamo   7.x     amd64   default 20231024_01:33
plamo   8.x     amd64   default 20231024_01:33
rockylinux      8       amd64   default 20231024_02:06
rockylinux      8       arm64   default 20231024_02:06
rockylinux      9       amd64   default 20231024_02:06
rockylinux      9       arm64   default 20231024_02:06
slackware       15.0    amd64   default 20231023_23:08
slackware       current amd64   default 20231023_23:08
springdalelinux 7       amd64   default 20231024_06:38
springdalelinux 8       amd64   default 20231024_06:38
springdalelinux 9       amd64   default 20231024_06:38
ubuntu  bionic  amd64   default 20231023_07:42
ubuntu  bionic  arm64   default 20231023_07:42
ubuntu  focal   amd64   default 20231023_07:42
ubuntu  focal   arm64   default 20231023_07:42
ubuntu  jammy   amd64   default 20231023_07:42
ubuntu  jammy   arm64   default 20231023_07:42
ubuntu  lunar   amd64   default 20231023_07:42
ubuntu  lunar   arm64   default 20231023_08:11
ubuntu  mantic  amd64   default 20231023_07:42
ubuntu  mantic  arm64   default 20231023_08:09
ubuntu  xenial  amd64   default 20231023_07:42
ubuntu  xenial  arm64   default 20231023_08:10
voidlinux       current amd64   default 20231023_17:10
voidlinux       current arm64   default 20231023_17:10
---
Distribution: 
ubuntu
Release: 
bionic
Architecture: 
amd64
Downloading the image index
Downloading the rootfs
Downloading the metadata
The image cache is now ready
Unpacking the rootfs
---
You just created an Ubuntu bionic amd64 (20231023_07:42) container.
To enable SSH, run: apt install openssh-server
No default root or user password are set by LXC.

启动test-ubuntu容器,查看容器信息:

$ sudo lxc-start -n test-ubuntu
frd@frd-PowerEdge-T620:~$ sudo lxc-ls --fancy
NAME         STATE   AUTOSTART GROUPS IPV4       IPV6 UNPRIVILEGED 
test-busybox RUNNING 0         -      -          -    false        
test-ubuntu  RUNNING 0         -      10.0.3.241 -    false        
frd@frd-PowerEdge-T620:~$ sudo lxc-info -n test-ubuntu
Name:           test-ubuntu
State:          RUNNING
PID:            7249
IP:             10.0.3.241
CPU use:        1.00 seconds
BlkIO use:      27.94 MiB
Memory use:     30.79 MiB
KMem use:       11.91 MiB
Link:           vethWU1UQG
 TX bytes:      1.88 KiB
 RX bytes:      3.94 KiB
 Total bytes:   5.82 KiB
frd@frd-PowerEdge-T620:~$

修改用户ubuntu密码为ubuntu:

$ sudo chroot /var/lib/lxc/test-ubuntu/rootfs passwd ubuntu
passwd: no record of root in /etc/shadow, using /etc/passwd
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully

登录test-ubuntu容器控制台:

$ sudo lxc-console -n test-ubuntu
Connected to tty 1
Type <Ctrl+a q> to exit the console, <Ctrl+a Ctrl+a> to enter Ctrl+a itself
Ubuntu 18.04.6 LTS test-ubuntu pts/0
test-ubuntu login: ubuntu
Password: 
Last login: Tue Oct 24 10:52:57 UTC 2023 on pts/0
Welcome to Ubuntu 18.04.6 LTS (GNU/Linux 4.15.0-191-generic x86_64)
 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage
ubuntu@test-ubuntu:~$ 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jason.rr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值