Using SSH Host shortnames to connect to Amazon EC2 servers

On December 9, 2011, in AWS, Mac, by Anuj Gakhar

I work with quite a few different Amazon EC2 instances on a daily basis and I use SSH command line interface to connect to these servers and as you can imagine, a normal command for that would look something like this :-

[xml]
ssh -i ~/.ec2/server.pem username@ec2-identifier.compute-1.amazonaws.com
[/xml]

Now, imagine typing that in for 5 different servers multiple times during the course of your day. Well, I have been doing that for a long time, until today, that is. Apparently, you can define the commonly used SSH hosts in the SSH config file.

First of all, create the SSH config file if you don’t have it already :-

[xml]
sudo vim ~/.ssh/config
[/xml]

Then, enter your commonly used hosts in that file :-

[xml]
Host server1
HostName ec2-identifier.compute-1.amazonaws.com
User ubuntu
IdentityFile ~/.ec2/server1.pem

Host server2
User root
IdentityFile ~/.ec2/server2.pem
HostName ec2-identifier.compute-1.amazonaws.com

Host server3
User ubuntu
IdentityFile ~/.ec2/server3.pem
HostName ec2-identifier.compute-1.amazonaws.com
[/xml]

You can add as many hosts as you want…and once you are done, you can SSH into these servers like this :-

[xml]
ssh server1
or
ssh server2
or
ssh server3
[/xml]

Isn’t that cool? I can see that saving me quite a lot of typing everyday.

Tagged with:  

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2011 Anuj Gakhar
%d bloggers like this: