VS Code Remote-SSH 导致 Git Push 失败的问题

问题描述

今天在 VS Code 中使用 Git Push 时,突然出现以下报错:

1
2
3
4
5
6
> git push origin master:master
/Users/xxx/.ssh/config line 6: invalid time value.
/Users/xxx/.ssh/config line 7: integer value invalid.
/Users/xxx/.ssh/config line 8: unsupported option
/Users/xxx/.ssh/config: terminating, 3 bad configuration options
fatal: Could not read from remote repository.

原因分析

排查后发现,问题出在 ~/.ssh/config 配置文件中。

此前使用 VS Code Remote-SSH 插件自带的 Add New SSH Host 功能添加远程主机时,插件自动重写了 SSH 配置文件,导致部分配置项与行尾注释之间的空格丢失。

错误如下:

原本的配置是这样的:

1
2
3
4
Host *
ServerAliveInterval 60 # 每 60s 发一次心跳
ServerAliveCountMax 3 # 连续 3 次无响应才断开
AddKeysToAgent yes # 自动把密钥加入 ssh-agent

使用ssh插件添加主机之后,插件会自动重写.ssh/config文件,处理之后成了下面这种写法:

1
2
3
4
Host *
ServerAliveInterval 60# 每 60s 发一次心跳
ServerAliveCountMax 3# 连续 3 次无响应才断开
AddKeysToAgent yes# 自动把密钥加入 ssh-agent

由于参数值与 # 直接相连,OpenSSH 无法正确解析配置项,从而导致 SSH 初始化失败,进而影响 Git Push,而且ssh插件也无法使用。