大网数据广告活动
服务器运维问答

SSH does not take server specific port config on Mac

· 52CE 我爱测

I'm trying to connect to a GitLab instance with non-standard port from Mac. I've studied .ssh/config documentation and tried different options and I think my configuration is ok, but ssh still uses port 22. ssh -vv gitlab.braemer…

问题原文

原帖链接 · Andy Victors · CC BY-SA 4.0

I'm trying to connect to a GitLab instance with non-standard port from Mac. I've studied .ssh/config documentation and tried different options and I think my configuration is ok, but ssh still uses port 22.

ssh -vv gitlab.braemer.myds.me
OpenSSH_7.9p1, LibreSSL 2.7.3
debug1: Reading configuration data /Users/andrey/.ssh/config
debug1: /Users/andrey/.ssh/config line 1: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 48: Applying options for *
debug1: Connecting to gitlab.braemer.myds.me port 22.
ssh: connect to host gitlab.braemer.myds.me port 22: Connection refused

And the config is here:

cat ~/.ssh/config
Host *
  Port 22

Host github.com-irondad
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_2
  IdentitiesOnly yes

Host gitlab.braemer
  HostName gitlab.braemer.myds.me
  User git 
  IdentityFile ~/.ssh/id_rsa
  IdentitiesOnly yes
  Port 87111

Any ideas what is wrong?

已采纳解答

原帖链接 · Gordon Davisson · CC BY-SA 4.0

In addition to the problems Michael Hampton pointed out, when the config file has multiple applicable declarations for the same parameter, the first one is used. Since the Port 22 declaration in the Host * section comes before the Port 87111 declaration in the Host gitlab.braemer section, the Port 22 declaration will always take precedence. From the ssh_config man page (emphasis added):

For each parameter, the first obtained value will be used. The configuration files contain sections separated by ''Host'' specifications, and that section is only applied for hosts that match one of the patterns given in the specification. The matched host name is the one given on the command line.

Since the first obtained value for each parameter is used, more host-specific declarations should be given near the beginning of the file, and general defaults at the end.

So if you want to include default parameter settings in a Host * section, you should put it at the end of the file. Or in this case, just leave it off, since port 22 is the default anyway.

For completeness, the problems Michael Hampton pointed out are that for declarations in the config file to apply, you must use the name in the Host section header, not the one in the HostName declaration, and that port numbers only go up to 65535.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-10-01)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。