问题
通过SSH,使能agent forward后,远程登录Ubuntu Linux服务器后,运行git fetch
或者git pull
更新代码时报如下错误。
$ ssh -A user@server_name_or_ip
$ git fetch
ssh_exchange_identification: Connection closed by remote host
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
解决方法
加载正确的SSH key,确保能正常访问Linux Server和远程代码库。
我之所以会有这样的问题,是因为有两个SSH key:新的pub key放到Linux服务器上,但没有放到远程代码库的服务器上;旧的pub key没有放到Linux服务器上,但却放到了远程代码库的服务器上。如此这样,访问Linux服务器,需要新的key;访问代码,需要旧的key。也就是,必须两个key都加载了,才能在Linux服务器上使用agent forwarding来访问Git远程代码库。
SSH相关知识
.ssh/config中使能agent forward
Host server_name
HostName ip_or_server_name
User ubuntu
ProxyCommand ssh bastion-server -W %h:%p
# Enable forward agent
ForwardAgent yes
检测私钥是否加载
$ ssh-add -L
加载私钥
$ ssh-add -K
# 或者
$ ssh-add {path_of_key}
检测agent forwarding是否使能
$ echo "$SSH_AUTH_SOCK"
检测能否访问Git Server
$ ssh -T git@{git_server}