Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify private key in SSH as string

Tags:

bash

ssh

I can connect to a server via SSH using the -i option to specify the private key:

ssh -i ~/.ssh/id_dsa user@hostname 

I am creating a script that takes the id_dsa text from the database but I am not sure how I can give that string to SSH. I would need something like:

ssh --option $STRING user@hostname 

Where $STRING contains the value of id_dsa. I need to know the --option if there is one.

like image 732
rtacconi Avatar asked Aug 20 '12 16:08

rtacconi


People also ask

How do I specify a private key using ssh?

To specify a private key file in SSH from the command line, you can simply use -i option in the ssh command. However, things get complicated when you have multiple private keys. In that case, you can declare which private key to use for each SSH server, in your SSH configuration file which is found at ~/. ssh/config .

Can you share ssh private key?

ssh between systems is fine so long as it's limited to just files like authorized_keys , config , and known_hosts . If you want two hosts to be able to access each other, each host needs its own private SSH key, which must then be added to the other host's authorized_keys file.


2 Answers

Try the following:

echo $KEY | ssh -i /dev/stdin username@host command 

The key doesn't appear from a PS statement, but because stdin is redirected it's only useful for single commands or tunnels.

like image 89
user2132025 Avatar answered Sep 20 '22 19:09

user2132025


There is no such switch - as it would leak sensitive information. If there were, anyone could get your private key by doing a simple ps command.

EDIT: (because of theg added details in comment)

You really should store the key in to a temporary file. Make sure you set the permissions correctly before writing to the file, if you do not use command like mktemp to create the temporary file.

Make sure you run the broker (or agent in case of OpenSSH) process and load the key using <whatever command you use to fetch it form the database> | ssh-add -

like image 26
Kimvais Avatar answered Sep 20 '22 19:09

Kimvais