How to access Raspberry PI QEMU VM via network

17,723

Solution 1

The easier method - less flexible but you don't need to muck around so much

They way you have configured qemu appears to be the default host NAT network (aka. qemu -net nic -net user configuration)

By default, your host (your Mac in this case) will be found on 10.0.2.2 when accessed from your guest. From inside your guest you can connect to services on your host (your MAC) at 10.0.2.2. But this is using NAT, so you can't go back the other way as easily.

For example, if you decide to you want to connect to the SSH service and a web server running inside your guest, you will need to start qemu with modified options like the following:

qemu -net nic -net user,hostfwd=tcp::2222-:22,hostfwd=tcp::22280-:80

What this will do is route connections from port 22280 on your host (your MAC) to port 80 inside your qemu guest, (same for port 2222 --> port 22 in the guest)

Which means you can browse to http://localhost:22280 on your Mac, to get to the web server in your virtual Raspberry Pi, etc.

FTP and SMB will be more complicated to setup this way because of the different ports used and they way they are used, etc. although if you setup FTP to use PASV mode it wont be too much of a problem.

The more complicated method

This involves doing what you suggested, configuring the virtual machine quest to be able to get an IP address from your router. In this case, you need to make a bridge from your virtual machine onto your hosts network.

This requires a lot more setup than can be quickly explained here, but essentially, you need to assign your NIC to a vlan and add a tap interface, for example:

qemu -net nic,vlan=0 -net tap,ifname=tap0

This however requires more setup on the host (initially, manual, as you figure out your own situation, but then, scriptable) to create a bridge and tap interface - which usually requires root access beyond that needed to simply run qemu. A bit of Googling brings up a variety of methods to do this, because it varies more depending on your setup. (I found an example setup script here: https://gist.github.com/EmbeddedAndroid/6572715 )

  • Note - network MAC addresses, network card models, etc. and other qemu options omitted for clarity.

The SAMBA method

Note: I have only tried this under Linux

You can enable a samba server inside qemu:

qemu -smb /path/to/files

This creates a SMB share accessible from inside the guest at \10.0.2.4\qemu mapped from /path/to/files on the host.

Solution 2

If you are happy to use SFTP/SCP then IMO the easiest thing to do is to install openssh-server on your Pi guest:

sudo apt-get update && sudo apt-get install openssh-server

When you launch your Pi VM use the -redir switch when your launch your QEMU guest to redirect port 22 (SSH/SFTP/SCP) e.g.

qemu <other-qemu-switches-options-etc> -redir tcp:2222::22

You can then connect to your VM SFTP/SCP (or SSH) via port 2222 on your host machine. So from your host you can use localhost; from other machines on your LAN you can use :2222

[update 2019] As noted by @ChristophBimminger in a comment, in newer versions of QEMU the -redir switch has been deprecated/removed. The way to achieve this same thing is now via the -net or -netdev switches. Please check the man page, e.g. Debian testing (I don't currently have a QEMU running so can't give a definitive example).

Share:
17,723

Related videos on Youtube

Andi
Author by

Andi

Updated on August 02, 2022

Comments

  • Andi
    Andi over 1 year

    I have successfully setup a Raspberry PI VM on my Mac OS X via QEMU. Now I want to access the filesystem of this VM from my Mac.

    When I call ifconfig on my VM I get this. enter image description here

    And here the content of my /etc/network/interfaces file enter image description here

    On my Mac

    en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
        options=10b<RXCSUM,TXCSUM,VLAN_HWTAGGING,AV>
        ether 3c:07:54:65:da:50 
        inet6 fe80::3e07:54ff:fe65:da50%en0 prefixlen 64 scopeid 0x4 
        inet6 fdbf:a879:6730::3e07:54ff:fe65:da50 prefixlen 64 autoconf 
        inet6 fdbf:a879:6730::401e:56f5:f2f9:a236 prefixlen 64 autoconf temporary 
        inet 192.168.1.119 netmask 0xffffff00 broadcast 192.168.1.255
        nd6 options=1<PERFORMNUD>
        media: autoselect (1000baseT <full-duplex,flow-control>)
        status: active
    

    I think the reason is, that the VM uses some kind of Shared Network. Is there a way that the VM get the IP address from my router?

  • Christoph Bimminger
    Christoph Bimminger about 5 years
    The -redir option of QEMU was a parameter in elder versions of QEMU. Current versions (e.g. QEMU 3.x) require to use -net or -netdev and do no longer support the -redir parameter.
  • Jeremy Davis
    Jeremy Davis about 5 years
    Thanks @ChristophBimminger - I'll edit my answer to include your note. FWIW this answer is nearly 5 years old (and IIRC I was using Debian stable at the time), so would have definitely been a much older version of QEMU.