Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a bunch of servers with the names srv1.domain.com, srv2.domain.com, ..., srv50.domain.com

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
HOSTNAME srv1.domain.com
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]*).domain.com
HOST \1
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\1.domain.com, where \1 corresponds to the substring in the paranthesis matched by the regular expression?

Thanks.

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

3 Answers

up vote 2 down vote accepted

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

search domain.com

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

Examples:

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

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 srv1.dc1.domain.com.

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 srv1.dc1.domain.com. This avoids the useless request to the root servers for the dc1 top level domain.

2.) Or an AAAA record.

share|improve this answer
add comment

This should do the trick on OpenSSH 5.5 and greater.

Host *.*
  Hostname %h

Host *
  Hostname %h.domain.com
  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 domain.com to the end.

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

share|improve this answer
add comment

openssh supports patterns (see http://linux.die.net/man/5/ssh_config), but not references to matches (AFAIK).

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

Host srv*.domain.com
  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"
  else
    echo "ssh'ing to srv$1.domain.com"
    ssh srv$1.domain.com
  fi
}

and use it like:

ss 10

Easier than changing dns and doesn't cause problems when you want to hit srv01.someotherdomain.com.

share|improve this answer
add comment

Your Answer

 
discard

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.