Ssh – Modifying /etc/hosts to easily access a domain name (or ip address) for amazon EC2 instance

amazon ec2hostnamehosts-filessh

I previously added into my local(Mac OS x 10.6) /etc/hosts file my server's public ip something like this:

123.123.123.123 myServer

to allow me to ssh to myServer without having to remember the ip address of the server and worked fine (using shh public key) , like this:

ssh myServer1

However on Amazon EC2 instance I tried to do the same using the long public dns address provided and in /etc/hosts I added this:

ec2-23-23-23-23.compute-1.amazonaws.com myServer2 

but when I try to ssh myServer2 says that the 'hostname cannot be resolved'

any ideas why this is not working? How can I make this work ?

The complete command I want to use is:

ssh -i key.pem user@ec2-23-23-23-23.compute-1.amazonaws.com 

and I am trying to create a shortcut which might be something like this:

ssh -l username myServer2 or ssh -i key.pem -l username myServer2

any ideas?

UPDATE: I used alias instead which is easier:

Added this inside .bash_profile (MAC OS x)

alias myServer='ssh -i /path/to/key.pem user@ec2-23-23-23-23.compute-1.amazonaws.com' 

(need to close and open terminal again or use source ~/.bash_profile )
Then ssh with just using myServer on command line
$ myServer

Best Answer

Instead of editing your hosts file, read about the SSH client configuration.

You can create host aliases in ~/.ssh/config. For example:

Host myServer2
     HostName ec2-23-23-23-23.compute-1.amazonaws.com
     UserName username

If you're only using SSH this will work fine. If you need to access other services, either set up DNS, or use port forwarding in SSH (with -L).