1

I'm trying to set up some defaults for several of my Ubuntu machines running the Ubuntu 22.04 server. Here is my config:

#cloud-config
ssh_pwauth: false

groups:
  - mygrp

users:
  - name: myuser
    gecos: myuser
    sudo: ALL=(ALL) NOPASSWD:ALL
    groups: [mygrp, sudo]
    ssh_import_id: None
    lock_passwd: true
    shell: /bin/bash
    # Replace the <ssh-pub-key> with the actual SSH public key
    ssh_authorized_keys:
      - <ssh-pub-key>
    
preserve_hostname: false
hostname: open-electrons-m1
manage_etc_hosts: true

# If this is set, 'root' will not be able to ssh in and they
# will get a message to login instead as the default $user
disable_root: true

What is wrong with this script that I have? I do not see any issues in the cloud-init.log. No error messages. After running:

cloud-init init

Running this command:

cat /etc/hostname

is not printing my-host-name

The user is also not created. What is happening with this script? I'm breaking my head around this!

joesan
  • 13,963
  • 27
  • 95
  • 232

1 Answers1

1

After running cloud-init init

This is not how cloud-init works. Cloud-init runs multiple times on boot as part of many different systemd services. Additionally, most modules that run during first boot are set to not run again, even if cloud-init runs again. You wouldn't want cloud-init attempting to recreate a new user every single boot, so cloud-init won't attempt to create users again unless it is specifically requested to. See the boot stage docs for more info on the cloud-init boot stages.

If you're trying to emulate cloud-init's first-boot behavior, you have a couple of different options. The safest way is to install LXD and launch a new container from there. If your cloud-config has already been supplied to the instance, you can run the cloud-init commands manually. I would only run this in a container/VM as it could make destructive changes to your system:

cloud-init clean --logs
cloud-init init --local
cloud-init init
cloud-init modules --mode=config
cloud-init modules --mode=final
cloud-init status --wait --long

Using LXD, I ran your config and it seems to work mostly as expected:

root@me:~# cat /etc/hostname
open-electrons-m1
root@me:~# grep "myuser" /etc/passwd
myuser:x:1000:1001:myuser:/home/myuser:/bin/bash

There's still some problems with the cloud-config though. According the users and groups module

Groups are added before users, so any users in a group list must already exist on the system.

So your user cannot be added to the mygrp group. Also None isn't a valid value for ssh_import_id. You can remove that line entirely.

falcojr
  • 1,299
  • 1
  • 10
  • 18
  • So what is wrong in my configuration? I have a group added before users, but running this with cloud-init init command does not create myuser – joesan Jan 13 '23 at 18:12