$ 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 -A remote_server
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:
# Close all ControlMaster connections (ssh sockets)
# 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
for socket in $(ls ~/.ssh/master-*@*); do
echo Close socket: $socket
/usr/bin/ssh -o ControlPath=$socket -O exit blah
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
echo 'Start session remote_server if socket does not exist';
if [ ! -S $ssh_socket ]; then
echo "Creating SSH socket to remote_server"
/usr/bin/ssh -A -o ControlMaster=auto -M -N -f remote_server;
Sessions of ControlMaster are stored to ~/.ssh
That is why I need to close connections of ControlMaster when I close session and login again.