Add a new SSH key to a virtual machine

Scenario

In this scenario, an RHEL 9-based Virtual Machine (VM) named exercise8 is currently in the Running state. The team has identified that the SSH key created by this VM has been lost. To regain SSH access, a new SSH key must be added to the VM.

Constraints

  • Access to the VM via the console is not possible because the user lab-user does not have password defined.

  • The new SSH key to be added is located at /home/userx/.ssh/id_rsa.pub.

Task

Perform the necessary steps to add the specified SSH key to the exercise8 VM to restore remote access.

The primary goal is to get access to the exercise8 via SSH virtual machine restored.
Use virtctl ssh lab-user@exercise8 command to test the access.

The steps to fix exercise8 are:

Console

To solve this exercise, the command line (CLI) is recommended

Command line (CLI)

  1. Login to Openshift server API using the assigned user account with oc command if not logged in.

OpenShift login command
{login_command}
  1. Go to the assigned namespace-userx

oc project namespace-userx
  1. Check that ssh is not working

virtctl ssh lab-user@exercise8
Warning: Permanently added 'vmi/exercise8.namespace-userx' (ED25519) to the list of known hosts.
lab-user@vmi/exercise8.namespace-userx: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
exit status 255
  1. Add the user SSH key to exercise8 virtual machine using virtctl

virtctl credentials add-ssh-key \
-f /home/userx/.ssh/id_rsa.pub \
--user lab-user --force exercise8
  1. the output will be.

 Successfully added the key to secret "idrsa"
  1. Get the pod name

PODNAME=$(oc get pods -l kubevirt.io/domain=exercise8 \
-o NAME -n namespace-userx)
  1. Check the logs

oc logs $PODNAME -n namespace-userx -f --tail 10
  1. and wait for the message on virtual machine pod

{"component":"virt-launcher","kind":"","level":"info","msg":"Reloading access credentials because secret changed","name":"exercise8","namespace":"namespace-{user}","pos":"access_credentials.go:448","timestamp":"2024-11-25T18:47:52.185346Z","uid":"728e56da-dff5-400d-8967-fe406e4ca72f"}
  1. Test the SSH access

virtctl ssh lab-user@exercise8

What you learned

In this exercise, you learned that you can use virtctl to add SSH keys to RHEL-9 virtual machines if dynamic credential support is enabled on the VM.