13

I have an SSH config like the one below, which works great for:

ssh b21
ssh 23
ssh s267

Example .ssh/config (hostname passed is slotted in at %h):

host s*
    HostName atrcu%h.example.com
    User example
    Port 22
host b*
    HostName atrcx%h.example.com
    User example
    Port 22
host ??*
    HostName atvts%h.example.com
    User example
    Port 2205

but I'd like to include the username in the host:

ssh b21ex

which would ssh to:

[email protected]

but instead will:

atvts21ex.example.com

is their any way to cut/modify %h as it's passed and perhaps have the connection match more patterns to get a username along the way?

2 Answers 2

3

You can do what you describe in the examples with the match specification instead of host. It's another way to specify a host, or a set of hosts.

For example:

Match user u* host t*
  Hostname %hest.dev
  User %r

This will match against a user pattern and a target host pattern.

The command line will then be something like ssh u@t, resulting in this substitution: [email protected].

Here's a snippet from the ssh debug output:

debug2: checking match for 'user u* host t*' host t originally t
debug3: /Users/_/.ssh/config line 2: matched 'user "u"' 
debug3: /Users/_/.ssh/config line 2: matched 'host "t"' 
debug2: match found
...
debug1: Connecting to test.dev port 22.
debug1: Connection established.
...
[email protected]'s password:

match can match against a couple of other things (and there's an option to execute a shell command) but otherwise it's just like host. The ssh client options that go in there are the same ones (e.g. you can specify and IdentityFile that will be used with the matching spec)

You can read more in the man page: ssh_config

Sign up to request clarification or add additional context in comments.

2 Comments

Interestingly enough, when this question was originally asked that feature didn't exist: man.openbsd.org/OpenBSD-5.4/ssh_config
yep, I didn't know of it either, it's from 2014 apparently. I happen to check out the man page the other day for something else and found it. @RobC thanks for fixing my formatting
0

I think you will have to create separate HostName/User entries for each possible abbreviation match. You could then use %r to access separate identity files for each user. This would allow you to skip using user@host for login at the expense of creating a more complex configuration file.

You might have better luck writing a script or shell alias that unmunges your abbreviations and hands them to ssh ready to go.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.