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 / 问题 / 1005504
Accepted
RogueKnight
RogueKnight
Asked: 2020-03-04 21:42:26 +0800 CST2020-03-04 21:42:26 +0800 CST 2020-03-04 21:42:26 +0800 CST

Ubuntu 服务器 - 具有 Internet 访问权限的“虚拟”网络接口

  • 772

我有一个带有一个公共网络接口 (eth0) 的 Ubuntu 18.04 服务器。我正在尝试创建其他“虚拟”网络接口,这些接口也能够通过设置 NAT 规则访问互联网。

我使用以下命令创建了一个虚拟接口:

ip link add type veth

ifconfig veth0 192.168.1.1

下面是我的 ifconfig 的输出:

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 10.0.0.1  netmask 255.255.255.0  broadcast 10.0.0.255
    ether f2:3c:92:1f:2a:62  txqueuelen 1000  (Ethernet)
    RX packets 85664  bytes 111561237 (111.5 MB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 15392  bytes 2229468 (2.2 MB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
    inet 127.0.0.1  netmask 255.0.0.0
    loop  txqueuelen 1000  (Local Loopback)
    RX packets 1385  bytes 213213 (213.2 KB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 1385  bytes 213213 (213.2 KB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 192.168.1.1  netmask 255.255.255.0  broadcast 192.168.1.255
    ether a6:e7:de:40:9a:28  txqueuelen 1000  (Ethernet)
    RX packets 27  bytes 2082 (2.0 KB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 1132  bytes 48520 (48.5 KB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

我已将“/proc/sys/net/ipv4/ip_forward”设置为 1:

$ cat /proc/sys/net/ipv4/ip_forward

1

我还尝试了许多 iptables MASQUERADE、FORWARD 和 NAT 规则,但无法从 veth0 访问 Internet。

当我从 veth0 接口 ping 192.168.1.1 时,一切正常:

$ ping -I veth0 192.168.1.1

PING 192.168.1.1 (192.168.1.1) from 192.168.1.1 veth0: 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.029 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.046 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.085 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=0.062 ms
64 bytes from 192.168.1.1: icmp_seq=5 ttl=64 time=0.061 ms
--- 192.168.1.1 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4097ms
rtt min/avg/max/mdev = 0.029/0.056/0.085/0.020 ms

但是,如果我尝试从 veth0 ping eth0 接口 IP 地址,我没有得到任何响应:

$ ping -I veth0 10.0.0.1

PING 10.0.0.1 (10.0.0.1) from 192.168.1.1 veth0: 56(84) bytes of data.
--- 10.0.0.1 ping statistics ---
39 packets transmitted, 0 received, 100% packet loss, time 38900ms

下面是我的路由命令的输出:

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gw-li832.linode 0.0.0.0         UG    0      0        0 eth0
xx.xx.xx.xx     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 veth0

我似乎无法弄清楚我做错了什么,任何帮助将不胜感激。

ubuntu networking nat iptables virtual-network
  • 1 1 个回答
  • 315 Views

1 个回答

  • Voted
  1. Best Answer
    RogueKnight
    2020-03-05T23:31:19+08:002020-03-05T23:31:19+08:00

    我能够使用以下指南解决此问题:

    https://josephmuia.ca/2018-05-16-net-namespaces-veth-nat/

    基本上我必须创建两个 veth 接口对并将一个附加到一个新的网络命名空间。以下命令实现了这一点:

    首先,我启用了以下 IP 转发:

    echo 1 > /proc/sys/net/ipv4/ip_forward
    

    然后我创建了一个新的网络命名空间 (netns0) 并为其附加了一个新的环回适配器。如果没有这一步,它将无法工作:

    ip netns add netns0
    ip netns exec netns0 ip link set lo up
    

    然后我创建了两个 veth 对。这些基本上用作以太网线路的两端。一端留在主网络上,另一端连接到网络命名空间:

    ip link add veth0a type veth peer name veth0b
    ip link set veth0b netns netns0
    

    然后我为对中的每个接口设置 IP 地址并启用它们:

    ip addr add 192.168.0.1/24 dev veth0a
    ip netns exec netns0 ip addr add 192.168.0.2/24 dev veth0b
    ip link set veth0a up
    ip netns exec netns0 ip link set veth0b up
    

    以下 iptable 规则启用了转发和分类:

    iptables -A FORWARD -o eth0 -i veth0a -j ACCEPT
    iptables -A FORWARD -i eth0 -o veth0a -j ACCEPT
    iptables -t nat -A POSTROUTING -s 192.168.0.2/24 -o eth0 -j MASQUERADE
    

    以下命令为命名空间设置默认路由。这对于与主网络进行通信至关重要:

    ip netns exec netns0 ip route add default via 192.168.0.1
    

    最后,我使用我的 DNS 服务器创建了一个特定于命名空间的 resolv.conf 文件。这对于来自网络命名空间的域名解析至关重要:

    mkdir -p /etc/netns/netns0
    echo "nameserver 1.1.1.1" > /etc/netns/netns0/resolv.conf
    

    在这些步骤之后,我可以使用网络命名空间与 Internet 进行通信:

    $ ip netns exec netns0 ping google.com
    
    PING google.com (172.217.10.238) 56(84) bytes of data.
    64 bytes from lga25s59-in-f14.1e100.net (172.217.10.238): icmp_seq=1 ttl=57 time=2.50 ms
    64 bytes from lga25s59-in-f14.1e100.net (172.217.10.238): icmp_seq=2 ttl=57 time=1.44 ms
    64 bytes from lga25s59-in-f14.1e100.net (172.217.10.238): icmp_seq=3 ttl=57 time=1.39 ms
    --- google.com ping statistics ---
    3 packets transmitted, 3 received, 0% packet loss, time 2003ms
    rtt min/avg/max/mdev = 1.392/1.779/2.505/0.514 ms
    

    使用“ip netns exec netns0 (COMMAND)”允许您使用此命名空间运行命令。

    • 0

相关问题

  • 无法通过 Ubuntu VPN 访问外部网络

  • ubuntu apt-get upgrade - 如何在 shell 中单击确定?

  • VirtualBox 上 Ubuntu 的访客优化技巧 [关闭]

  • 外部硬盘上的 virtualbox 虚拟硬盘驱动器(Vista 主机上的 ubuntu 客户机)

  • 如何在 Ubuntu 上挂载 LVM 分区?

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