Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script that executes ssh-add and feeds the passphrase automatically

Tags:

git

bash

ssh

I'm using a script that executes

eval `ssh-agent`
ssh-add

which prompts you to input your SSH passphrase. Is it possible to input the passphrase from the script? The goal of this is to open connection to git repo whenever I open GitBash without having to constantly input my passphrase. I know putting passphrase in a script is terrible security, but I really don't care. I'm doing for testing.

like image 831
Sotirios Delimanolis Avatar asked Nov 25 '12 03:11

Sotirios Delimanolis


People also ask

How do I add a passphrase to SSH?

$ ssh-keygen -p -f ~/.ssh/id_ed25519 > Enter old passphrase: [Type old passphrase] > Key has comment '[email protected]' > Enter new passphrase (empty for no passphrase): [Type new passphrase] > Enter same passphrase again: [Repeat the new passphrase] > Your identification has been saved with the new passphrase.

Should you add passphrase for ssh key?

Using passphrases increases the security when you are using SSH keys. Using a key without a passphrase can be risky. If someone obtains a key (from a backup tape, or a one-time vulnerability) that doesn't include a passphrase, the remote account can be compromised.

Does SSH-Agent start automatically?

Starting ssh-agentOn most Linux systems, ssh-agent is automatically configured and run at login, and no additional actions are required to use it. However, an SSH key must still be created for the user. The ssh-agent command outputs commands to set certain environment variables in the shell.

How does SSH passphrase work?

SSH uses private/public key pairs to protect your communication with the server. SSH passphrases protect your private key from being used by someone who doesn't know the passphrase. Without a passphrase, anyone who gains access to your computer has the potential to copy your private key.


2 Answers

Answer how to do it is here:

https://ifireball.wordpress.com/2015/01/12/automatic-loading-of-ssh-keys-from-scripts/

Summary:

echo "exec cat" > ap-cat.sh
chmod a+x ap-cat.sh
export DISPLAY=1
echo $MY_SSH_PASS | SSH_ASKPASS=./ap-cat.sh ssh-add ~/.ssh/id_rsa
rm ap-cat.sh

Note: you need to export the DISPLAY environment variable to some value.

like image 159
axel22 Avatar answered Sep 28 '22 03:09

axel22


If you're going to do that you'd be better off just not using a passphrase on the key. In which case you wouldn't even need to use ssh-agent. You can change or remove a passphrase from an existing key with ssh-keygen -p.

like image 20
qqx Avatar answered Sep 28 '22 03:09

qqx