Recovering access to a CentOS Virtual Machine

If you lost your Key Pair, did not set a password for emergency access via VNC, or misconfigured the network and cannot connect to your CentOS Linux Virtual Machine (VM), this guide will help you restore access.

You need to perform the following steps to regain SSH access to your CentOS-based VM:

  1. First, you must set a password for the user centos. Note that the Cirros distribution is not suitable for CentOS recovery — use Ubuntu or Debian instead. (read more on how to reset a Linux password);
  2. After setting the password and exiting rescue mode using unrescue, use the VNC console to log in to the system using the username centos and the password you set;
  3. You can check the status of the SSH service with the commands: sudo systemctl status sshd or sudo journalctl -xe
  4. Usually, after recovery, the SSH server fails to start due to missing host keys. You need to regenerate them:

    sudo ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
    sudo ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
    sudo ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key

     
  5. If you encounter other errors, resolve them as well;
  6. Then run the command: sudo systemctl start sshd. You can verify that the SSH server is running: sudo systemctl status sshd
  7. We also recommend disabling SELinux.
  • Run sudo vi /etc/sysconfig/selinux and change enforcing to disabled, then save the changes.
  • Run: sudo getenforce
  • Reboot the VM.
Updated Date 04.12.2025