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
    • 最新
    • 标签
主页 / user-88774

Karel Bílek's questions

Martin Hope
Karel Bílek
Asked: 2022-03-07 22:30:51 +0800 CST

通过网络命名空间内的网络连接连接到互联网,而不设置为默认路由

  • 0

我需要进行这样的设置(使用 Linux):

  • 我想要一个网络命名空间(假设weth_ns),其网络接口连接到互联网(weth0)
  • 我想在主机命名空间 ( )中使用不同的网络接口作为默认路由eth0
  • 我希望能够以某种方式使用类似的东西curl,通过某种接口,透明地使用该命名空间内的网络连接到互联网。

所以,像这样。

图表

我似乎无法让它工作。到目前为止我做了什么:

ip netns add weth_ns
ip link set weth0 netns weth_ns
ip netns exec weth_ns ip link set dev weth0 up
ip netns exec weth_ns ip a # to see list to make sure it's there
ip netns exec weth_ns dhclient weth0

ip netns exec weth_ns curl website.com # works as expected

这将设置命名空间并将接口放在那里。这使得 curl 在命名空间内工作。现在我想以某种方式使用来自主机的隔离命名空间连接。

我试图做的是建立一个网桥,并在命名空间内设置 iptables。所以我运行的是:

ip link add veth0_left type veth peer veth0_right
ip link add bridge0 type bridge
ip link set bridge0 up
ip addr add 10.9.0.0/24 dev bridge0 # 10.9.0.0 is address I came up with
ip link set veth0_left master bridge0 up 
ip link set dev veth0_right netns weth_ns
ip netns exec weth_ns ip link set dev veth0_right up
ip netns exec mobile_ns ip addr add 10.9.0.1/24 dev veth0_right

这增加了一个桥;我现在可以成功地从命名空间 ping 到命名空间。所以桥本身可以工作。

ping 10.9.0.1 # works
ip netns exec mobile_ns ping 10.9.0.0 # works

现在我需要设置 iptables 以使用命名空间内的连接。我怀疑这是我犯了某种错误的地方。这是我从各种在线资源中收集到的:

ip netns exec mobile_ns bash
# all following inside the namespace bash:
iptables -t nat -A POSTROUTING -s 10.9.0.0/255.255.255.0 -o weth0 -j MASQUERADE
iptables -A FORWARD -i weth0 -o veth0_right -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -o weth0 -i veth0_right -m state --state ESTABLISHED,RELATED -j ACCEPT

(所有涉及的网络都 /proc/sys/net/ipv4/conf/<network>/forwarding设置为 1。来自命名空间的内部和外部。)

而且......它不起作用。我的网络现在应该是这样的;但我无法通过 curl 连接。使用任一veth0_left或bridge0网络接口。

图2

我一定在某处做错了什么。


编辑:

指定什么是“不起作用”。

curl --interface bridge0 8.8.8.8
curl: (7) Failed to connect to 8.8.8.8 port 80: No route to host
curl --interface veth0_left 8.8.8.8
curl: (7) Failed to connect to 8.8.8.8 port 80: No route to host

当我尝试查看命名空间内的 tcpdump 时,我看到了who-hasARP 请求,但仅在网桥接口上。它似乎永远不会“进入” weth0 界面。

iptables linux-networking network-namespace
  • 1 个回答
  • 246 Views

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