Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

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

in .ssh/config file

Scenario is that I have two public ips now (dynamic), one is connect to aws and one is connect to rackspace

Both require different pem keys to connect. But I want to use ssh config file to save my time

Host <ip1> 
User root
Port 22
HostName %h
IdentityFile ~/.ssh/aws.pem

Host <ip2> 
User root
Port 22
HostName %h
IdentityFile ~/.ssh/rackspace.pem

From this setting, i cannot distinguish which pem file to be use. I want to make use of the Host as part of parameter to specify which server to connect to e.g. ssh aws-ip1 ssh rack-ip2

then the config file can distinguish which pem file to use

Anyone know how to do this?

share|improve this question

Ok... This question is very old. Yet I can think of a way that will help in these kind of scenarios. Using wildcard(*) and %h parameters here.

Let's assume Amazon's IP as 10.10.20.* and Rackspace IP as 20.20.10.*. The following config will help:

Host 10.10.20.*
    Hostname %h
    User root
    IdentityFile ~/.ssh/aws.pem

Host 20.20.10.*
    Hostname %h
    User root
    IdentityFile ~/.ssh/rackspace.pem

And then calling, say ssh 10.10.20.5 will call something like ssh -i ~/.ssh/aws.pem root@10.10.20.5, which is what might just be required.

share|improve this answer

The 'Host' part is kind of 'shortcut' you can use:

Host aws-ip1
    HostName ip-or-name-to-ip1
    IdentityFile ~/.ssh/aws.pem

Host rack-ip2
    HostName ip-or-name-to-ip2
    IdentityFile ~/.ssh/rackspace.pem

and then you just use ssh aws-ip1 or ssh rack-ip2 ...

To quote from man ssh-config:

Host    Restricts the following declarations (up to the next Host key-
        word) to be only for those hosts that match one of the patterns
        given after the keyword.
share|improve this answer
    
Thx: but how can I pass ip1, ip2 to hostname? – Kit Ho Nov 3 '12 at 11:49
    
you write the ip-address behind the "Hostname" statement. maybe you did not asked exactly what you wanted. do you need a group of hosts having the same setting? – akira Nov 3 '12 at 11:51
    
then this would be hardcodeded, my question is that how can I pass the parameter using Host, both IP address are dynamically, I want sth that we can pass parameter into HostName – Kit Ho Nov 3 '12 at 11:54
    
well, you might go with HostName %h in .ssh/config and then you might use ssh aws-ip1 -o HostName real_ip – akira Nov 3 '12 at 11:56
    
Do you mind describe more how to use %h? If %h is accepted from "Host", then I cannot distinguish aws or rackspace... – Kit Ho Nov 3 '12 at 12:00

Another option is to use plain old-school shell aliases:

 alias ssh_rack="ssh -i .ssh/rackspace.pem"
 alias ssh_aws="ssh -i .ssh/aws.pem"

and then use them instead of ssh.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .