代码之家  ›  专栏  ›  技术社区  ›  Greg Rogers

如何在Linux中创建虚拟以太网设备?

  •  22
  • Greg Rogers  · 技术社区  · 15 年前

    我正在测试使用以太网(而不是IP)在两台计算机之间进行对话的协议的实现。为了不需要实际拥有两台物理计算机,我想创建两个虚拟以太网接口。它们只能互相通信,因此一个端点程序将绑定到一个接口,另一个端点将绑定到另一个接口。

    这有可能吗?我该怎么做?

    7 回复  |  直到 10 年前
        1
  •  21
  •   Tobu    15 年前

    您可以使用虚拟交换机vde2。

    例如(您需要一些术语):

    # Install vde2 (assumes Debian/Ubuntu)
    sudo aptitude install vde2
    # Create the switch and two tap interfaces attached to it
    sudo vde_switch -tap tap0 -tap tap1
    # Configure the interfaces
    sudo ip addr add 10.0.31.10 dev tap0
    sudo ip addr add 10.0.31.11 dev tap1
    # Start a server
    socat - TCP-LISTEN:4234,bind=10.0.31.10
    # Alternatively, an echo server:
    #socat PIPE TCP-LISTEN:4234,bind=10.0.31.10
    # Start a client
    socat - TCP:10.0.31.10:4234,bind=10.0.31.11
    

    在一侧键入,它将出现在另一侧。

        2
  •  6
  •   MarkR    15 年前

    您可以使用“tap”虚拟以太网驱动程序,它允许用户空间程序假装为以太网接口。现在这是一个标准的内核特性(但可能在内核中没有启用)。

        3
  •  2
  •   Andrew McGregor    15 年前

    如果需要,可以使用NS3模拟两个TAP设备之间的复杂网络: http://www.nsnam.org/

    我让它在两个虚拟箱实例之间模拟两个交换机、一个无线客户端和一个AP。

        4
  •  1
  •   Tiago Geada    14 年前

    人机接口 人IFCONFIG

    只需在/etc/network/interfaces中添加一个新的节

    我的示例配置:

    iface eth0 inet static
       address 192.168.2.150
       netmask 255.255.255.0
       network 192.168.2.0
       broadcast 192.168.2.255
       gateway 192.168.2.253
       # dns-* options are implemented by the resolvconf package, if installed
       dns-nameservers 8.8.4.4
    
    
    iface eth0:1 inet static
        address 192.168.2.2
        netmask 255.255.255.0
        network 192.168.2.0
        broadcast 192.168.2.255
        gateway 192.168.2.253
        # dns-* options are implemented by the resolvconf package, if installed
        dns-nameservers 8.8.4.4
    

    ——

    eth0的IP为192.168.2.150,而eth0:1的IP为192.168.2.2。

        5
  •  1
  •   Community CDub    8 年前

    如果你想要自己的子网,不想费心使用VDE。

    this . 简而言之:

    # tunctl -t eth0
    Set 'eth0' persistent and owned by uid 0
    # ifconfig eth0
    eth0      Link encap:Ethernet  HWaddr a6:9b:fe:d8:d9:5e  
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:500 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
    

    或与IP:

    # ip tuntap add dev eth0 mode tap
    # ip link ls dev eth0
      7: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 500
      link/ether 0e:55:9b:6f:57:6c brd ff:ff:ff:ff:ff:ff
    
        6
  •  0
  •   Uli Köhler    11 年前

    可以使用vconfig命令 例子:

    vconfig add eth0 10 #virtual interface eth0.10 will be created
    
        7
  •  0
  •   theCowardlyFrench    10 年前

    也许我遗漏了一些重要的东西……但这不正是环回(Lo)接口的作用吗?