QEMU网络--端口重定向

要在用户模式网络下,允许网络连接到guest OS,可以将主机操作系统上的端口重定向到guest OS上的端口。 这对于支持guest OS里的文件共享、Web 服务器和 SSH 服务器很有用。(跟docker容器的端口重定向同样的应用场景)

 

以下是如何在用户模式网络下设置 QEMU来与 Windows XP guest共享文件和网页。 主机上的 TCP 端口 5555 被重定向到guest的端口 80(web服务器),主机上的 TCP 端口 5556 被重定向到guest的端口 445 (Windows 网络):

 

qemu -m 256 -hda disk.img -redir tcp:5555::80 -redir tcp:5556::445 &

...

mkdir -p /mnt/qemu

mount -t cifs //localhost/someshare /mnt/qemu -o user=test,pass=test,dom=workgroup,port=5556

firefox http://localhost:5555/

 

注意:当通过 Windows 网络从guest共享文件夹到主机时,您必须为 mount 登录时用的用户指定密码; 如果您尝试不使用密码,则挂载将失败并出现 I/O 错误。(这是CIFS协议的特性)

 

语法如下:

qemu -redir {tcp|udp}:host_port:[guest_ip]:guest_port

但是,貌似这个选项已经被取代了!改成-netdev user,hostfwd=

When using the '-netdev user,hostfwd=...' option, TCP or UDP connections can be redirected from the host to the guest. It allows for example to redirect X11, telnet or SSH connections.

当使用 '-netdev user,hostfwd=...' 选项时,TCP 或 UDP 连接可以从主机重定向到guest OS。 例如,它允许重定向 X11、telnet 或 SSH 连接。

注意: 不要将新的 hostfwd 与 -tftp -bootp -smb -redir选项同时使用, 混用会导致未定义的结果。

 

语法变成如下

hostfwd=[tcp|udp]:[hostaddr]:hostport-[guestaddr]:guestport

3ca9c1a812a347109b90926f56d40187.png

sudo qemu-system-x86_64 -m 2048 -enable-kvm ubuntu.img -net user,hostfwd=tcp:192.168.150.158:2333-:6666 -net nic

注记:使用 -net user 必须同 -net nic配合,否则如下

46d58e1a485d4549b95b7e68bb620a14.png

正确执行如下,将host的2333重定向到guest的6666端口。

bbb86dc4fd45428db58c66b5eb86ae8d.png

 接下来在guest中监听6666端口。

d28277b1b0484cdab52ef3d6a6d6383e.png

 在host中连接2333端口,并发送信息。

840feffda078415fab370dbde5d5ab71.png

 guest中成功收到了信息。

ab60f15e6cea4d2591e1dbe3f23e053b.png

猜你喜欢

转载自blog.csdn.net/m0_43406494/article/details/124828032