大网数据广告活动
服务器运维问答

Unable to add interface to network namespace on boot

· 52CE 我爱测

I have problem with adding a phyisical ethernet interface to a predefined namespace , adding that to interfaces file in pre-up/post-up event do not work. It adds eth to namespace, but ip address and LOWER_UP state never appear. R…

问题原文

原帖链接 · Swift · CC BY-SA 4.0

I have problem with adding a phyisical ethernet interface to a predefined namespace , adding that to interfaces file in pre-up/post-up event do not work. It adds eth to namespace, but ip address and LOWER_UP state never appear. Removing namespace in down event doesnt work either, making restart of networking service impractical: it flags it as failed if namespace already exist, in result interface never comes to "up" state.

auto lo
iface lo inet loopback

#description of other interfaces

auto eth2
iface eth2 inet static
    address 196.10.10.200
    network 196.10.10.0
    gateway 196.10.10.100
    netmask 255.255.255.0
    pre-up ip netns add mynetns
    pre-up ip link set eth2 netns mynetns
    down ip netns delete mynetns

Purpose ofthis is to keep a daemon software runningwhich works only in one interface present. The host contains multiple interfaces. Daemon can be relaunched and is created in upstart.

已采纳解答

原帖链接 · A.B · CC BY-SA 4.0

Overall OP's attempt will fail: even if the interface is put in the new network namespace, then the address assignment will be attempted in the initial namespace and fail. Only the manual method should be used, and everything configured with pre-up/up/down. Then it's simply juggling around ifupdown.

To avoid de-synchronization between interface state (including namespace existence) and ifupdown state because of previous manual attempts, I added a few || : to prevent failure. Those are not needed once everything is set correctly. As the manual method is still responsible to bring up the interface, a fake interface must still be created in place (but can be deleted later, see at the end of the answer).

I'm using modern syntax shortcut to configure in a namespace. In the past this might not have been available. If needed on a very old system, consider:

ip -n FOO BAR

to be equivalent to:

ip netns exec FOO ip BAR

and replace accordingly.

auto eth2
iface eth2 inet manual
    pre-up ip netns add mynetns || :
    pre-up ip link set eth2 netns mynetns || :
    pre-up ip link add eth2 type dummy || :
    pre-up ip -n mynetns link set eth2 up
    up ip -n mynetns address add 196.10.10.200/24 brd +
    up ip -n mynetns route add default via 196.10.10.100 dev eth2
    down ip link del eth2
    down ip -n mynetns link set eth2 netns 1 # prevents a rogue process to keep the interface "lost"
    down ip netns delete mynetns

With this in place (and in this test the real eth2 faked with a macvlan interface hence @if2):

# ifup eth2
# ip -br link show dev eth2
eth2             UNKNOWN        5a:4e:5c:64:98:92 <BROADCAST,NOARP,UP,LOWER_UP> 
# ip -n mynetns -br link show dev eth2
eth2@if2         UP             12:34:56:78:9a:bc <BROADCAST,MULTICAST,UP,LOWER_UP> 
# ip -n mynetns -br route
default via 196.10.10.100 dev eth2 
196.10.10.0/24 dev eth2 proto kernel scope link src 196.10.10.200 
# ifdown eth2
# ip -br link show dev eth2
eth2@enp2s0      DOWN           12:34:56:78:9a:bc <BROADCAST,MULTICAST> 

If that's an issue to keep a fake eth2 interface, simply replace:

    down ip link del eth2

with:

    up ip link del eth2

That way the dummy interface will be deleted right after its role is over: keep ifupdown happy when it wants to bring the interface up (and then there's no need to delete it later).

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-07-20)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。