What do I do if an SSH key is accidentally deleted or the virtual machine key is changed?

Symptom

The virtual machine cannot be accessed using a key pair.

Cause

The virtual machine key pair was lost or updated.

Solution

  1. Run the following command in the virtual machine to regenerate the key pair:

    [root@localhost ~]# ssh-keygen
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa):
    # Press Enter
    /root/.ssh/id_rsa already exists.
    Overwrite (y/n)?
    # Press Enter
  2. Run the following command to configure passwordless authentication and send the key to the target virtual machine:

    [root@localhost ~]# ssh-copy-id $IP

On this page