端口转发到虚拟机 qemu

发布于 2024-12-11 16:32:38 字数 487 浏览 0 评论 0原文

我最近在 Ubuntu 11.10 下安装了一个虚拟机,现在,我假设它正在使用 NAT,其内部地址是 192.168.122.88。

我在虚拟机中设置了一个 Web 服务器,并且希望在访问 192.168.122.88 时能够访问它。然而,现在它超时了。当我登录到虚拟机并尝试访问本地主机时,它可以工作。

因此,由于某种原因,我的 iptables 阻止了从主机到虚拟机的流量(但反之则不然)。

如何允许流量从我的主机流向我的虚拟机,以便我可以从主机看到网络服务器?

我使用带有 KVM 和 libvirt 的 Ubuntu 虚拟机管理器。

我尝试做这样的事情

iptables -t nat -A PREROUTING -d 192.168.0.10 -p tcp --dport 80 -j DNAT --to-destination 192.168.122.88:80

但没有成功。显然它说没有路由到主机?

I recently installed a Virtual Machine under Ubuntu 11.10, Right now, I assume, it is using NAT and its internal address is 192.168.122.88.

I have setup a web server in my virtual machine and I want to be able to access it when I go to 192.168.122.88 . However, right now it times out. When I log in to the virtual machine and try to access localhost it works.

So, for some reason, my iptables is blocking traffic from the host to the virtual machine (But not the other way around).

How can I allow traffic to flow from my host to my vm so I can see the webserver from the host?

I used Ubuntu Virtual Machine Manager w/KVM and libvirt.

I tried doing someting like this

iptables -t nat -A PREROUTING -d 192.168.0.10 -p tcp --dport 80 -j DNAT --to-destination 192.168.122.88:80

with no avail. Apparently it says there is no route to host??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵你手 2024-12-18 16:32:38

“没有到主机的路由”意味着主机没有可以与您尝试访问的网络相匹配的 IP 地址(甚至没有默认路由),请确保主机上有两个网络。

例如:

$ ip route show  
default via 192.168.1.254 dev p3p1  src 192.168.1.103  
default via 172.16.128.1 dev p3p1  
169.254.0.0/16 dev p3p1  scope link  metric 1003  
172.16.128.0/17 dev p3p1  proto kernel  scope link  src 172.16.128.2  
192.168.1.0/24 dev p3p1  proto kernel  scope link  src 192.168.1.103

在 KVM 主机上,我将虚拟接口附加到某个网桥。例如:

<interface type='bridge'>     
  <mac address='01:02:03:04:05:06'/>       
  <source bridge='br4'/>    
  <target dev='vnet4'/>    
  <model type='virtio'/>    
  <alias name='net0'/>    
  <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>    
</interface>

然后,我为主机上的网桥分配一个 IP 地址,并将其设置为:

ip address add 192.168.0.1/24 dev br4    
ip link set up dev br4

在我的虚拟机上,我在子网上分配一些 IP 地址,如 192.168.0.2,然后它们之间的 ping 应该成功。

ping 192.168.0.1

'No route to host' means that the host machine doesn't have a IP address that can match the net you are trying to reach (you even don't have a default route), assure you have both nets on the host.

For example:

$ ip route show  
default via 192.168.1.254 dev p3p1  src 192.168.1.103  
default via 172.16.128.1 dev p3p1  
169.254.0.0/16 dev p3p1  scope link  metric 1003  
172.16.128.0/17 dev p3p1  proto kernel  scope link  src 172.16.128.2  
192.168.1.0/24 dev p3p1  proto kernel  scope link  src 192.168.1.103

On KVM host machines, I attach the virtual interfaces to some bridge. For example:

<interface type='bridge'>     
  <mac address='01:02:03:04:05:06'/>       
  <source bridge='br4'/>    
  <target dev='vnet4'/>    
  <model type='virtio'/>    
  <alias name='net0'/>    
  <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>    
</interface>

Then, I assign an IP address to the bridge on the host, and set it on up:

ip address add 192.168.0.1/24 dev br4    
ip link set up dev br4

On my virtual machine, I assign some IP address on the subnet like 192.168.0.2, then the ping should be successful between them.

ping 192.168.0.1
赏烟花じ飞满天 2024-12-18 16:32:38

也许您需要允许转发到虚拟机的连接。试试这个:

iptables -I FORWARD -m state -d 192.168.122.0/24 --state NEW,RELATED,ESTABLISHED -j ACCEPT

希望这有帮助。

Maybe you need to allow forwarded connections to the virtual machines. Try this:

iptables -I FORWARD -m state -d 192.168.122.0/24 --state NEW,RELATED,ESTABLISHED -j ACCEPT

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文