Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two sets of servers, one which starts with ab22 and one that starts with cd22. For ab22 hosts(ab22*), we have to first ssh to a host A1 and then to the specific host. For cd22 hosts(cd22*), we have to first ssh to host B1 and then to the specific host.

I want to ssh these hosts directly from my home directory.It should ssh the hostname that I provide.

So, if i provide ab22xyz, it shud go there and if i provide ab22lmn, it should go there.

From what i ve been able to find, I feel it will use the ~/.ssh/config file but I am not able to make these settings.

I cannot add the individual hosts in the config file since the number is huge.

Can someone please help me. I need to know if it's possible and if yes, how is it possible.

Please provide the entry to be made in host file.

(I have my keys setup for login to A1 and B1 but not to indiv servers)

Please help. Thanks

share|improve this question

Host takes not just names but patterns. Simply use ab22* and cd22* as your Host entries and configure accordingly.

share|improve this answer
ya, Host field does take patterns but it is the hostname field here that is the problem. So even if i put Host as ab22*, that means that when i type ab22*, I will be taken to that host, but how do i specify that particular hostname. – saurav Feb 23 '12 at 23:35
With ProxyCommand, as has been mentioned many times on this site. – Ignacio Vazquez-Abrams Feb 23 '12 at 23:37
Thanks for the help but can you please provide me the entry that should be made in order to solve this problem. This is the first time I am trying to do anything like this and have been googling for hours but am not getting how to achieve it. – saurav Feb 23 '12 at 23:39

There are a few things you must establish before getting everything set up. The first being that there is no easy way to "hop" between machines without trickery to use your local key and authenticate on the other end (eg, port forwarding from ssh, the -L flag). The second, that you cannot really instruct SSH to go to one machine and then connect to another one.

A common name for gateway machines to log in into infrastructure is jumpboxes. I would suggest you generate private keys both on host A1 and B1 and then distribute their public keys appropriately amongst ab22* and cd22*. That is a better practice and should a person in your team decide to quit, you have only two places to remove their keys from instead of hundreds of potential servers.

What I suggest is writing a tiny script that wraps the ssh binary, sshes into the jumpbox for the appropriate class of hosts (ab22* and cd22* accordingly) and then sshes into the end machine you are trying to reach.

share|improve this answer

You could use a shell function that looks for the ab22* and cd22* pattern in the ssh arguments and does the right thing. Assuming you use sh, bash or ksh:

myssh () {
   case "$@" in
      ab22*) relay=A1 ;;
      cd22*) relay=B1 ;;
      *) relay="" ;;
   if test -z "$relay"
      ssh $@
      ssh -t $relay ssh $@

This function if put into your .profile will provide you with a myssh command that does an ssh to A1 or B1 as needed before doing the ssh to the final destination.

share|improve this answer
Hey Thanks a lot, I think that will work. But again I am facing a problem. My home dir is named as saurav, so it is taking username as saurav to login whereas my login username is sjoshi. I do not wish to change my home. Tried using -l sjoshi at the places u have used ssh but no luck. To be specific, I rewrote the line inside if construct as "ssh $@ -l sjoshi" and ssh -t $relay -l sjoshi ssh $@ "but it's not working at all and starts giving "nodename not defined". Can you please help and thanks again – saurav Feb 24 '12 at 0:30
Use ssh -l sjoshi -t $relay ssh $@. – Kyle Jones Feb 24 '12 at 1:05

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.