0

I need to set the root password in the autoinstall as it allows me to check the system by logging into the console after installation. Unfortunately, this doesn't seem to work, and I have the following:

#cloud-config
users:
  - name: root
    lock_passwd: false
    hashed_passwd: <output from mkpasswd --method=SHA-512>

I even tried using late-commands run-command sections with chpasswd commands like the following, but it didn't work

chroot /target sh -c 'echo "root:<output from mkpasswd --method=SHA-512>" | /usr/sbin/chpasswd -e'

Is it possible to change the root password using auto-install? If so, I would appreciate any assistance. Thanks

Prasad
  • 1
  • https://stackoverflow.com/questions/61591885/how-do-i-set-a-custom-password-with-cloud-init-on-ubuntu-20-04 – moo Feb 14 '24 at 16:42

1 Answers1

0

The user-data section can be used. With this option cloud-init will set the root password during first boot. Be aware that the root password hash will be in /etc/cloud/cloud.cfg.d/99-installer.cfg on the installed system.

#cloud-config
autoinstall:
  user-data:
    chpasswd:
      expire: false
      list:
        - root:$6$REDACTED

Alternatively you can use late-commands. This will set the password during the install. Be aware the root password hash will be in the installer logs in /var/log/installer on the installed system.

#cloud-config
autoinstall:
  late-commands:
    - |
      echo 'root:$6$REDACTED' | /usr/sbin/chroot /target chpasswd -e

The chroot needs to be after the pipe so the proper command is chrooted.

note

You can not use the cloud-init users module to set the password for root because the root user exists. The config schema documentation explicitly states

passwd: ... This will NOT be applied if the user already exists

see also

  • Thanks, unfortunately, it doesn't work. I can see the root password hash in the /etc/cloud/cloud.cfg.d/99-installer.cfg file on the installed system but am unable to login. datasource: None: metadata: instance-id: b21ac651-8e44-4197-bd96-9aced02090f0 userdata_raw: "#cloud-config\ngrowpart:\n mode: 'off'\nlocale: en_US.UTF-8\n\ preserve_hostname: true\nresize_rootfs: false\nssh_pwauth: false\nusers:\n-\ \ gecos: root\n groups: adm,cdrom,dip,lxd,plugdev,sudo\n lock_passwd: false\n\ \ name: root\n passwd: $6$REDACTED\n\ – Prasad Feb 14 '24 at 13:10
  • @Prasad Can you confirm you are trying to login by console (and not by ssh)? Can you confirm you replaced $6$REDACTED with your own password hash? If you are able to see the 99-installer.cfg file contents then you can also check /etc/shadow to see if it is correct and the logs to see if they indicate why the login failed. – Andrew Lowther Feb 15 '24 at 23:01
  • Thanks, Andrew! I am logging in to the console and have replaced $6$REDACTED with the actual hash. Moreover, I can set the password correctly if I use the same hash using chpasswd -e after the installation. The strange thing is that I am not seeing the password hash in /etc/shadow it's * instead of the hash, which I believe means the password is locked despite the auto-install configuration intended to set and activate it. It gets replaced with the hash if I set it with chpasswd -e after logging in to the system post installation. Any pointers on what could be wrong here? – Prasad Feb 16 '24 at 02:43
  • @Prasad It looks like you are using the cloud-init users module instead of chpasswd. I updated the answer with a note why this will not work. – Andrew Lowther Feb 16 '24 at 22:55