How to Change the root Password and add a Public SSH Key to a QCOW2 Image and Flash That Image to an SSD
How to Change the root Password and add a Public SSH Key to a QCOW2 Image and Flash That Image to an SSD
- Create the password hash:
openssl passwd -6 'MyNewPassword'
- Use
guestfishto open the qcow2 image:
sudo guestfish --rw -a ./image.qcow2 -i
- Edit the
shadowfile:
edit /etc/shadow
- Find the
rootline and add in the hashed password like so:
root:$6$k1ZzD3M5$z2lFtIYwREEXAMPLEHASH/sCEzF/:19956:0::7:::
- For the public key, open the
authorized_keysfile. If you don’t already have a public/private ssh key pair, generate it withssh-keygen -t rsa -b 4096:
edit /root/.ssh/authorized_keys
-
Add your public
sshkey to the bottom of the file. -
exitfrom the image. -
Provide the following permissions on the local if needed:
chmod 700 /root/.ssh
chmod 600 /root/.ssh/authorized_keys
- Convert the
qcow2image intorawformat:
qemu-img convert -f qcow2 -O raw image.qcow2 image.raw
-
When copying the image onto the USB stick, make sure the USB’s filesystem is either XFS or EXT4. Create the partitions on the USB stick with
fdiskand thensudo mkfs.xfs /dev/<partition>for the filesystem. Change the file permissions as well afterwards withsudo chown howard:howard ./ -
Copy the
rawimage over to a USB stick and boot the target machine into the Rocky Linux recovery shell via a Ventoy USB (Rocky Linux 8.10 is a good choice). -
Flash the image and boot:
sudo dd if=image.raw of=/dev/sdX bs=4M status=progress conv=fsync