2

I have an instance on EC2 that I think I Stopped at one point or another. When I restart it and tried to ssh in, I got those messages about the host key having changed. But why?

Is it because the Elastic IP I'd associated with the instance had to be remapped to it?

Shouldn't the host key be generated once, and not be regenerated on the same host?

Ladlestein
  • 241
  • 3
  • 8

1 Answers1

0

The ssh key is never regenerated on the same host.

You are seeing this message because you previously had your Elastic IP connected to a completely different instance.

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • I don't see how I got mixed up here, but I must have. – Ladlestein Aug 03 '14 at 18:50
  • Actually, I just detached a root volume and attached it to another machine, to fix a dumb firewall mistake that locked me out. When I re-attached, the host keys had changed, as proven by the mtimes. A normal stop/start doesn't do that; I tested it. – Halfgaar Apr 22 '15 at 13:01
  • I just tested rebooting, and then stopping+restarting, an EC2 instance and the host key DID seem to change – but only for the (first) 'reboot' and not the second stop+start. I wonder if the key is regenerated on instance start because it had expired? Weird! – Kenny Evitt Aug 30 '22 at 21:24
  • 1
    [This answer](https://serverfault.com/a/1089348/40350) on a similar question claims that the AWS 'cloud-init' might be regenerating the SSH key when the (AWS internal) instance ID changes. – Kenny Evitt Aug 30 '22 at 21:29
  • I ran the command suggested in the answer to which I previously linked – `grep ssh /var/log/cloud-init.log` and found lines in the output for some `util.py` script that removed and then re-generated all of the SSH keys on the server. – Kenny Evitt Aug 30 '22 at 21:38
  • Seeing this as well. cloud-init.log shows old instance id being written to /var/lib/cloud/data/previous-instance-id, followed by key removal and regeneration: Attempting to remove /etc/ssh/ssh_host_rsa_key... – blindsnowmobile Sep 01 '22 at 20:42