2

I started arch linux in a qemu vm with guest port 22 connected to host port 60022, set the root password in the guest, and edited the sshd config in the guest to allow root logins and set the log level to debug. Then I started sshd on the guest and tried to log into the guest from the host using ssh [email protected] -p 60022 -vvv.

Nothing happens.

journalctl -xe on the guest reveals sshd is started and listening on guest port 22, but does not say anything at all about an attempted connection. On the host, -vvv gives a lot of output, but it clearly tells me it connects to the guest, lists its id files and version, and then waits two minutes before saying kex_exchange_identification: read: Connection reset by peer and giving up. Again, the guest journalctl doesn't report any connection attempt.

I can ping external websites from the guest, so I know the internet is set up correctly. I have no idea what the purpose of the abomination journalctl is when log files already existed.

ssh immediately fails if I try to connect to port 60021 or any port besides 60022, instead of waiting 2 minutes, plus on 60022 it shows "Connection established" in the -vvv output, so I know the host is probably connecting to the guest. But the guest sshd is not working very hard because it won't even immediately reject usernames besides root that don't exist.

My full qemu command is qemu-system-x86_64 -netdev user,id=n0 -device rtl8139,netdev=n0 -enable-kvm -machine q35,accel=kvm -device intel-iommu -cpu host,hv_relaxed,hv_spinlocks=0x1fff,hv_vapic,hv_time -nic user,hostfwd=tcp::60022-:22 -m 8G -smp 2 -cdrom ~/Downloads/archlinux-2020.09.01-x86_64.iso ~/vmimgs/lfs.qcow2

How can I ssh into my virtual machine? (See also this question from about 4 years ago where a user had the same problem but with virtualbox and no solution was given.)

1 Answer 1

1

It turns out that, even though many sources use multiple networking options to qemu, they can step on each other's toes. Switching to just -nic user,id=vmnic,hostfwd=tcp::60022-:22 works perfectly, allowing BOTH passing through the tcp connection on that port for ssh AND using the internet from the guest os. Notice this has id=vmnic. id=n0 or anything else works now because this is the only option that references an id. hostfwd=tcp:127.0.0.1:60022-:22 also works. :: is localhost in ipv6 and 127.0.0.1 is in ipv4, but either should work.

My full command is now qemu-system-x86_64 -bios /usr/share/ovmf/x64/OVMF.fd -enable-kvm -machine q35,accel=kvm -device intel-iommu -cpu host,hv_relaxed,hv_spinlocks=0x1fff,hv_vapic,hv_time -nic user,id=vmnic,hostfwd=tcp::60022-:22 -m 8G -smp 2 ~/vmimgs/lfs.qcow2. The added -bios option and removed -cdrom option are not directly important to sshing into the guest, however booting from a qcow instead of an iso lets ssh configuration like run on boot or user public keys be persisted.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .