bashgitsshpassphrase

Bash script don't take efect ater passphrase enter


I don't want to login automatically to ssh agent, but only effective execute simple script in 'sh' file:

#!/bin/bash
clear
echo " >> Start the ssh-agent in the background."
eval $(ssh-agent -s)
echo " >> Add SSH private key to the ssh-agent"
ssh-add ~/.ssh/id_rsa
echo " >> List of ssh agents"
ssh-add -l
echo " >> Attempts ssh to GitHub"
ssh -T git@github.com

it does trigger the password request and does wait for it to be entered, even not in home dir. git inform that 'Identity added:' and 'You've successfully authenticated'

but the problem is after try to communicate with Github - 'git push' or 'pull' command does not take any positive effect :

sign_and_send_pubkey: signing failed: agent refused operation
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists. 

after that issue I can type from keyboard the same commands ex.

ssh-add ~/.ssh/id_rsa

enter passphrase and then it enables me successfully communication with Github. What's wrong in the above script?

My context:

OS name: "linux", version: "4.15.0-76-generic", arch: "amd64", family: "unix"

Solution

  • ssh-agent works by setting a bunch of environment variables to tell your shell how to communicate with it. It prints them to STDOUT and eval $(ssh-agent -s) turns them into environment variables. The important one is SSH_AUTH_SOCK which points to the socket file used to communicate with the agent.

    $ echo $SSH_AUTH_SOCK
    /tmp/path/to/the/socket
    

    Environment variables only persist for the current process and its children. Your shell program is executed in a new process. Any environment variables set in your shell program die with the shell program. Your shell will not know how to speak to the agent.

    You have two choices.

    First, instead of executing your shell program, you can source it. This runs it as a series of shell commands in your current shell just as if you'd typed them. Environment variables which are set by the script will persist.

    Second, and better, is to start ssh-agent when you login. There's many ways to do this depending on your operating system. You might already have one running. Check $SSH_AUTH_SOCK.


    PS echo " >> List of ssh agents" should be echo " >> List of ssh keys"