Score:2

Ubuntu Multipass Cloud-init SSH Issue: Retaining 'ubuntu' User's SSH Key While Injecting My Own

sn flag

I am trying to create a cloud-init configuration file to inject my own SSH keys into the "ubuntu" user for a Multipass instance. I have managed to successfully inject my SSH key when I create a new user for myself and set the SSH key for that user. However, the SSH key for the "ubuntu" user disappears in the process. How should I structure the YAML file so that the "ubuntu" user remains unchanged and I can still access the machine using SSH?

Here is my example cloud-init YAML file:

#cloud-config

users:
  - default
  - name: vmuser
    sudo: ALL=(ALL) NOPASSWD:ALL
    ssh_authorized_keys:
    - ssh-rsa AAAAB3Nza....

output:
  all: ">> /var/log/cloud-init-output.log"

Additionally, I have tried using the runcmd and echo commands to directly set the SSH key in the authorized_keys file, like so:

runcmd:
  - sudo echo "ssh-rsa AAAAB3Nza..." >> ~/home/ubuntu/.ssh/authorized_keys

However, this does not work.

Any help or guidance on how to achieve this would be greatly appreciated. Thank you!

muru avatar
us flag
`~/home/ubuntu` = `/home/<user-the-command-is-run-as>/home/ubuntu`
Score:0
th flag

The ssh_authorized_keys key should do what you need. The following example will set the key for your user along with the default ubuntu user:

#cloud-config

users:
  - default
  - name: vmuser
    sudo: ALL=(ALL) NOPASSWD:ALL
    ssh_authorized_keys:
    - ssh-rsa AAAAB3Nza....

output:
  all: ">> /var/log/cloud-init-output.log"

ssh_authorized_keys:
  - ssh-rsa AAAAB3Nza....
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.