Regexp in .ssh/config

As gelraen stated default ssh_config won't support regular expressions but you can try something different like :

  • Advanced ssh-config on Github - it is said to have better regex engine but you have to test it on your own
  • some workarounds : another question on unix.stackexchange

You can use full regular expressions in your ssh_config.

The relevant documentation is IMO very hard to read and understand. I only understand it because I have 13+ years Linux experience and and 8+ years using ssh. So here is my summary of the documentation:

  • man ssh_config describes a feature called Match
  • then there is a feature called exec which let's you use an arbitrary shell command for determining a match
  • passing input parameters to the arbitrary shell command is possible and described in the section labeled TOKENS

In my case i ended up using in my ~/.ssh/config:

Match exec "echo %h | grep -q 'NAME[0-9]\+$'"
    User USER
    IdentityFile /path/to/identity/file
    Hostname %h.fully.qualified.domain.name

Testing must be done with something tricky like this. And to do that testing you call ssh -vvv HOSTNAME. Which will show exactly what is happening, and whether your new Match is implemented correctly.


Please, see PATTERNS section in man ssh_config.

Long answer is: No, you can't, unless you patch your ssh to support that.