2

I'm using a config file for my OpenSSH client instead of defining options in command line whenever needed, sample from my config file:

$ cat ~/.ssh/config

---- TOP PARTS REDACTED HERE ----

Host hmnw
        Hostname hmnw.mydomain.com 
        User myname
        Port 60022
        IdentityFile ~/.ssh/id_rsa

When I try to ssh to this host:

$ ssh hmnw

It doesn't work.

But when I do

$ ssh -p 60022 hmnw

It goes through.

Out of curiosity, I did strace on this process:

$ ssh hmnw&
$ sudo strace -p $!
connect(3, {sa_family=AF_INET, sin_port=htons(22), sin_addr=inet_addr("XX.XX.XX.XX")}, 16

I understand it is trying to connect to default SSH port 22.

Note: Changed my actual hostname and IP address above.

What am I doing wrong?

My ssh version

$ ssh -V
OpenSSH_7.2p2 Ubuntu-4ubuntu2.1, OpenSSL 1.0.2g  1 Mar 2016

Adding this after @muru's suggestion

$ ssh -vvvv hmnw                                                            
OpenSSH_7.2p2 Ubuntu-4ubuntu2.1, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /home/myname/.ssh/config
debug1: /home/myname/.ssh/config line 2: Applying options for *
debug1: /home/myname/.ssh/config line 21: Applying options for hmnw
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "hmnw.mydomain.com" port 22
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to hmnw.mydomain.com [xx.xx.xx.xx] port 22.
5
  • Add the output of ssh -v hmnv please.
    – muru
    Oct 20, 2016 at 5:01
  • @muru Added -vvvv output. Thanks for reminder
    – user311189
    Oct 20, 2016 at 5:52
  • Methinks you are not telling us everything; especially when you say you provide a "sample" of your configuration file, you may have omitted something significant. Also, I am pretty certain you are not actually running the command ssh -p hmnw.
    – fkraiem
    Oct 20, 2016 at 5:57
  • @fkraiem my mistake I omitted port there
    – user311189
    Oct 20, 2016 at 5:59
  • @fkraiem This is how I log in manually
    – user311189
    Oct 20, 2016 at 6:06

2 Answers 2

3

I found the offender piece in configuration, it is my mistake based on an assumption:

Top of my ~/.ssh/config file

### default for all ##
Host *
     ForwardAgent no
     ForwardX11 no
     ForwardX11Trusted yes
     User myname
     Port 2222
     Protocol 2
     ServerAliveInterval 30
     ServerAliveCountMax 120

Port 2222 here was overriding Port 60022 below which belongs to hmnw.

I removed Port 2222 from Host * , now it works.

Update: As per @muru's suggestion, moving default config from top to bottom of config file allows me to use custom port entry for default *

@steeldriver's suggestion Host * !hmnw is also useful, I don't have a use case for it yet but I can see it would allow me to negate certain settings for particular host, if I needed to.

2
  • 4
    Keep the default Host * stanza at the end of the file.
    – muru
    Oct 20, 2016 at 6:36
  • 1
    IIRC you can use negations to exclude certain hosts from wildcard matches e.g. Host * !hmnw - see Stop ssh config on first match Oct 20, 2016 at 13:51
1

It seems that ssh (on ubuntu 16.04) doesn't not use ~/.ssh/config by default which contradict the guide on OpenSSH client configuration, saying that

Command-line options take precedence over configuration files. The user-specific configuration file ~/.ssh/config is used next. Finally, the global /etc/ssh/ssh_config file is used. The first obtained value for each configuration parameter will be used.

While the following two solutions still work:

  1. sudo ssh -F ~/.ssh/config the_name_after_Host
  2. put the configuration lines in file /etc/ssh/ssh_config (at the beginning of the file) instead of ~/.ssh/config

You must log in to answer this question.