1 个不稳定版本
0.1.0 | 2023年11月19日 |
---|
#56 在 #namespaces
20KB
336 行
网络命名空间工具
一条命令创建网络命名空间!
用法
Usage: netns [OPTIONS] --name <NAME>
Options:
-n, --name <NAME> Name of the network namespace
-i, --ifname <IFNAME> Interface name to enslave in the network namespace
-d, --destroy Destroy the network namespace
-h, --help Print help
-V, --version Print version
示例
- 创建包含'eno1'接口的网络命名空间'test'
guillaume@nix:~$ netns -n test -i enp5s0
Creating network namespace test
Joining network namespace test
guillaume@test:~$ ip link
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp5s0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether fc:aa:14:cb:e9:aa brd ff:ff:ff:ff:ff:ff
- 在另一个终端中连接网络命名空间
guillaume@nix:~$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
5: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether de:e8:6d:eb:72:30 brd ff:ff:ff:ff:ff:ff
guillaume@nix:~$ netns -n test
Joining network namespace test
guillaume@test:~$ ip link
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp5s0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether fc:aa:14:cb:e9:aa brd ff:ff:ff:ff:ff:ff
- 退出所有终端时简单地销毁网络命名空间
guillaume@test:~$ exit
exit
netns reference count: 1
guillaume@nix:~$
guillaume@test:~$ exit
exit
netns reference count: 0
Destroying network namespace test
NETNS Inode: 4026534538
guillaume@nix:~$
依赖项
~3.5MB
~72K SLoC