Akanksha gore
Akanksha gore

Reputation: 608

SSH key not working on git bash after closing and opening new

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

Answers (5)

Arifuzzaman Nishan
Arifuzzaman Nishan

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

Dennis Gonzales
Dennis Gonzales

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

Minu
Minu

Reputation: 3

You can follow the below steps if you have to add the key each time you start a new bash session.

  1. After generating the key and adding it to the .ssh/ folder(if you have doubt in generating the public and private keys you can refer to this link from git documentation [key-gen]).
  2. The Bash profile is a file on your computer that Bash runs every time a new Bash session is created hence you can start and add the key everytime by using this bash profile in your system.
  3. Edit .bash_profile of your git bash using the below script. Add this to your .bash_profile
    #!/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

harshp
harshp

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

Madhu Bhat
Madhu Bhat

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 :

  • Go to the location of Git installation (usually at C:\Program Files\Git\etc\ssh)
  • Edit the 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

Related Questions