Manually add ssh keys to a lxd container

Web & Teknik

Manually add ssh keys to a lxd container

First create a file called authorized_keys in the your home directory on the LXD host with the keys you want to use in the container. While we are on the subject it might be a good time to upgrade your ssh keys to ssh-keygen -t ed25519

# vi authorized_keys

Then use the following commands:

lxc exec demo2 -- useradd --create-home -s /bin/bash myusername
lxc exec demo2 -- passwd myusername
lxc exec demo2 -- mkdir /home/myusername/.ssh
lxc file push authorized_keys demo2/home/myusername/.ssh/
lxc exec demo2 -- chown -R myusername:myusername /home/myusername/.ssh

To do the same with the root user

lxc file push authorized_keys demo2/root/.ssh/

# if the user is going to be in the sudo group add

lxc exec demo2 -- adduser myusername sudo