M
M
Maxim2018-06-04 13:00:16
git
Maxim, 2018-06-04 13:00:16

Why doesn't ssh-agent find the key on a new terminal session?

Good afternoon, I
add the ssh key Documentation bitbucket , at this stage everything is smooth and ssh works, but when I log into the server again (logout of the current terminal session) I get an error

$git ls-remote

repository access denied.
fatal: Could not read from remote repository.

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

note that eval 'ssh-agent' does not work
[email protected]:/home/b/frontend# eval 'ssh-agent'
SSH_AUTH_SOCK=/tmp/ssh-J4mjc2fCUI2r/agent.29034; export SSH_AUTH_SOCK;
SSH_AGENT_PID=29035; export SSH_AGENT_PID;
echo Agent pid 29035;

have to use
exec ssh-agent bash
How to solve this problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim, 2018-06-07
@maximkv25

Need to add bibucket host to ssh config

Host bitbucket.org
  HostName bitbucket.org
  IdentityFile ~/.ssh/id_rsa

Decision

A
Alexey Shumkin, 2018-06-07
@ashumkin

this happens because, on ssh-agentstartup, it gives the name of the socket, which is set to the environment variable SSH_AUTH_SOCK( ssh-agent bashmakes this task easier by issuing it in a form suitable for execution in bash/shell). They are "lost" when re-login. So, in order to connect to a previously running ssh-agent'u, you need to restore the path to its socket.
How to do it? For example, like this:
https://gist.github.com/martijnvermaat/8070533
Upd.

I did this to myself (in ~/.bash_functions)
function __ssh_agent_find_sockets() {
    local user=$(whoami)
    find /tmp/ssh-* -user "$user" -name agent\* -printf '%[email protected] %p\n' 2>/dev/null | sort -k 1nr | sed 's/^[^ ]* //'
}

function __ssh_agent_restore() {
    local found_socket=$(__ssh_agent_find_sockets | head -n 1)
    export SSH_AUTH_SOCK="$found_socket"
}

function __ssh_agent_socket() {
    # set up SSH agent socket symlink
    export SSH_AUTH_SOCK_LINK="/tmp/ssh-$USER/agent"
    if ! [ -r $(readlink -m $SSH_AUTH_SOCK_LINK) ] && [ -r $SSH_AUTH_SOCK ]
    then
        mkdir -p "$(dirname $SSH_AUTH_SOCK_LINK)" \
            && chmod go= "$(dirname $SSH_AUTH_SOCK_LINK)" \
            && ln -sfn $SSH_AUTH_SOCK $SSH_AUTH_SOCK_LINK
    fi
}
function __ssh_agent_run_do() {
    eval $(ssh-agent -s)
}

function __ssh_agent_run() {
    test -z "$SSH_AUTH_SOCK" && __ssh_agent_run_do
    __ssh_agent_socket
return
}

а в ~/.bashrc:
source ~/.bash_functions
...
__ssh_agent_run
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question