How to SSH from host to guest using QEMU?

I think that the error does not come from the -net statement, but from:

-chardev socket,host=localhost,port=7777,server,nowait,id=port1-char

The statement uses already the port 7777. For the port forwarding, with

-net user,hostfwd=tcp::7777-:8001

It works fine when not setting up the virtio serial channel.

If I understand right, you want to set up a virtio serial channel to communicate from the host to the VM using a Unix Domain Socket?

In this case, the following could do the job:

/usr/bin/qemu-system-x86_64 \
-m 1024 \
-name vserialtest \
-hda ubuntu1204 \
-chardev socket,path=/tmp/port1,server,nowait,id=port1-char \
-device virtio-serial \
-device virtserialport,id=port1,chardev=port1-char,name=org.fedoraproject.port.0 \
-net user,hostfwd=tcp::7777-:8001

An example of how to connect from the host using ssh to the VM:

-net user,hostfwd=tcp::10022-:22
-net nic

This host-forwarding maps the localhost (host) port 10022 to the port 22 on the VM. Once the VM was started like this, you can access it from the localhost as follows:

ssh vmuser@localhost -p10022

The -net nic command initializes a very basic virtual network interface card.


Try this on when launching qemu -redir tcp:2222::22

$ ssh -p 2222 localhost

The tcp:2222::22 flag in the qemu launch command maps the 2222 port of the host machine to port 22 (the default ssh port) on the virtual machine.

Then, simply sshing to the 2222 port on your localhost (the host machine) will redirect any traffic into the ssh 22 port in the virtual machine, which should allow you to ssh as you normally would any other machine.


OpenSSH configuration tested on Buildroot 2016.05, QEMU 2.5.0, Ubuntu 16.04 host

Besides the QEMU network forwarding, you also need to setup SSH properly, which I'll cover here.

Start with qemu_x86_64_defconfig and enable the openssh package:

make qemu_x86_64_defconfig
echo 'BR2_PACKAGE_OPENSSH=y' >> .config
make BR2_JLEVEL=$(nproc)

Then start QEMU with:

qemu-system-x86_64 \
  -M pc \
  -append root=/dev/vda \
  -drive file=output/images/rootfs.ext2,if=virtio,format=raw \
  -enable-kvm \
  -kernel output/images/bzImage \
  -m 512 \
  -net nic,model=virtio \
  -net user,hostfwd=tcp::2222-:22

Then on guest:

vi /etc/ssh/sshd_config

Modify the following settings:

PermitRootLogin yes
PermitEmptyPasswords yes

And restart the server:

/etc/init.d/S50sshd restart

It is because this file exists that sshd starts by default, here is the source: https://github.com/buildroot/buildroot/blob/2018.02/package/openssh/S50sshd and the key startup operations are:

/usr/bin/ssh-keygen -A
/usr/sbin/sshd
touch /var/lock/sshd

Then from host:

ssh root@localhost -p 2222

In case of failure, first test that the networking forwarding is working with a lower level tool than sshd: e.g. nc -l as described here.

also check the server logs on guest:

less /var/log/messages

Then on the final system you should automate the creation of that log file with BR2_ROOTFS_OVERLAY or BR2_ROOTFS_POST_BUILD_SCRIPT: Customizing the generated target filesystem | buildroot.org