I have a dd os image, I can mount it and edit files, but the root user, has no password. so then when I burn the image, I can't log in via shh until I set a root password. I'd prefer not to change that. So instead I have to use a uart adapter, login as root and manually change the password. It would be simpler if I could skip that step, and just modify the dd image to include a root password, but I'm not sure how to do that. Suggestions please.
1 Answer
If you can mount it, and it has an operating system of the right architecture (i.e. same as on the host machine or compatible with the host - e.g. amd64 chroot on amd64 host, or x86 32-bit chroot on amd64 host) and at least a minimum viable operating system in the correct directory structure, then you can chroot into it and run passwd (or whatever else you need to run).
chroot runs a shell or other command as if the directory you specify is the root directory. In order for this to work, that directory must have its own /etc, /bin, /usr, /lib and other directories containing at least the minimum required config files, binaries, and libraries.
e.g. if you have mounted your disk image as /mnt:
# chroot /mnt
# passwd
.
. do anything else you need
.
# exit
alternatively, just run chroot /mnt passwd if all you need to do is execute that one command.
See man chroot for a summary of command-line options and, if you are using GNU coreutils, run info chroot or pinfo chroot for full documentation.
BTW, instead of (or as well as) setting the root password, you might want to add your ssh public keys to the disk image's /root/.ssh/authorized_keys file. That would allow you to set PasswordAuthentication no before you even deploy the image.
-
and what if its not the same architecture?j0h– j0h2022-12-04 19:20:02 +00:00Commented Dec 4, 2022 at 19:20
-
11. Add your public key(s) to root's authorized_keys file (optionally change the password over ssh later). 2. Or boot it up on an emulator and change the passwd while it's running on that. 3. Create a dummy account with the password you require and copy just the password from that /etc/shadow entry to root's /etc/shadow entry in the disk image. See When I copy /etc/shadow to another system, is it possible to login...? for some caveats. If you're using the same root password on multiple copies of the disk image, copy the password from a running imagecas– cas2022-12-05 01:24:57 +00:00Commented Dec 5, 2022 at 1:24