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?

Andy Victors
  • 103
  • 1

2 Answers2

3

You didn't specify the port number in your command.

You didn't specify the alternate hostname you configured in your .ssh/config file in your command.

You must do at least one of these.


You'll also need to use a valid port number. The port numbers only go up to 65535.

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • Wait, I do not get it - I use gitlab.braemer.myds.me in both config and command, no? And also I assumed that specifying port in config is exactly for not having to specify it for each command - since I will use normal git commands on this server. – Andy Victors Oct 01 '20 at 17:32
  • @AndyVictors Yes, but you specified a different name in your configuration. That configuration is only used when you use the alternate name in your ssh command. If that's not what you want, then edit the config file to use the actual hostname. – Michael Hampton Oct 01 '20 at 17:34
  • Could you please point me to the difference between the names? I cannot currently see and I.m tying hard :/ – Andy Victors Oct 01 '20 at 17:46
  • @AndyVictors You specified a custom configuration for `gitlab.braemer`, which connects to the host `gitlab.braemer.myds.me`. But your ssh command tries to connect to `gitlab.braemer.myds.me`, which has no custom configuration. – Michael Hampton Oct 01 '20 at 17:48
  • Thanks, I will try. Me thinking is ok, test command is test command. I wonder how git will recognize proper server automatically. But I will try first. – Andy Victors Oct 01 '20 at 18:39
  • Well no, cannot confirm :/ I tried both with one name and another and same result - still using port 22! – Andy Victors Oct 01 '20 at 18:49
2

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.

Gordon Davisson
  • 11,216
  • 4
  • 28
  • 33
  • Thanks could be a valid point with default. I see in log also that rules from /etc/ssh/ssh_config for * are applied afterwards. however there is no port setting in that file – Andy Victors Oct 02 '20 at 06:29
  • I removed Host * / Port 22 and used "Host" name and it works now! – Andy Victors Oct 02 '20 at 06:32