Skip to main content
2 of 13
edited body
clearlight
  • 259
  • 2
  • 9

The following resolved this problem for me, fortunately, because I couldn't login to Raspian to run dpkg to reconfigure the keys within its own ssh distro.

If you happen to have a running Linux host (on a PC, as a guest VM running on VirtualBox or other Virtualization solutions) available that you can physically mount the Raspian media on, it's easy to reconfigure the SSH keys by overwriting them. You don't need to be booted into Raspian to do it, because your 'helper' Linux host should have ssh tools installed already.

HOW TO DO IT

Let's assume you've installed Raspian on a 16GB micro SSD card.

  1. Put that SSD into a USB flash card reader, and insert it into a USB port on your Linux helper host.

  2. On the Linux helper host, as root, run fdisk -l to find the device associated with your Raspian media. In particular, you want to find the device to which the Raspian Linux ext4 partition is assigned.

For example, in the following fdisk output the Linux partition is at /dev/sb2

# fdisk -l

Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x989b1246

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdb1        8192    98045    89854 43.9M  c W95 FAT32 (LBA)
/dev/sdb2       98304 30375935 30277632 14.4G 83 Linux
  1. Mount the Linux partition, as follows (we'll assume the device is /dev/sb2 and the mount point is /mnt through the rest of the example).

Note: Your actual device and chosen mount point may be different and you'll have to adjust this procedure accordingly.

# mount /dev/sb2 /mnt

  1. Now you can generate new keys to replace the old keys in your host ssh directory as follows:

     # ssh-keygen -f /mnt/etc/ssh/ssh_host_rsa_key -N '' -t rsa 
     # ssh-keygen -f /mnt/etc/ssh/ssh_host_dsa_key -N '' -t dsa
     # ssh-keygen -f /mnt/etc/ssh/ssh_host_ecdsa_key -N '' -t ecdsa -b 521
    
  2. Now eject the ssd card so you can safely physically remove it:

    # eject /mnt

Note: Eject will fail if any terminal windows are still cd'd into any Raspian partition directories, so either close any such windows or cd to a different directory.

clearlight
  • 259
  • 2
  • 9