How to access Raspberry PI QEMU VM via network(转载自stackoverflow)

How to access Raspberry PI QEMU VM via network
Asked 7 years, 5 months ago
Active 2 years, 7 months ago
Viewed 17k times

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.
在这里插入图片描述
And here the content of my /etc/network/interfaces file
在这里插入图片描述
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
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?

asked Apr 16 '14 at 9:56

Andi
4672 2 gold badges 66 silver badges 20 20 bronze badges

How do you want to be able to access files and on which part (or all) of the filesystem? e.g. by using a web browser, or by FTP, or using scp, pr webdav, or (etc …) ? –
6EQUJ5
Apr 16 '14 at 12:00

SMB or FTP. I think the problem is the network configuration. I can’t reach (ping) the VM from my Mac Host. –
Andi
Apr 16 '14 at 17:22

related unix.stackexchange.com/questions/124681/… –
Ciro Santilli
Aug 27 '16 at 9:48
Add a comment
Start a bounty
2 Answers

14

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.

edited Apr 18 '14 at 11:39
answered Apr 17 '14 at 9:34

6EQUJ5
2,95611 gold badge1717 silver badges2727 bronze badges
Add a comment

1

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 -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).

edited Feb 6 '19 at 19:33
answered Oct 24 '14 at 12:39

Jeremy Davis
6211111 silver badges1414 bronze badges

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. –
Christoph Bimminger
Feb 6 '19 at 18:33

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. –
Jeremy Davis
Feb 6 '19 at 19:19
Add a comment

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值