8

I make use of an .ssh/config file to set my username appropriately based on a given portion of a subdomain, e.g.

Host *widgetshop.com*
   User foobar
   ControlMaster auto
   ControlPath ~/.ssh/socket-%r@%h:%p

Host *
   ControlMaster auto
   ControlPath ~/.ssh/socket-%r@%h:%p

This works if I write:

ssh foo.widgetshop.com

but does not work if I were to write:

ssh foo

which upon search path resolution becomes the same fqdn.

Anyone have a way to expand shortnames before calling ssh, or an alternative approach?

1
  • Does setting CanonicalizeHostname to yes solve the problem? It does not change how ssh is called but changes what part of the config is applied. Feb 27, 2016 at 19:41

4 Answers 4

5

Perhaps you can use the host command:

ssh $(host -t A  foo | cut -f1 -d" ")

Put that into a shell script (replace "foo" with "$1") and exec ssh.

2
  • This is what I will likely end up doing. Was just hoping someone had something a bit slicker than me doing an ssh alias. Thanks! Jan 15, 2014 at 14:31
  • Well, I actually make custom launchers with some part that looks like ssh [email protected].
    – Keith
    Jan 15, 2014 at 18:18
12

As of OpenSSH 6.5, you can do this with the CanonicalizeHostname option in ssh_config. Here's an example that should do what you want:

CanonicalizeHostname yes
CanonicalDomains widgetshop.com

Host *.widgetshop.com
     User foobar
1
  • 1
    This should be the accepted answer now. Works perfectly. Mar 29, 2023 at 19:54
1

The ssh_config manpage answers this:

The host is the hostname argument given on the command line (i.e. the name is not converted to a canonicalized host name before matching).

In other words, it doesn't work because it doesn't work that way.

1
  • Quite aware, thanks. Was hoping for a preexisting ssh alias/function/wrapper or the like. Jan 15, 2014 at 14:32
-1

please see my "alternative approach")

mbp:~ alexus$ grep f9 /etc/hosts
XX.XXX.XX.XXX   f9
mbp:~ alexus$ ssh f9 hostname
f9.XXX.org
mbp:~ alexus$ 
2
  • 2
    Don't think you understood the problem. I want the hostname fully expanded BEFORE calling ssh. This does not do so, nor would putting a million entries in /etc/hosts be a solution. Jan 15, 2014 at 14:33
  • 3
    Cramming stuff into /etc/hosts is almost UNIVERSALLY the wrong solution.
    – voretaq7
    Jan 15, 2014 at 18:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .