AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1148492
Accepted
Matija Sirk
Matija Sirk
Asked: 2023-11-24 18:01:17 +0800 CST2023-11-24 18:01:17 +0800 CST 2023-11-24 18:01:17 +0800 CST

使用 /etc/libvirt/hooks/qemu 进行端口转发后无法从虚拟机访问 deb.debian.org

  • 772

设置

在带有 Debian Bookworm 的主机服务器上,我通过 cockpit (kvm)创建了两个 VM(pserver 192.168.122.227 和pagent-1 192.168.122.126)。他们都运行 Debian Bullseye。我正在使用默认网络。

此时一切正常,我可以从其中任何一个访问网络。

由于我只有一个 IPv4 地址而无法使用网桥,因此我想将 80 和 443 端口从主机转发到pserver。所以我/etc/libvirt/hooks/qemu在主机上创建了:

#!/bin/bash

# Source: https://wiki.libvirt.org/Networking.html#Forwarding_Incoming_Connections

if [ "${1}" = "pserver" ]; then

   GUEST_IP=192.168.122.227

   if [ "${2}" = "stopped" ] || [ "${2}" = "reconnect" ]; then
    /sbin/iptables -D FORWARD -o virbr0 -p tcp -d $GUEST_IP --dport 80 -j ACCEPT
    /sbin/iptables -t nat -D PREROUTING -p tcp --dport 80 -j DNAT --to $GUEST_IP:80
    /sbin/iptables -D FORWARD -o virbr0 -p tcp -d $GUEST_IP --dport 443 -j ACCEPT
    /sbin/iptables -t nat -D PREROUTING -p tcp --dport 443 -j DNAT --to $GUEST_IP:443
   fi
   if [ "${2}" = "start" ] || [ "${2}" = "reconnect" ]; then
    /sbin/iptables -I FORWARD -o virbr0 -p tcp -d $GUEST_IP --dport 80 -j ACCEPT
    /sbin/iptables -t nat -I PREROUTING -p tcp --dport 80 -j DNAT --to $GUEST_IP:80
    /sbin/iptables -I FORWARD -o virbr0 -p tcp -d $GUEST_IP --dport 443 -j ACCEPT
    /sbin/iptables -t nat -I PREROUTING -p tcp --dport 443 -j DNAT --to $GUEST_IP:443
   fi
fi

我的问题

进行此更改后,从主机到两个虚拟机的 SSH 都可以工作,但在pserver和pagent-1sudo apt update上都失败,因为它无法访问. 在主机上一切正常。deb.debian.org

在pserver上:

sudo apt update
Err:1 http://deb.debian.org/debian bullseye InRelease
  Cannot initiate the connection to debian.map.fastlydns.net:80 (2a04:4e42:8e::644). - connect (101: Network is unreachable) Could not connect to debian.map.fastlydns.net:80 (146.75.122.132), connection timed out Cannot initiate the connection to deb.debian.org:80 (2a04:4e42:8e::644). - connect (101: Network is unreachable)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.
W: Failed to fetch http://deb.debian.org/debian/dists/bullseye/InRelease  Cannot initiate the connection to debian.map.fastlydns.net:80 (2a04:4e42:8e::644). - connect (101: Network is unreachable) Could not connect to debian.map.fastlydns.net:80 (146.75.122.132), connection timed out Cannot initiate the connection to deb.debian.org:80 (2a04:4e42:8e::644). - connect (101: Network is unreachable)
W: Some index files failed to download. They have been ignored, or old ones used instead.

我可以 ping 通它,但无法 ping 通它:

ping deb.debian.org
PING debian.map.fastlydns.net (146.75.122.132) 56(84) bytes of data.
64 bytes from 146.75.122.132 (146.75.122.132): icmp_seq=1 ttl=59 time=5.33 ms
64 bytes from 146.75.122.132 (146.75.122.132): icmp_seq=2 ttl=59 time=5.37 ms
wget http://deb.debian.org/debian/
--2023-11-24 10:52:59--  http://deb.debian.org/debian/
Resolving deb.debian.org (deb.debian.org)... 146.75.122.132, 2a04:4e42:8e::644
Connecting to deb.debian.org (deb.debian.org)|146.75.122.132|:80...

然后它就卡住了。

我是否不小心将 iptables 规则设置得太宽泛或者覆盖了某些 iptables 规则?

networking
  • 1 1 个回答
  • 45 Views

1 个回答

  • Voted
  1. Best Answer
    A.B
    2023-11-26T03:25:56+08:002023-11-26T03:25:56+08:00

    DNAT 规则不会过滤它们将适用的内容:因此它们适用于所有内容:Internet 发起的流量以及 VM 发起的流量。使用端口 80 或 443 的任何内容始终会重定向到 VM 192.168.122.227,包括来自该 VM 本身或其他 VM 的流量(主机本身不受影响,这需要 nat/OUTPUT 规则)。当从虚拟机使用 HTTP 或 HTTPS 时,这会产生循环(但会让 ICMP ping 正常工作,从而使诊断变得更加困难)。

    qemu hook 脚本中的四个 DNAT 规则应该修改,这样它们就不会影响 VM(还引入了 libvirt 桥名称的参数):

    ...
    
    GUEST_BRIDGE=virbr0
    
    ...
        /sbin/iptables -t nat -D PREROUTING ! -i "$GUEST_BRIDGE" -p tcp --dport 80 -j DNAT --to $GUEST_IP:80
    ...
        /sbin/iptables -t nat -D PREROUTING ! -i "$GUEST_BRIDGE" -p tcp --dport 443 -j DNAT --to $GUEST_IP:443
    
    ...
    
        /sbin/iptables -t nat -I PREROUTING ! -i "$GUEST_BRIDGE" -p tcp --dport 80 -j DNAT --to $GUEST_IP:80
    ...
        /sbin/iptables -t nat -I PREROUTING ! -i "$GUEST_BRIDGE" -p tcp --dport 443 -j DNAT --to $GUEST_IP:443
    
    ...
    

    当流量从网桥(即从虚拟机)发起时,该规则将不适用:虚拟机发起的流量不会更改,也不会环回虚拟机。规则集的其余部分可能"$GUEST_BRIDGE"也应该更改以重用。

    如果事先不完全知道桥接口或者不是默认接口virbr0(但为了简单起见仍然是一个桥),可以通过(安装和)从virsh xmldump提供给stdin上脚本的隐式 VM 属性中检索它使用xmlstarlet。将该行替换GUEST_BRIDGE为:

    GUEST_BRIDGE="$(xmlstarlet select -t -m 'domain/devices/interface' -v 'source/@bridge')"
    

    xmlstartlet的用法有点复杂,但甚至在这个libvirt 挂钩脚本示例中使用过。重要的是,VM 的属性(以 XML 格式)仅向stdin提供一次。如果检索其他参数,则必须在同一个镜头中完成。

    • 2

相关问题

  • 谁能指出我的 802.11n 范围扩展器?

  • 我怎样才能得到一个网站的IP地址?

  • 在一个 LAN 中使用两台 DHCP 服务器

  • 如何在 Linux 下监控每个进程的网络 I/O 使用情况?

  • 为本地网络中的名称解析添加自定义 dns 条目

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve