# BalenaRequestError when trying to add SSH key

**URL:** <https://forums.balena.io/t/balenarequesterror-when-trying-to-add-ssh-key/34415>\
**Category:** openBalena\
**Created:** [September 26, 2019, 11:04pm UTC](https://forums.balena.io/t/balenarequesterror-when-trying-to-add-ssh-key/34415 "2019-09-26T23:04:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![robin](https://avatars.discourse-cdn.com/v4/letter/r/e5b9ba/32.png) [@robin](https://forums.balena.io/u/robin)\
**Post date:** [September 26, 2019, 11:04pm UTC](https://forums.balena.io/t/balenarequesterror-when-trying-to-add-ssh-key/34415/1 "2019-09-26T23:04:10Z")

</div>

I have a freshly deployed openBalena server running on an Ubuntu 18.04 digital ocean VM.  
It 's using an LE certificate.

I have also installed the latest balena CLI on my Mac OS X 10.14.5 machine.  
I’m running node v12.1.0

I can succesfully log in on the openBalena server with balena CLI.  
However, when I try to add my SSH key, it fails with the following output:

> ❯ balena keys  
> [debug] original argv0=“/usr/local/lib/balena-cli/bin/node” argv=[/usr/local/lib/balena-cli/bin/node,/usr/local/lib/balena-cli/bin/run,keys] length=3  
> BalenaRequestError: Request error: Internal Server Error  
> at /usr/local/lib/balena-cli/node\_modules/balena-request/build/request.js:197:17  
> at tryCatcher (/usr/local/lib/balena-cli/node\_modules/bluebird/js/release/util.js:16:23)  
> at Promise.\_settlePromiseFromHandler (/usr/local/lib/balena-cli/node\_modules/bluebird/js/release/promise.js:517:31)  
> at Promise.\_settlePromise (/usr/local/lib/balena-cli/node\_modules/bluebird/js/release/promise.js:574:18)  
> at Promise.\_settlePromise0 (/usr/local/lib/balena-cli/node\_modules/bluebird/js/release/promise.js:619:10)  
> at Promise.\_settlePromises (/usr/local/lib/balena-cli/node\_modules/bluebird/js/release/promise.js:699:18)  
> at \_drainQueueStep (/usr/local/lib/balena-cli/node\_modules/bluebird/js/release/async.js:138:12)  
> at \_drainQueue (/usr/local/lib/balena-cli/node\_modules/bluebird/js/release/async.js:131:9)  
> at Async.\_drainQueues (/usr/local/lib/balena-cli/node\_modules/bluebird/js/release/async.js:147:5)  
> at Immediate.Async.drainQueues [as \_onImmediate] (/usr/local/lib/balena-cli/node\_modules/bluebird/js/release/async.js:17:14)  
> at runCallback (timers.js:705:18)  
> at tryOnImmediate (timers.js:676:5)  
> at processImmediate (timers.js:658:5)  
> at process.topLevelDomainCallback (domain.js:126:23)

> ❯ balena key add Robin ~/.ssh/id\_rsa.pub  
> [debug] original argv0=“/usr/local/lib/balena-cli/bin/node” argv=[/usr/local/lib/balena-cli/bin/node,/usr/local/lib/balena-cli/bin/run,key,add,Robin,/Users/robin/.ssh/id\_rsa.pub] length=6  
> BalenaRequestError: Request error: Internal Server Error  
> at /usr/local/lib/balena-cli/node\_modules/balena-request/build/request.js:197:17  
> at tryCatcher (/usr/local/lib/balena-cli/node\_modules/bluebird/js/release/util.js:16:23)  
> at Promise.\_settlePromiseFromHandler (/usr/local/lib/balena-cli/node\_modules/bluebird/js/release/promise.js:517:31)  
> at Promise.\_settlePromise (/usr/local/lib/balena-cli/node\_modules/bluebird/js/release/promise.js:574:18)  
> at Promise.\_settlePromise0 (/usr/local/lib/balena-cli/node\_modules/bluebird/js/release/promise.js:619:10)  
> at Promise.\_settlePromises (/usr/local/lib/balena-cli/node\_modules/bluebird/js/release/promise.js:699:18)  
> at \_drainQueueStep (/usr/local/lib/balena-cli/node\_modules/bluebird/js/release/async.js:138:12)  
> at \_drainQueue (/usr/local/lib/balena-cli/node\_modules/bluebird/js/release/async.js:131:9)  
> at Async.\_drainQueues (/usr/local/lib/balena-cli/node\_modules/bluebird/js/release/async.js:147:5)  
> at Immediate.Async.drainQueues [as \_onImmediate] (/usr/local/lib/balena-cli/node\_modules/bluebird/js/release/async.js:17:14)  
> at runCallback (timers.js:705:18)  
> at tryOnImmediate (timers.js:676:5)  
> at processImmediate (timers.js:658:5)  
> at process.topLevelDomainCallback (domain.js:126:23)

Has anyone faced this before?  
Any tips on how to troubleshoot this further?

---

<div class="post-metadata">

**Author:** ![richbayliss](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/richbayliss/32/1054_2.png) [@richbayliss](https://forums.balena.io/u/richbayliss)\
**Post date:** [September 27, 2019, 2:47pm UTC](https://forums.balena.io/t/balenarequesterror-when-trying-to-add-ssh-key/34415/4 "2019-09-27T14:47:55Z")

</div>

Hi @robin

openBalena doesn’t support adding SSH keys via the CLI this way, nor does it support SSH through the CLI either (which is why I presume you are adding it)

It is possible to add your SSH key in the device `config.json` file as per this link: [https://github.com/balena-os/meta-balena#sshkeys](https://github.com/balena-os/meta-balena#sshkeys)

---

<div class="post-metadata">

**Author:** ![robin](https://avatars.discourse-cdn.com/v4/letter/r/e5b9ba/32.png) [@robin](https://forums.balena.io/u/robin)\
**Post date:** [September 27, 2019, 3:23pm UTC](https://forums.balena.io/t/balenarequesterror-when-trying-to-add-ssh-key/34415/5 "2019-09-27T15:23:23Z")

</div>

@richbayliss Thank you for your reply.

I have gone through the FAQ and this is not mentioned under the relevant entry.  
It would be tremendously useful to create a comprehensive table with differences…

Is adding env variables supported using balena CLI to openBalena?  
That command seems to execute correctly, but the env variables are not added…

---

<div class="post-metadata">

**Author:** ![thundron](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/thundron/32/1379_2.png) [@thundron](https://forums.balena.io/u/thundron)\
**Post date:** [September 27, 2019, 6:54pm UTC](https://forums.balena.io/t/balenarequesterror-when-trying-to-add-ssh-key/34415/7 "2019-09-27T18:54:23Z")

</div>

Hi,  
It is possible, you can refer to [this forum post](https://forums.balena.io/t/support-for-device-environment-variables/4357/3) for the command and a bit of troubleshooting following as well.  
Tell us how it goes and if you encounter any issues!

---

<div class="post-metadata">

**Author:** ![robin](https://avatars.discourse-cdn.com/v4/letter/r/e5b9ba/32.png) [@robin](https://forums.balena.io/u/robin)\
**Post date:** [September 30, 2019, 7:20am UTC](https://forums.balena.io/t/balenarequesterror-when-trying-to-add-ssh-key/34415/9 "2019-09-30T07:20:25Z")

</div>

It seems setting environment variable, both at the device and application level work splendidly.  
In my case, however, there appears to be an issue with listing the application envs.  
I can set them, and I can see them being applied to all devices in the application.  
However, when I try to query the server for it, I get “No environment variables found”.

> ❯ balena envs --application fort-napoleon [09:18:25]  
> [debug] original argv0=“/usr/local/lib/balena-cli/bin/node” argv=[/usr/local/lib/balena-cli/bin/node,/usr/local/lib/balena-cli/bin/run,envs,–application,fort-napoleon] length=5  
> [debug] new argv=[/usr/local/lib/balena-cli/bin/node,/usr/local/lib/balena-cli/bin/run,envs,–application,fort-napoleon] length=5  
> No environment variables found

---

<div class="post-metadata">

**Author:** ![dfunckt](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/dfunckt/32/56_2.png) [@dfunckt](https://forums.balena.io/u/dfunckt)\
**Post date:** [September 30, 2019, 12:26pm UTC](https://forums.balena.io/t/balenarequesterror-when-trying-to-add-ssh-key/34415/10 "2019-09-30T12:26:17Z")

</div>

Why do you need to add an SSH key? Keys are only used for authentication when doing `git push` on balenaCloud, which openBalena doesn’t support as a deployment method anyway.

That said, we recently added SSH keys into the openBalena API because they’re going to be used for SSHing into devices, but we haven’t published an openBalena version with these changes yet. (Nor should you manually update the API component, as it will most likely break your DB as it stands right now.)

Regarding env and config vars, the forum thread Lorenzo linked above should help, particularly this one that shows a workaround: [Support for device environment variables](https://forums.balena.io/t/support-for-device-environment-variables/4357/34)

---

<div class="post-metadata">

**Author:** ![robin](https://avatars.discourse-cdn.com/v4/letter/r/e5b9ba/32.png) [@robin](https://forums.balena.io/u/robin)\
**Post date:** [October 1, 2019, 8:40pm UTC](https://forums.balena.io/t/balenarequesterror-when-trying-to-add-ssh-key/34415/12 "2019-10-01T20:40:31Z")

</div>

I wanted to use SSH keys to able to login on the host os when connected to the local LAN to devices with a production image deployed.  
I managed to accomplish this by adding it to config.json before configuring the image to be flashed on the devices…

---

<div class="post-metadata">

**Author:** ![roman-mazur](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/roman-mazur/32/2194_2.png) [@roman-mazur](https://forums.balena.io/u/roman-mazur)\
**Post date:** [October 2, 2019, 8:40am UTC](https://forums.balena.io/t/balenarequesterror-when-trying-to-add-ssh-key/34415/15 "2019-10-02T08:40:54Z")

</div>

@robin Just wanted to let you know that in the upcoming [balenaOS version (2.44)](https://github.com/balena-os/meta-balena/pull/1688) we added the functionality of installing SSH keys added to the backend, so they can be used to SSH to the device automatically.  
The API endpoint used to fetch SSH keys is available in openBalena, so you should get a more convenient way of installing the SSH keys with the new OS version

---

<div class="post-metadata">

**Author:** ![bversluijs](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/bversluijs/32/3954_2.png) [@bversluijs](https://forums.balena.io/u/bversluijs)\
**Post date:** [October 3, 2019, 10:34am UTC](https://forums.balena.io/t/balenarequesterror-when-trying-to-add-ssh-key/34415/17 "2019-10-03T10:34:16Z")

</div>

Hi @roman-mazur,

Can you please explain what this function does and what it is for?  
It sounds like something we could really use!

---

<div class="post-metadata">

**Author:** ![roman-mazur](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/roman-mazur/32/2194_2.png) [@roman-mazur](https://forums.balena.io/u/roman-mazur)\
**Post date:** [October 3, 2019, 2:13pm UTC](https://forums.balena.io/t/balenarequesterror-when-trying-to-add-ssh-key/34415/18 "2019-10-03T14:13:26Z")

</div>

When you ssh into the device, it will check the user name, and if this user public key is not present on the device, it will try fetching it from the backend passing the username in the API call parameters. The backend can return SSH keys imported for that user.  
The backend endpoint implementation can be found here:

> <https://github.com/balena-io/open-balena-api/blob/v0.34.0/src/routes/auth.ts#L6>

Hope this helps. Cheers.
