New: letterpost.eu, forms for static sites
Put your SSH keys inside a vault

Put your SSH keys inside a vault

You put passwords into a vault? That is good. But your SSH keys belong there too. Don’t risk these to get compromised. Store them securely in Vaultwarden.

In the current light of events, 1Password got hacked again, I am happy once more, that I made the switch to a selfhosted password manager long time ago. But then I came across a post by Jola talking about the possibility and risk to store SSH keys locally - usually as a file inside your ~/.ssh folder. I never thought about this to be a problem, but then realised it can get risky.

Some months ago a friend became a victim of a malware attack, I wrote about it here, when applying for a job and running a coding test. And back then the SSH keys were compromised. What if this would happen to me?

I already use Vaultwarden with rbw1 on the command line. Vaultwarden supports storing SSH keys, and rbw can run a socket so your ssh client can use that identity socket.

Let’s go!

Import your keys

Neither Bitwardens web view does not support importing SSH keys, neither does the Desktop client. The import is a kind of hidden functionality in the Desktop client after your created a new key.

How to import your existing key:

  1. Create a new SSH key and save it in the vault.
  2. Go your terminal and copy your exisiting private SSH key into the clipboard.
  3. Click on edit of your freshly created key in the Desktop client.
  4. Right of the SSH key field, you can find a button “Import SSH key”. This takes your SSH key from the clipboard and stores it into the vault.

Do this for all the keys you already created locally and do not delete the pub and secret files yet.

Configure the SSH client

To ensure our SSH keys are properly found we need to tell the SSH client to look these up using rbw. For this, rbw provides a socket which is usually put at $XDG_RUNTIME_DIR/rbw/ssh-agent-socket. If the file is not already there, configure and run rbw a first time.

After the socket fils is there, we need to configure the path to this file in the ssh configuration.

You can add this to your .zshrc:

1export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/rbw/ssh-agent-socket"

This ensures every application which needs an SSH key knows where to look. You can now run ssh-add -L to test and see which keys are loaded by the SSH agent. You can also move your keys out of the .ssh folder, to be really sure the keys from Vaultwarden are used.

Additional configuration

 1Host *
 2  IdentitiesOnly yes
 3  # ...
 4
 5# Sample host1
 6Host nas2
 7  Hostname 192.168.178.98
 8  User timmy
 9  IdentityFile ~/.ssh/my_internal_key.pub  
10
11# Sample host2
12Host nas1
13  Hostname 41.62.x.x
14  User timmy
15  IdentityFile ~/.ssh/external.pub
~/.ssh/config

I made the configuration a bit more robust, and secure, by defining explicitly which key should be user per host. With IdentitiesOnly yes a definition of the key file per host is enforced. The SSH client does not need to guess, but gets a specified key to pick.

With the IdentityFile I define which key file should be used. Yes, the *.pub key is defined here. SSH get the private key from rbw automatically.

Distribute keys with chezmoi

I use Vaultwarden with rbw across multiple devices - Mac, Linux and NetBSD. To sync the public key across the devices, chezmoi is a great tool to keep files within a Git repository and pull it with changes on every machine.

I defined a template in private_dot_ssh/external-devices.tmpl that pulls in the public key from rbw.

1{{- /* rbw item: external-devices */ -}}
2{{ (rbw "<entry id in vaultwarden>").data.public_key }}
3 

This is documented in the chezmoi docs.

Closing thoughts

This setup works great for me for multiple devices and platforms. rbw caches the master password for some time so you do not need to enter the password every time. I cannot see any slowdown - rbw is super fast.


  1. The official command line client from Bitwarden runs with Electron. It is painfully slow compared to rbw written in Rust. ↩︎

This content was created on and updated on .
Enjoying codedge?

If I helped you in some way with my writing, a small contribution would support me. Your support keeps me creating!
Send a tip