如何更改 LXC 容器用户帐户的密码

LXC 容器有自己的用户名和密码定义。
无论容器是否正在运行,都可以在需要时更改 LXC 帐户的密码。

onitroad login: admin
Password:
Login incorrect
Oracle Linux Server 7.1
Kernel 3.8.13-118.19.3.el6uek.x86_64 on an x86_64
on it road .com

使用容器根文件系统作为chroot jail,正常修改密码

找到前面所示的 LXC 容器根文件系统。
然后使用 chroot(1) 命令在容器中输入一个执行上下文。
以 root 身份正常更改密码。

必须停止 LXC 容器才能执行此过程。

# chroot /container/Oracle7/rootfs/ passwd oracle
Changing password for user oracle.
New password:
BAD PASSWORD: The password contains the user name in some form
Retype new password:
passwd: all authentication tokens updated successfully.

然后启动容器,让用户正常登录。

重置忘记的 LXC 容器用户密码

以 root 用户身份连接到正在运行的容器

如果容器正在运行,则无需停止容器即可重置密码。

# lxc-attach -n Oracle7
[root@Oracle7 ~]# passwd
Changing password for user root.
New password:
BAD PASSWORD: The password contains the user name in some form
Retype new password:
passwd: all authentication tokens updated successfully.
[root@Oracle7 ~]#

lxc-attach 命令在正在运行的容器中启动一个新进程。
新进程将使用运行 lxc-attach 命令的用户的凭据运行。
这里 root 用户启动了一个普通的 shell 环境。
一旦 shell 启动,只需像往常一样使用常用的 passwd 命令。

删除密码以允许用户登录和正常重置。
LXC 容器文件可直接从主机命令行访问。
通过编辑 LCX 容器中的 /etc/passwd 文件,可以删除用户的密码要求,以打开帐户进行无密码登录。

第一个目标是找到要修改的文件的 LXC 容器路径。
必须停止 LXC 容器才能执行此过程。

步骤1:找到保存容器文件的位置

# lxc-config -l
lxc.default_config
lxc.lxcpath
lxc.bdev.lvm.vg
lxc.bdev.lvm.thin_pool
lxc.bdev.zfs.root
# lxc-config lxc-lxcpath
/container

首先,找出哪些 LXC 属性可用。
使用 lxc-lxcpath 属性来定位容器文件:

步骤2:LXC 主机挂载的容器文件在哪里?

# mount | grep container
/dev/sdb on /container type btrfs (rw)

在这里,可以通过 /container 目录访问 LXC 容器文件。

步骤3:找到容器根文件系统

# cd /container/
# ls
lxc-monitord.log Oracle5U9 Oracle7 Orcle5U9
# cd Oracle7/
# ls
config Oracle7.log rootfs rootfs.dev
# cd rootfs
# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin selinux srv sys tmp usr var

步骤4:删除 etc/fstab 中问题用户的第二个字段

# grep oracle etc/passwd
oracle:x:1000:1000::/home/oracle:/bin/bash
# vi etc/passwd

删除感兴趣的用户的前两个冒号 (:) 字符之间的任何内容

# grep oracle etc/passwd
oracle::1000:1000::/home/oracle:/bin/bash
日期:2020-09-17 00:13:11 来源:oir作者:oir