如何向我的 Linux PC 添加虚拟 NIC,以便它们将其 MAC 公开给 ISP 的域?

如何向我的 Linux PC 添加虚拟 NIC,以便它们将其 MAC 公开给 ISP 的域?

我已经了解了如何在 VirtualBox 中向客户系统添加虚拟 NIC,只需使用Shared Adapter,然后所有虚拟 NIC 和我的主机的 NIC 都会作为真实 NIC 显示给 ISP。

因此,我猜一定有一种方法可以将这些虚拟网卡添加到我的电脑,而无需解析 VBox。我尝试了ip link add link enp0s3 type macvlan

ip netns add ns
ip link add link enp0s3 type macvlan
ip link set netns ns macvlan0
ip netns exec ns ip link set up macvlan0
ip netns exec ns ip addr add 192.168.222.101/24 dev macvlan0
ip addr add 192.168.222.1/24 dev enp0s3
ping 192.168.222.101
PING 192.168.222.101 (192.168.222.101) 56(84) bytes of data.
^C
--- 192.168.222.101 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 1999ms

我也尝试过ip link add link enp0s3 type macvlan mode bridgeip link add link enp0s3 type macvtap [mode bridge],但没有帮助。

答案1

最简单的解决方案可能是使用macvlans。类似的解决方案是苹果电脑虚拟局域网,与上面的只有微小的差别;如果没有更多的细节,很难选择其中一个而不是另一个。但下面的肯定有效。

对于每个网络命名空间,分配一个 macvlan 接口,如下所示:

 NNS=TheNameOfYourNetworkNamespace
 ip netns add $NNS
 ip link add link eth0 mac$NNS address 56:61:4f:7c:77:db type macvlan mode bridge
 ip link set dev mac$NNS netns $NNS

我使用了特定的 MAC 地址,address 56:61:4f:7c:77:db如果您不想指定它,您可以直接删除该部分,从而将其留给机会。

现在进入网络命名空间并配置网络:

 ip netns exec $NNS xterm

并从打开的 内部xterm执行以下操作:

 ip link set dev $NAME_OF_MAC_INTERFACE up
 ip link set dev lo up
 ip addr add 127.0.0.1 dev lo
 dhclient -v mac$NNS

您已经完成。

最后一点:上面使用的选项mode bridge允许所有网络命名空间直接相互通信,IE无需通过交换机。但是,无论你是否使用这样的选项,你的主持人将无法与网络命名空间通信,除非您为主机本身构建一个 macvlan,在这种情况下,它将能够与其他 NNS 进行通信,就好像它本身是其中之一一样。

编辑:

它不起作用,因为我应该记住,一旦进入 NNS,环境变量(如 $NNS)就不再定义,所以我应该更正上面的行。事实上,在我的系统上,

 # ip addr show dev eth0
   2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether e8:e0:b7:be:72:6a brd ff:ff:ff:ff:ff:ff
   inet 192.168.73.55/24 brd 192.168.73.255 scope global eth0
   valid_lft forever preferred_lft forever
   inet6 fe80::eae0:b7ff:febe:726a/64 scope link 
   valid_lft forever preferred_lft forever
  # NNS=ns1
  # ip netns add $NNS
  # ip link add link eth0 mac$NNS address 56:61:4f:7c:77:db type macvlan 
  # ip link set dev mac$NNS netns $NNS
  # ip netns exec $NNS /bin/bash
  # ip link set dev macns1 up
  #  ip link set dev lo up
  #  ip addr add 127.0.0.1 dev lo
  # dhclient -v macns1
    Internet Systems Consortium DHCP Client 4.2.4
    Copyright 2004-2012 Internet Systems Consortium.
    All rights reserved.
    For info, please visit https://www.isc.org/software/dhcp/

    Listening on LPF/macns1/56:61:4f:7c:77:db
    Sending on   LPF/macns1/56:61:4f:7c:77:db
    Sending on   Socket/fallback
    DHCPDISCOVER on macns1 to 255.255.255.255 port 67 interval 3 (xid=0x473d2846)
    DHCPREQUEST of 192.168.73.48 on macns1 to 255.255.255.255 port 67 (xid=0x46283d47)
    DHCPOFFER of 192.168.73.48 from 192.168.73.1
    DHCPACK of 192.168.73.48 from 192.168.73.1
    bound to 192.168.73.48 -- renewal in 21288 seconds.
  # ping -c1 superuser.com
    PING superuser.com (104.16.127.192) 56(84) bytes of data.
    64 bytes from 104.16.127.192: icmp_seq=1 ttl=53 time=12.9 ms

    --- superuser.com ping statistics ---
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 12.927/12.927/12.927/0.000 ms

如您所见,它在我的系统上运行良好。您可以检查我在 NNS 中获得的 IP 地址(192.168.73.48)与主机上的 IP 地址 192.168.73.55 不同。

如果它不能在你的系统上运行,我需要的不仅仅是这是行不通的,因为我无法重现您的错误。

相关内容