Setting a root password via REST API

I am trying to set a username and a password for new containers when creating through the REST API. The profile is attached to the instance, but the user does not exist.

Can anybody point me in the direction of setting the password?

ubuntu ~ > lxc profile show test
config:
  user.user-data: |-
    ssh_pwauth: yes
    users:
      - name: james
        passwd: $6$3N1u1YuLwzEGohID$O4qofI3MNgqr4tmO7SxrSWpE79jZ0Q6c0CfmI/yGi21Nfq.rF/RTSzvEqb.dV/f8QepZHwaNgfOcOCcj9T0Va1
description: Custom username and password
devices: {}
name: test
used_by:
- /1.0/instances/ubuntu-instance

Fairly certain it needs to start with #cloud-config I.E

user.user-data: |-
    #cloud-config
    ssh_pwauth: yes
    users:
      - name: james
        passwd: $6$3N1u1YuLwzEGohID$O4qofI3MNgqr4tmO7SxrSWpE79jZ0Q6c0CfmI/yGi21Nfq.rF/RTSzvEqb.dV/f8QepZHwaNgfOcOCcj9T0Va1
description: Custom username and password
devices: {}
name: test
used_by:
- /1.0/instances/ubuntu-instance

Also check /var/log/cloud-init-output.log & /var/log/cloud-init.log

Could not get it working still, no log files by those names either. I have tried running the exec endpoint to run echo -e "root\nroot" | passwd ubuntu, this was in log, but did not change either.

"root
root" | passwd ubuntu

Basically i am trying to figure out how to use the API to set ROOT password or use SSH keys. Spent hours and got no where.

odd, are you sure your using cloud-init enabled images? I would expected the logs to contain something, are you tail -f or inspecting them “properly”, it can be hard to miss!

To be honest I have only just come across this cloud-init images, I am downloading Ubuntu 20.04 from the linuxcontainers.org image server, and the files you mention do not exist.

So what is the correct way to set the password or SSH keys via the API, is this cloud init way?

You were so close,

Generate your password like so (I use “ubuntu” as the password for this)

mkpasswd -m sha-512 -s <<< ubuntu

put the following in a file

#cloud-config
ssh_pwauth: yes
users:
 - name: james
   passwd: $6$wjMk.uTUi1tUX2cX$qn7plHDqzdlVLPc0Y5ruiTTpLGuPM5qpFls47v5Z3xGxxhpGx1QlAW2SR/aluE.kkmpITSWBsZq9J.za0iiJz.
   lock_passwd: false

create a profile
lxc profile create ssh-access

set the data
lxc profile set ssh-access user.user-data - < /path/to/above/file.yaml

create the container
lxc launch ubuntu: ssh -p default -p ssh-access

get the ip address
lxc list (find Ip)

ssh to host

ssh james@container_ip
password: ubuntu #if you copy and paste my yaml file above

Many thanks, I will try to implement that through the API.

I mean the steps are the same as the lxc commands, they are just calling the API, if you have a more specific question about that API id be happy to help (please provide more details like what language are you using ETC)

1 Like

I am really grateful for your time and help and I will let you know, when I attack it again.

Also I saw your PHP code on Github for Rest API, it is very clean and beautiful, nicely written.

I am building a web based interface for LXD to make things simpler, its quite interesting and complex at the same time because its kind of unusual not working without a database backend at all.

I mean i do already have a whole web interface here https://github.com/turtle0x1/LxdMosaic if your interested in not re writting all the boring parts yourself and being able to jump straight into what ever you want

Your almost defo going to need a “db” of some kind to store the paths to the lxd servers and the paths to your certs etc - ssh is not good a way todo what you are doing, LXD has api instead use it!