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

Solution 1:

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).

Solution 2:

The format of lines in /etc/hosts is

<ip address>  <hostname> [<alias> ...]

so you have to specify an IP address as the first field. This is why your attempt to use the EC2 hostname didn't work.

You shouldn't need to do this anyway, because (if DNS resolution is working) the name will be resolved by a DNS lookup.