How can non-root user access key when creating Linode via api?

According to api doc:

authorized_users is a list of usernames. If the usernames have associated SSH keys, the keys will be appended to the root users ~/.ssh/authorized_keys file automatically.

I assume I need to append user's key to authorized_keys. How would I then copy this key to the user's .ssh directory so I can login as the user after creating the Linode? I'm using a Stackscript so would it be something like ssh-copy-id?

1 Reply

There are a few different ways you could achieve this. Since your non-root user's authorized_keys file will be empty upon deployment, you could simply copy the root authorized_keys file over and chown to get the desired result:

cp /root/.ssh/authorized_keys /home/$USER/.ssh/authorized_keys
chown $USER:$USER /home/$USER/.ssh/authorized_keys

Alternatively:

cat /root/.ssh/authorized_keys >> /home/$USER/.ssh/authorized_keys

Since you're using a StackScript you could also use a User Defined Variable to accomplish the same thing without requiring the key to ever exist in root's authorized_keys file.

echo $UDF >> /home/$USER/.ssh/authorized_keys

This difference with this method is that it would require either entering the UDF upon deployment or hard coding it into the StackScript.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct