0

If I want to attach the network interface to the LXC we can specify it in the lxc config file as

lxc.network.type=phys
lxc.network.link=eth3
lxc.network.name=eth1

or we can run the below command as part of lxc hook to attach it during lxc-start

# on the host:
pid=$(lxc-info -pHn foobar)
ip link set dev eth3 netns $pid name eth1

Now I am looking for the option of removing or detaching the above attached phys interface from the container back to the host and how can I achieve it? I could not find anything in 'ip link help' option so far.

Viswesn
  • 4,674
  • 2
  • 28
  • 45

1 Answers1

1

If you have assigned a link to a network namespace like this:

ip link set dev eth3 netns $pid name eth1

Then you can simply re-assign that link to the global network namespace by running the ip link set ... command inside the target namespace, which you can do with the ip netns exec ... command, if you're using a "named" namespace. If not, you are probably better off using the nsenter command, like this:

nsenter -t $pid -n ip link set netns 1 eth1 name eth3

This takes advantage of the fact that PID 1 is pretty much guaranteed to be running in the global network namespace.

larsks
  • 277,717
  • 41
  • 399
  • 399