如何向 LXC 容器添加网络接口

如何向 LXC 容器添加网络接口

我有一个关于 LXC 容器中网络接口的问题:在我的容器中,默认情况下我有这些接口:

ubuntu@u5:~$ ifconfig
eth0      Link encap:Ethernet  HWaddr 00:16:3e:b7:de:91 
          inet addr:10.0.3.138  Bcast:10.0.3.255  Mask:255.255.255.0
          inet6 addr: fe80::216:3eff:feb7:de91/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:56 errors:0 dropped:0 overruns:0 frame:0
          TX packets:40 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:7230 (7.2 KB)  TX bytes:3500 (3.5 KB)

       lo        Link encap:Local Loopback 
                 inet addr:127.0.0.1  Mask:255.0.0.0
                 inet6 addr: ::1/128 Scope:Host
                 UP LOOPBACK RUNNING  MTU:65536  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:0
                 RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

我想添加这个新界面:

      auto eth1

      iface eth1 inet static
      address 192.168.1.3
      netmask 255.255.255.0
      network 192.168.1.1
      broadcast 192.168.1.255

所以,我修改了这个文件:/etc/network/interfaces

     # This file describes the network interfaces available on your system
     # and how to activate them. For more information, see interfaces(5).

     # The loopback network interface
     auto lo
     iface lo inet loopback

     auto eth0
     iface eth0 inet dhcp

     auto eth1

     iface eth1 inet static
     address 192.168.1.3
     netmask 255.255.255.0
     network 192.168.1.1
     broadcast 192.168.1.255

我已经重新启动了,但没有用!当我使用 ifconfig 时,我找不到新界面:

      ubuntu@u5:/etc/network$ ifconfig
      eth0      Link encap:Ethernet  HWaddr 00:16:3e:b7:de:91 
                inet addr:10.0.3.138  Bcast:10.0.3.255  Mask:255.255.255.0
                inet6 addr: fe80::216:3eff:feb7:de91/64 Scope:Link
                UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
                RX packets:57 errors:0 dropped:0 overruns:0 frame:0
                TX packets:40 errors:0 dropped:0 overruns:0 carrier:0
                collisions:0 txqueuelen:1000
                RX bytes:7337 (7.3 KB)  TX bytes:3500 (3.5 KB)

       lo        Link encap:Local Loopback 
                 inet addr:127.0.0.1  Mask:255.0.0.0
                 inet6 addr: ::1/128 Scope:Host
                 UP LOOPBACK RUNNING  MTU:65536  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:0
                 RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

请问你有什么想法吗?

答案1

您需要在主机中修改容器配置文件,而不是在来宾中(您没有指定conf.文件是否在/etc/lxc或在〜/.config/lxc),添加与新界面相关的新节,如下所示:

 lxc.network.type = veth
 lxc.network.name = eth0
 lxc.network.link = br0
 lxc.network.ipv4 = 10.0.3.138/24
 lxc.network.flags = up

 lxc.network.type = veth
 lxc.network.link = br1
 lxc.network.ipv4 = 192.168.0.63/24
 lxc.network.name = eth1
 lxc.network.flags = up

其中第一节是您可能已经拥有的内容(提供或采用一些额外的选项),第二节复制不同子网中新接口已存在的内容。然后重新启动来宾,您就可以开始了。

相关内容