How to add a network interface in lxc?

When i create a lxc ,there is only one interface named eth0 , and I can see it as vethEKIFDFD in host server . I wanna add a interface named eth1, and can be see in host server ,too.
How to do it .

2 Likes

If it’s LXD, then you’ll want something like:

lxc config device add CONTAINER-NAME eth1 nic name=eth1 nictype=bridged parent=lxdbr0

If it’s LXC, then edit /var/lib/lxc/CONTAINER-NAME/config and duplicate the existing set of lxc.network entries for your second network interface (changed the device name and possibly the bridge).

5 Likes

It works ! thank you very much!

thank you very much ,but I have a new question.
if I create a container for the first time in host server ,there will be lxcbr0 and lxdbr0 bridge appear in host server ,and vet***1 will bridge in lxdbr0 auto.

if I create a new container ,there is no new bridge appear in the same time ,and the new vetch**2 will also bridge to lxdbr0 .

I want a new bridge will be created auto when I create a new container .

if I create a bridge by myself , is does not work rightly.

I need your help.

You’ll need to do something like:

  • lxc network create br-c1
  • lxc init ubuntu:16.04 c1
  • lxc config device add c1 eth0 nic nictype=bridged parent=br-c1 name=eth0
  • lxc start c1

So for every container you create, first create a new bridge, then create the container without starting it, then add its network device and then start it.

I do this and then got this
bridge name bridge id STP enabled interfaces
br-c1 8000.febe816a54d4 no vethXDTRTD

but there is no " lxc network create " in my lxc version .
so I use
brctl addbr br-c1
then I create a vxlan and bridge to br-c1
so,br-c1 have 2 member vxlan and vethXDTRTD( eth0 of lxc )

then i config a address of eth0 in lxc (1.1.1.1)
its remote peer vxlan also config as this one . remote address of lxc eth0 is 1.1.1.2
but I can not ping 1.1.1.2.

if I change the bridge both side to lxdbr0 .
and then I can ping 1.1.1.2 .

so ,I think maybe something wrong when I use bridge create by brctl.
it is not same with
lxc network create br-c1

but when I input lxc network create
there is no this command and give out help information
version is :
root@i-fhnacejl:~# lxc version
2.0.2

so …I still need your help

-------------------update 8.24 2017---------------
its version problem .when i got 2.16 lxc network create c1 works

thank you very much

Why doesn’t lxc config device add c1 eth0 add the network interface to /etc/network/interfaces inside container c1? What lxc command will do this?

LXD doesn’t alter files inside the container. It does however provide ways to expose that information to an agent in the container (like cloud-init) but that’s typically just for initial configuration and requires user involvement.

lxc config device add adds a device to a container, just like you’d get on a virtual machine or by adding a physical network card in a server. LXD doesn’t know (nor wants to) about the 50 different ways to then configure networking depending on the Linux distribution you’re running and what network management tool you prefer.

1 Like