Reset root password in LXC container

Installing container in LXC goes like this:

lxc-create -n my-ubuntu -t download -- --dist ubuntu --release jammy --arch amd64

Run and login:

lxc-start -n my-ubuntu -F

Here you may get incorrect login, so need to change password.

First, stop container, in this case the name of container is my-ubuntu:

lxc-stop -n my-ubuntu

Then check if any user has pass there in container my-ubuntu:

cat /var/lib/lxc/my-ubuntu/rootfs/etc/shadow
root:*:20271:0:99999:7:::
daemon:*:20271:0:99999:7:::
bin:*:20271:0:99999:7:::
sys:*:20271:0:99999:7:::
sync:*:20271:0:99999:7:::
games:*:20271:0:99999:7:::
man:*:20271:0:99999:7:::
lp:*:20271:0:99999:7:::
mail:*:20271:0:99999:7:::
news:*:20271:0:99999:7:::
uucp:*:20271:0:99999:7:::
proxy:*:20271:0:99999:7:::
www-data:*:20271:0:99999:7:::
backup:*:20271:0:99999:7:::
list:*:20271:0:99999:7:::
irc:*:20271:0:99999:7:::
gnats:*:20271:0:99999:7:::
nobody:*:20271:0:99999:7:::
systemd-network:*:20271:0:99999:7:::
systemd-resolve:*:20271:0:99999:7:::
messagebus:*:20271:0:99999:7:::
systemd-timesync:*:20271:0:99999:7:::
syslog:*:20271:0:99999:7:::
_apt:*:20271:0:99999:7:::
ubuntu:!:20271:0:99999:7:::
 

No, no any user has password. So, use chroot and passwd like this:

lxc-start -n my-ubuntu  #start container
lxc-attach -n my-ubuntu  #chroot to it
root@my-ubuntu:~# passwd  #set new pass
New password: admin
Retype new password: admin
passwd: password updated successfully
root@my-ubuntu:~# exit
exit
# lxc-stop -n my-ubuntu #stop container again

Now start and login as usual:

lxc-start -n my-ubuntu -F

my-ubuntu login: root
Password: admin
Welcome to Ubuntu 22.04.5 LTS (GNU/Linux 6.1.0-37-amd64 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

root@my-ubuntu:~# 

You are in container's system.

Section