linode-cli does not work in a shell script

I have a shell script that is set up something like this:

#!/usr/bin/env sh
set -x

MY_SERVER_AUTORIZED_KEY=${MY_SERVER_AUTORIZED_KEY:-$(cat ~/.ssh/id_rsa.pub)}

linode_id=$( \
  linode-cli linodes create \
    --image "linode/debian9" \
    --label my-server \
    --root_pass "${MY_SERVER_ROOTPW}" \
    --booted true \
    --region us-west \
    --type g6-standard-4 \
    --stackscript_id 607433 \
    --authorized_keys  "\"${MY_SERVER_AUTORIZED_KEY}\"" \
    --text --format 'id' --no-headers \
)
echo $linode_id

When I run this on my local computer, I get the following error:

Request failed: 400
authorized_keys SSH Key 1 key-type must be ssh-dss, ssh-rsa, ecdsa-sha2-nistp, or ssh-ed25519.

The thing is that when I run directly on the command line the same linode-cli command that gets printed due to my "set -x" I get a successful result. What am I doing wrong here?

2 Replies

Linode Staff

When I tested this script in my local environment, I received the same error. After some testing, I found that this syntax allows the Linode to be created:

#!/usr/bin/env sh
set -x

Key=$( cat ~/.ssh/id_rsa.pub )
MY_SERVER_AUTORIZED_KEY="$Key"

linode_id=$( \
  linode-cli linodes create \
    --image "linode/debian9" \
    --label my-server \
    --root_pass "${MY_SERVER_ROOTPW}" \
    --booted true \
    --region us-west \
    --type g6-standard-4 \
    --stackscript_id 607433 \
    --authorized_keys  "$MY_SERVER_AUTORIZED_KEY"
    --text --format 'id' --no-headers \
)
echo $linode_id

Saving the contents of id_rsa.pub into a separate variable before calling it makes the syntax easier to manage, but there's probably a way to combine both variables. I did also want to make a suggestion with regards to the Linode label, since when I was testing this script, I got an error stating "Label must be unique among your Linodes". You can avoid that by accepting input for --label the way you did for root password, or by creating a loop that iterates over subsequent numbers (i.e. my-server-1, my-server-2, etc.)

Thanks! I ended up settling on this syntax:

#!/usr/bin/env sh
set -x

RTMP_SERVER_AUTORIZED_KEY_FILE=${RTMP_SERVER_AUTORIZED_KEY_FILE:-"~/.ssh/id_rsa.pub"}

linode_id=$( \
  linode-cli linodes create \
    --image "linode/debian9" \
    --label my-server \
    --root_pass "${MY_SERVER_ROOTPW}" \
    --booted true \
    --region us-west \
    --type g6-standard-4 \
    --stackscript_id 607433 \
    --authorized_keys  "$(cat $RTMP_SERVER_AUTORIZED_KEY_FILE)" \
    --text --format 'id' --no-headers \
)
echo $linode_id

There was just something about putting the key contents into an environment variable that just didn't work. I never quite figured it out. However, having a file path in the environment variable works perfectly, so I just used that.

Concerning the label, the above is just example code the demonstrated the issue. My actual code is a little more sophisticated with this. But thanks!

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