Error on `git pull`: Permission denied (publickey). fatal: The remote end hung up unexpectedly

Sometimes my Devuan unexpectedly close the session, because of some JavaScripts on websites. After re-login I cannot connect to remote git repository with errors:

$ ssh remote_server
$ git pull
Error reading response length from authentication socket.
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

Some people advise to start ssh-agent and load keys to ssh-agent, but that does not work in my case:

eval $(ssh-agent -s)
ssh-add ~/.ssh/id_rsa
ssh -A remote_server
git pull

Note that often need to add option -A when connect to remote server via ssh for load your keys to remote server for use in git.

 

Close all connections of ssh ControlMaster

The issue is with ControlMaster, that keeps holding ssh connections and does not work properly after re-login. Need to close all connections of ControlMaster. I use function cmc in ~/.bashrc:

# cmc
#   Close all ControlMaster connections (ssh sockets)
#
# https://github.com/ClockworkNet/cmc
#
# this solves issue:
# $ ssh remote_server
# $ git pull
# Error reading response length from authentication socket.
# Permission denied (publickey).
# fatal: The remote end hung up unexpectedly
#
cmc() {
        for socket in $(ls  ~/.ssh/master-*@*); do
                echo Close socket: $socket
                /usr/bin/ssh -o ControlPath=$socket -O exit blah
        done;
}
 

That works! After that I can successfully connect to remote server via ssh and use git repository.

 

How do I use ControlMaster for better speed while connect to ssh

I use ControlMaster for better speed when I re-connect to remote server again and again. It makes connection for just 0.1 sec instead of 0.5 sec and that is very convenient.

        # Start ssh to remote_server session
        ssh_socket='/home/user/.ssh/master-user@remote_server:port'
        echo 'Start session remote_server if socket does not exist';
        if [ ! -S $ssh_socket ]; then
                sleep 3;
                echo "Creating SSH socket to remote_server"
                /usr/bin/ssh -A -o ControlMaster=auto -M -N -f remote_server;
                sleep 2;
        fi;

Sessions of ControlMaster are stored to ~/.ssh

That is why I need to close connections of ControlMaster when I close session and login again.

Section
Category