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 :-

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

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 :-

sudo vim ~/.ssh/config

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

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

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

ssh server1
or
ssh server2
or
ssh server3

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

Tagged with:  

Leave a Reply

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 445 other subscribers

© 2011 Anuj Gakhar
%d bloggers like this: