Debian官方qcow2镜像修改root密码

前言

debian官网的qcow2镜像不设置便无法登录,且根分区只有2G的大小,这里扩容到10G
默认需要使用cloud-init来设置账户密码,这里提供几种其他修改root账号密码的方法

1
2
3
sudo apt install qemu-utils libguestfs-tools
wget https://cloud.debian.org/images/cloud/buster/20200511-260/debian-10-generic-arm64-20200511-260.qcow2
qemu-img resize debian-10-generic-arm64-20200511-260.qcow2 +8G

QEMU方案

1
2
3
4
5
6
7
8
9
10
modprobe nbd max_part=8			# 加载模块
qemu-nbd -c /dev/nbd0 /home/pi/debian-10-generic-arm64-20200511-260.qcow2
mount /dev/nbd0p1 /mnt/ # 挂载目录

chroot /mnt/ /bin/bash -l # 切换目录
passwd root # 修改密码
systemctl disable cloud-init-local cloud-init cloud-config cloud-final

umount /mnt/ # 取消挂载
qemu-nbd -d /dev/nbd0 # 关闭设备

libguestfs-tools方案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 方法一:virt-customize
virt-customize -a debian-10-generic-arm64-20200511-260.qcow2 --root-password password:root

# 方法二:guestfish
# 使用MD5算法对密码进行加密,将/etc/shadow中root:后面的第一个*替换为加密之后的密码
# 例如:root:$1$IsebZzF7$ccckI/6ym7yuGJb0THxwJ0:::::::
openssl passwd -1 YouPassword

guestfish --rw -a debian-10-generic-arm64-20200511-260.qcow2
><fs> run
><fs> list-filesystems
/dev/sda1: ext4
><fs> mount /dev/sda1 /
><fs> vi /etc/shadow

Cloud-init方案

1
2
3
4
5
6
7
8
9
10
11
12
# 根据上面方案挂载qcow2镜像后修改/etc/cloud/cloud.cfg中对应位置为如下
# 设置debian用户密码为debian并允许密码登录和ssh密码登录

default_user:
name: debian
lock_passwd: False
plain_text_passwd: 'debian'
gecos: Debian
groups: [adm, audio, cdrom, dialout, dip, floppy, netdev, plugdev, sudo, video]
sudo: ["ALL=(ALL) NOPASSWD:ALL"]
shell: /bin/bash
ssh_pwauth: 1

启动镜像

1
2
3
4
5
6
7
8
9
10
11
sudo apt install qemu-system-arm qemu-efi-aarch64 seabios vgabios

sudo qemu-system-aarch64 \
-M virt -smp 4 -m 1024 -cpu host -enable-kvm \
-bios /usr/share/qemu-efi-aarch64/QEMU_EFI.fd \
-device ramfb -device qemu-xhci,id=xhci \
-device usb-kbd -device usb-tablet -k en-us \
-device virtio-blk,drive=system \
-drive if=none,id=system,cache=unsafe,media=disk,file=debian.qcow2 \
-device virtio-net,disable-legacy=on,netdev=net0 \
-netdev user,id=net0,hostfwd=tcp::2222-:22 -vnc :0