Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have a bunch of servers with the names,, ...,

I want to be able to connect to these servers by ssh srv1

I don't want to put 50 configuration entries to my .ssh/config such as

HOST srv1
USER amac
IdentityFile /home/amac/.ssh/id_rsa

What I like is a regular expression matching, which would allow me to write something like

HOSTNAME (srv[0-9]*)
USER amac
IdentityFile /home/amac/.ssh/id_rsa

Is this possible? Maybe by creating a bash alias which would convert all instances of "srv(.*)" to srv\, where \1 corresponds to the substring in the paranthesis matched by the regular expression?


share|improve this question
Why can't you simply add to your search path in your DNS resolver so that any request to resolve a name foo will result in the resolver trying – Zoredache Feb 23 '12 at 18:09

3 Answers 3

up vote 2 down vote accepted

This sounds like a problem best solved by DNS. Add this to your /etc/resolv.conf:


If a DNS lookup contains no dots1 or returns an NXDOMAIN response then another DNS lookup will be made with that search value appended.


If you do ssh srv1, the DNS lookup will be made for

If you do ssh srv1.dc1, the DNS lookup will be for srv1.dc1 which will return NXDOMAIN. The automatic followup DNS lookup will be for

You can add multiple search domains to that line separated by whitespace and they will be tried in the order listed until one of them returns an A record2.

1.) This value is configurable and refers to the number of dots the short name must have fewer than. The default value is 1 and it should be set higher than 1 for sites where the hosts are of the form This avoids the useless request to the root servers for the dc1 top level domain.

2.) Or an AAAA record.

share|improve this answer

This should do the trick on OpenSSH 5.5 and greater.

Host *.*
  Hostname %h

Host *
  IdentityFile /path/to/keyfile.pem

The first rule matches any normal domain names and just passes the domain name through. The second rule handles single word hosts and appends to the end.

One side effect of this is that even for ssh calls to, ssh will try to use the IdentityFile for I don't think that matters however.

share|improve this answer

openssh supports patterns (see, but not references to matches (AFAIK).

You can specify wildcards pretty much as you had it in the question:

Host srv*
  User amac
  IdentityFile /home/amac/.ssh/id_rsa

If you don't want to have to specify the full hostname, you can either do the dns trick mentioned before (and change the Host line to srv*) or create a shell function as follows:

ss() {
  if [ -z "$1" ]; then
    echo "specify a server #, eg ss 10"
    echo "ssh'ing to srv$"
    ssh srv$

and use it like:

ss 10

Easier than changing dns and doesn't cause problems when you want to hit

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.