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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With