7.3. Connecting to a virtual machine by using SSH
You can interact with the terminal of a virtual machine (VM) by using the SSH connection protocol.
Prerequisites
- You have network connection and root access privileges to the target VM.
- If the target VM is located on a remote host, you also have connection and root access privileges to that host.
Your VM network assigns IP addresses by
dnsmasqgenerated bylibvirt. This is the case for example inlibvirtNAT networks.Notably, if your VM is using one of the following network configurations, you cannot connect to the VM by using SSH:
-
hostdevinterfaces - Direct interfaces
- Bridge interaces
-
The
libvirt-nsscomponent is installed and enabled on the VM’s host. If it is not, do the following:Install the
libvirt-nsspackage:# dnf install libvirt-nssEdit the
/etc/nsswitch.conffile and addlibvirt_guestto thehostsline:... passwd: compat shadow: compat group: compat hosts: files libvirt_guest dns ...
Procedure
When connecting to a remote VM, SSH into its physical host first. The following example demonstrates connecting to a host machine
192.0.2.1by using its root credentials:# ssh root@192.0.2.1 root@192.0.2.1's password: Last login: Mon Sep 24 12:05:36 2021 root~#Use the VM’s name and user access credentials to connect to it. For example, the following connects to to the
testguest1VM by using its root credentials:# ssh root@testguest1 root@testguest1's password: Last login: Wed Sep 12 12:05:36 2018 root~]#
Troubleshooting
If you do not know the VM’s name, you can list all VMs available on the host by using the
virsh list --allcommand:# virsh list --all Id Name State ---------------------------------------------------- 2 testguest1 running - testguest2 shut off