Method 1: Port Forwarding (The “Workaround”)
If your VM is behind NAT (IP addresses like 10.0.2.15), use Port Forwarding. This maps a port on your host (e.g., 2222) to the SSH port (22) on your VM.
1. Enable SSH on the Guest
Inside your VM (e.g., Rocky Linux, Ubuntu), ensure the SSH server is active:
- Install:
sudo dnf install openssh-server(Rocky/Fedora) orsudo apt install openssh-server(Ubuntu). - Start:
sudo systemctl enable --now sshd. - Firewall:
sudo firewall-cmd --permanent --add-service=ssh && sudo firewall-cmd --reload.
2. Map the Port via the Host
List Running VMs
virsh -c qemu:///session list --all
Start Vm if not started
virsh -c qemu:///session start VM_NAME
On an Arch Linux host using GNOME Boxes/KVM, run this command while the VM is running:
virsh -c qemu:///session qemu-monitor-command VM_NAME --pretty '{"execute": "human-monitor-command", "arguments": {"command-line": "hostfwd_add tcp::2222-:22"}}'
(Replace VM_NAME with your actual VM name).
3. Connect
Open your host terminal and type:
ssh username@localhost -p 2222
Method 2: Bridge Networking (The “Direct” Way)
Bridge networking makes the VM appear as a separate physical machine on your network, giving it a reachable IP address like 192.168.122.x.
1. Set the Network Source
- Open Virtual Machine Manager.
- Navigate to the VM’s NIC settings.
- Change Network source to
Virtual network 'default' : NATor a specific bridge likevirbr0.
2. Identify the IP
Inside the VM, run:
ip a
Look for the inet address (e.g., 192.168.122.50).
3. Connect
From your host terminal:
ssh username@192.168.122.50
Pro-Tip: Simplify with SSH Config
Instead of remembering ports and IP addresses, create an SSH alias.
- On your host, edit
~/.ssh/config:
Host myvm
HostName localhost
User inxeoz
Port 2222
- Now, simply type:
ssh myvm.