I start/stop lots of new instances as I'm learning to use Amazon EC2. Every temporary instance is added to the known_hosts file. Is this ever a problem for others who use EC2 a lot?

I'd like to tell ssh to skip this step anytime I connect to amazonaws.com. Is there a way to do that in the config? I'm using Linux & openssh.


This is done to prevent Man in the Middle attacks. Disabling it would disable basic functionality of the ssh tools.

You may want to keep a copy of your .ssh/known_hosts file without the entries and replace it when you are done.

  • You can create a simple bash function for these such as ecssh () { ssh $1; cp .ssh/saved_hosts .ssh/known_hosts } <-- That syntax isn't exact, but it shows the idea. – Doug Harris Jan 25 '11 at 15:30
  • Thanks. I believe for EC2 you're supposed to check the fingerprint with the instance's system log. – projectshave Jan 25 '11 at 21:39
  • Found the issue with my syntax. Needs a semicolon after the cp command: ecssh () { ssh $1; cp .ssh/saved_hosts .ssh/known_hosts; } – Doug Harris Jan 26 '11 at 16:28

Try this:

ssh -q -oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no -i $MYKEY $MYUSERNAME@$MYIP $MYCOMMAND

You can also do this in your config file:

Host *.amazonaws.com
  User root
  StrictHostKeyChecking no
  UserKnownHostsFile /dev/null
  LogLevel QUIET

I wrote a script that fixes this. It uses cloud-init pubkeys logged via the EC2 console output. (Which you actually noted in your comment is the only correct approach).

Basically, you tell it an instance name, and it finds that instance ID + IP, and makes a custom UserKnownHostsFile for it, using the pubkeys it finds in the console output, and launches SSH with the other arguments passed.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.