Reputation: 608
Have create ssh key using following link (https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/) Once create ssh key using git bash and added to github. It works only for that particular git bash. If I close git bash and open again. Same ssh key wont work, need to create new. CAn anyone please tell me why is it happening? or what is missing?
Upvotes: 8
Views: 24204
Reputation: 169
you can add the following to your .bashrc or .zshrc file:
if ! pgrep -u "$USER" ssh-agent > /dev/null; then
eval $(ssh-agent -s)
fi
if [[ "$(ssh-add -l)" == "The agent has no identities." ]]; then
ssh-add ~/.ssh/your_key_file_name
fi
Upvotes: 0
Reputation: 111
In your ~/.ssh/config
file, add:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
Create the file if it doesn't exist.
Upvotes: 1
Reputation: 3
You can follow the below steps if you have to add the key each time you start a new bash session.
#!/bin/bash
eval `ssh-agent -s`
a=$?
if [ $a == 0 ]
then
ssh-add ~/.ssh/"<yoursshkeyfile>"
b=$?
if [ $b == 0 ]
then
echo "------------------key added-----------------"
else
echo "--------------key not added but agent started----------"
fi
else
echo "----------------agent not started---------------"
fi
This basically starts and adds your key everytime you open the bash.
eval ssh-agent -s
starts the agent and ssh-add ~/.ssh/"<yoursshkeyfile>"
(add the private key file and not the public) adds your key present in ./ssh directory. "$?" this is just to check the exit status of the command executed before. This returns 0 if executed without error.
Upvotes: 0
Reputation: 111
No matter what operating system version you run you need to run this command to complete setup:
$ ssh-add -K ~/.ssh/id_rsa
Upvotes: 4
Reputation: 15223
You would need to auto add the ssh key to every session when you open Git Bash. For that, if you are on Windows, follow the below steps :
C:\Program Files\Git\etc\ssh
)ssh_config
file and add the line IdentityFile Drive:\path\to\key
where Drive:\path\to\key
should specify the local path to your key that you have generated earlier, and save the file after editing.Now every time you open Git Bash, the key would automatically be added to the ssh session and you will not need to add the ssh key everytime.
Upvotes: 9