大网数据广告活动
服务器运维问答

Connect two QEMU VMs

· 52CE 我爱测

I created two QEMU VMs using Ubuntu cloud images . Then, I started them and enabled the ssh port forwarding using the following command: qemu-system-x86_64 -m 4G -smp 4 -hda ./ubuntu2404-vm0.img \ -device e1000,netdev=net0 -netde…

问题原文

原帖链接 · xiaogw · CC BY-SA 4.0

I created two QEMU VMs using Ubuntu cloud images. Then, I started them and enabled the ssh port forwarding using the following command:

qemu-system-x86_64 -m 4G -smp 4 -hda ./ubuntu2404-vm0.img \
      -device e1000,netdev=net0 -netdev user,id=net0,hostfwd=tcp::2222-:22 \
      -nographic -enable-kvm
qemu-system-x86_64 -m 4G -smp 4 -hda ./ubuntu2404-vm1.img \
      -device e1000,netdev=net0 -netdev user,id=net0,hostfwd=tcp::2223-:22 \
      -nographic -enable-kvm

Both VMs have an IP address of 10.0.2.15. Can I specify different IP addresses using cloud-init and connect these two VMs?

已采纳解答

原帖链接 · larsks · CC BY-SA 4.0

Can I specify different IP addresses using cloud-init and connect these two VMs?

You have a couple of options depending on exactly what sort of connectivity you need between the two virtual machines.

Option 1: Do nothing

You can already connect between the two virtual machines without making any changes. Because you're forwarding port 22 on each vm to a unique port on your host, you can connect from a vm to the appropriate host port to reach the other vm.

To reach your host, you can use the address of any interface on your host. For example, I have a system on which eth0 has address 192.168.1.123. If I run the same two qemu machines as in your question, then to reach the second vm from the first vm I can run:

ssh -p 2223 192.168.1.123

And in the other direction:

ssh -p 2222 192.168.1.123

Since you can ssh between these two virtual machines, that means you can set up ssh port forwarding between them to reach additional services.

Option 2: Set up a socket network

If you actually want regular networking between these two virtual machines, then set up a [socket}(https://wiki.qemu.org/Documentation/Networking#Socket) network instead of a user network. A socket network creates a virtual network between participating virtual machines. You might start you virtual machines like this:

qemu-system-x86_64 -m 4G -smp 4 -hda ./ubuntu2404-vm0.img \
  -device e1000,netdev=net0 -netdev user,id=net0,hostfwd=tcp::2222-:22 \
  -device e1000,netdev=net1 -netdev socket,id=net1,listen=:1234
  -nographic -enable-kvm
qemu-system-x86_64 -m 4G -smp 4 -hda ./ubuntu2404-vm0.img \
  -device e1000,netdev=net0 -netdev user,id=net0,hostfwd=tcp::2223-:22 \
  -device e1000,netdev=net1 -netdev socket,id=net1,connect=:1234
  -nographic -enable-kvm

This will give each vm two network interfaces -- eth0 will be a user network, which will give you the ability to forward host ports to internal services and to have external connectivity from your vms.

eth1 will be on the socket network. You will need to manually manage the IP address of this interface -- either by setting it by hand, or by running a dhcp server attached to the socket network.

Option 3: Use a tap network

A tap network is the most performant and most flexible network option, but it requires that you have root access on your host, and you will need to be familiar with creating and configuring Linux bridge devices.

This is the sort of network you get when working with libvirt.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2024-05-26)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。