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:
Command line (CLI)
-
Login to Openshift server API using the assigned user account with
oc
command if not logged in.
{login_command}
-
Go to the assigned namespace-userx
oc project namespace-userx
-
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
-
Add the user SSH key to
exercise8
virtual machine usingvirtctl
virtctl credentials add-ssh-key \
-f /home/userx/.ssh/id_rsa.pub \
--user lab-user --force exercise8
-
the output will be.
Successfully added the key to secret "idrsa"
-
Get the pod name
PODNAME=$(oc get pods -l kubevirt.io/domain=exercise8 \
-o NAME -n namespace-userx)
-
Check the logs
oc logs $PODNAME -n namespace-userx -f --tail 10
-
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"}
-
Test the SSH access
virtctl ssh lab-user@exercise8