5

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

venix
  • 51
  • 1
  • 1
  • 3

2 Answers2

7

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

James O'Gorman
  • 5,249
  • 2
  • 23
  • 28
  • 1
    Also, to specify which key to use per host, add a line with `IdentityFile ~/.ssh/my_ec2_key.pem`. To specify default options for hosts not listed, start a block with `Host *`. – platforms Jul 25 '13 at 11:16
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.

Flup
  • 7,688
  • 1
  • 31
  • 43
  • 1
    Hmm but i don't have an ip address, and I can only used ec2-23-23-23-23.compute-1.amazonaws.com, is there an alternative approach to achieve the same thing? I want to create a 'shortcut' to ec2-23-23-23-23.compute-1.amazonaws.com so that instead of ssh -i key.pem user@ec2-23-23-23-23.compute-1.amazonaws.com to just ssh myServer2 or ssh -i key.pem myServer2 – venix Jul 25 '13 at 10:06
  • Try `host ec2-23-23-23-23.compute-1.amazonaws.com` -- that should give you the IP address. Alternatively if you don't have `host`, try `nslookup` or one of many websites that'll do the lookup for you. – Flup Jul 25 '13 at 10:07
  • To create a shortcut you could run a DDNS server somewhere that you can send dynamic updates to when your EC2 instances start. – Flup Jul 25 '13 at 10:08
  • Thanks for your help, but my bad, because I figure out that I could just use alias inside .bash_profile instead of going all that trouble. Thanks anw – venix Jul 25 '13 at 10:39