网络命令配置

随笔记录

目录

[1. 背景介绍](#1. 背景介绍)

[2. 配置网络命令空间](#2. 配置网络命令空间)

[3 验证](#3 验证)

[3.1 未网络命令空间外网卡配置IP](#3.1 未网络命令空间外网卡配置IP)

[3.2 验证配置](#3.2 验证配置)

[3.2.1 在网络命令空间外接口启动iperf3](#3.2.1 在网络命令空间外接口启动iperf3)

[3.2.2 网络命令空间内启动iperf3 client](#3.2.2 网络命令空间内启动iperf3 client)


1. 背景介绍

2. 配置网络命令空间

1. 配置前

[root@localhost SDK-V1.10.1.7]# ip netns exec netABC bash
Cannot open network namespace "netABC": No such file or directory
[root@localhost SDK-V1.10.1.7]# 


2. 创建网络命令空间  
# ip netns add <网络命令空间名>

[root@localhost SDK-V1.10.1.7]# 
[root@localhost SDK-V1.10.1.7]# ip netns add netABC  
[root@localhost SDK-V1.10.1.7]# 


3. 查看存在的网络命令空间
[root@localhost SDK-V1.10.1.7]# 
[root@localhost SDK-V1.10.1.7]# ip netns list    
netABC
[root@localhost SDK-V1.10.1.7]# 


4. 查询网络接口,并将接口添加到网络命令空间
# 查询网阔接口
[root@localhost SDK-V1.10.1.7]# 
[root@localhost SDK-V1.10.1.7]# ifconfig
enp5s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.1.22.73  netmask 255.255.255.0  broadcast 10.1.22.255
        inet6 fe80::5a11:22ff:fe9e:2888  prefixlen 64  scopeid 0x20<link>
        ether 58:11:22:9e:28:88  txqueuelen 1000  (Ethernet)
        RX packets 98650  bytes 112990563 (107.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 49070  bytes 4039655 (3.8 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 58  bytes 4946 (4.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 58  bytes 4946 (4.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

swift1f0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 32:74:4f:81:e0:e2  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 847  dropped 0  overruns 0  frame 847
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

swift1f1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether f6:38:ce:dd:9f:ed  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:56:07:08  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost SDK-V1.10.1.7]# 
[root@localhost SDK-V1.10.1.7]# 



5. 将指定网络接口添加到网络命令空间
# ip link set <网络接口> netns netABC

[root@localhost SDK-V1.10.1.7]# 
[root@localhost SDK-V1.10.1.7]# ip link set swift1f1 netns netABC
[root@localhost SDK-V1.10.1.7]# 
[root@localhost SDK-V1.10.1.7]# ip netns exec netABC bash
[root@localhost SDK-V1.10.1.7]# 
[root@localhost SDK-V1.10.1.7]# ifconfig
[root@localhost SDK-V1.10.1.7]# 
[root@localhost SDK-V1.10.1.7]# ifconfig swift1f1 up
[root@localhost SDK-V1.10.1.7]# 
[root@localhost SDK-V1.10.1.7]# ifconfig
swift1f1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::f438:ceff:fedd:9fed  prefixlen 64  scopeid 0x20<link>
        ether f6:38:ce:dd:9f:ed  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6  bytes 516 (516.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost SDK-V1.10.1.7]# 
root@localhost SDK-V1.10.1.7]# ip netns exec netABC bash
[root@localhost SDK-V1.10.1.7]# 
[root@localhost SDK-V1.10.1.7]# ifconfig
[root@localhost SDK-V1.10.1.7]# 
[root@localhost SDK-V1.10.1.7]# ifconfig swift1f1 up
[root@localhost SDK-V1.10.1.7]# 
[root@localhost SDK-V1.10.1.7]# ifconfig               # 分配IP 前
swift1f1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::f438:ceff:fedd:9fed  prefixlen 64  scopeid 0x20<link>
        ether f6:38:ce:dd:9f:ed  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6  bytes 516 (516.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost SDK-V1.10.1.7]# 

[root@localhost SDK-V1.10.1.7]# 
[root@localhost SDK-V1.10.1.7]# ip route     # 配置Route 前
[root@localhost SDK-V1.10.1.7]# 



6. 为网络命令空间中接口指定IP/Route
# 指定IP
# ifconfig <接口名> <IP/掩码>

root@localhost SDK-V1.10.1.7]# 
[root@localhost SDK-V1.10.1.7]# ifconfig swift1f1 11.1.1.77/24
[root@localhost SDK-V1.10.1.7]# 
[root@localhost SDK-V1.10.1.7]# ifconfig
swift1f1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 11.1.1.77  netmask 255.255.255.0  broadcast 11.1.1.255
        inet6 fe80::f438:ceff:fedd:9fed  prefixlen 64  scopeid 0x20<link>
        ether f6:38:ce:dd:9f:ed  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 15  bytes 1146 (1.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost SDK-V1.10.1.7]# 
[root@localhost SDK-V1.10.1.7]# ip route      # 添加Route
11.1.1.0/24 dev swift1f1 proto kernel scope link src 11.1.1.77 
[root@localhost SDK-V1.10.1.7]# 
[root@localhost SDK-V1.10.1.7]# 


7. 退出网络命令空间
[root@localhost SDK-V1.10.1.7]# 
[root@localhost SDK-V1.10.1.7]# exit
logout

Connection closed.

Disconnected from remote host(10.1.22.73-NDPP) at 10:47:57.

Type `help' to learn how to use Xshell prompt.
[C:\~]$ 


8. 删除网络命令空间
# ip netns delete <网络命令空间名称> 
[root@localhost ~]# 
[root@localhost ~]# ip netns delete netABC  //删除网络命令空间
[root@localhost ~]# 
[root@localhost ~]# ip netns list
[root@localhost ~]# 

注意:不进入网络命令空间也可以进行以上配置。

# 不进入网络命令空间,进行以上配置,只需要在执行命令前添加
# ip netns exec <网络命令空间名称> <需要执行命令>

比如查询 ip route
# ip netns exec netABC ip route

# 如 启动网络命令空间中接口
# ip netns exec netABC ifconfig swift1f1 up

等等。


[root@localhost SDK-V1.10.1.9]# ifconfig
enp5s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.1.22.73  netmask 255.255.255.0  broadcast 10.1.22.255
        inet6 fe80::5a11:22ff:fe9e:2888  prefixlen 64  scopeid 0x20<link>
        ether 58:11:22:9e:28:88  txqueuelen 1000  (Ethernet)
        RX packets 1182797  bytes 1628346524 (1.5 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 592699  bytes 123898394 (118.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 16755  bytes 381672557 (363.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16755  bytes 381672557 (363.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

swift1f0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 88:63:c5:ee:e2:52  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

swift1f1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether b2:c2:ae:e6:dc:47  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:56:07:08  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost SDK-V1.10.1.9]# 
[root@localhost SDK-V1.10.1.9]# 
[root@localhost SDK-V1.10.1.9]# ip link set swift1f1 netns netABC
[root@localhost SDK-V1.10.1.9]# ip netns exec netABC ifconfig
[root@localhost SDK-V1.10.1.9]# 
[root@localhost SDK-V1.10.1.9]# ip netns exec netABC ifconfig swift1f1 up
[root@localhost SDK-V1.10.1.9]# 
[root@localhost SDK-V1.10.1.9]# ip netns exec netABC ifconfig
swift1f1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::b0c2:aeff:fee6:dc47  prefixlen 64  scopeid 0x20<link>
        ether b2:c2:ae:e6:dc:47  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6  bytes 516 (516.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost SDK-V1.10.1.9]# ip netns exec netABC ifconfig swift1f1 11.1.1.77/24
[root@localhost SDK-V1.10.1.9]# ip netns exec netABC ifconfig
swift1f1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 11.1.1.77  netmask 255.255.255.0  broadcast 11.1.1.255
        inet6 fe80::b0c2:aeff:fee6:dc47  prefixlen 64  scopeid 0x20<link>
        ether b2:c2:ae:e6:dc:47  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 9  bytes 726 (726.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost SDK-V1.10.1.9]# 
[root@localhost SDK-V1.10.1.9]# ip netns exec netABC ip route
11.1.1.0/24 dev swift1f1 proto kernel scope link src 11.1.1.77 
[root@localhost SDK-V1.10.1.9]# 
[root@localhost SDK-V1.10.1.9]# 
[root@localhost SDK-V1.10.1.9]# ip route
default via 10.1.22.1 dev enp5s0 proto static metric 100 
10.1.22.0/24 dev enp5s0 proto kernel scope link src 10.1.22.73 metric 100 
192.168.2.0/24 via 10.1.22.1 dev enp5s0 proto static metric 100 
192.168.4.0/22 via 10.1.22.1 dev enp5s0 proto static metric 100 
192.168.30.0/24 via 10.1.22.1 dev enp5s0 proto static metric 100 
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown 
[root@localhost SDK-V1.10.1.9]# 

3 验证

3.1 未网络命令空间外网卡配置IP

# 注意:
# 因个人需求需要 网络命令空间内的网卡 swift1f1 与 网口命令空间外的网卡 swift1f0 互通,
所以需要配置同一网段。即:
swift1f0: 11.1.1.73/24
swift1f1: 11.1.1.77/24


# 配置IP 前
[root@localhost SDK-V1.10.1.7]# ifconfig
enp5s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.1.22.73  netmask 255.255.255.0  broadcast 10.1.22.255
        inet6 fe80::5a11:22ff:fe9e:2888  prefixlen 64  scopeid 0x20<link>
        ether 58:11:22:9e:28:88  txqueuelen 1000  (Ethernet)
        RX packets 100905  bytes 113208973 (107.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 50245  bytes 4158774 (3.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 94  bytes 10310 (10.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 94  bytes 10310 (10.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

swift1f0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 32:74:4f:81:e0:e2  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 847  dropped 0  overruns 0  frame 847
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:56:07:08  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost SDK-V1.10.1.7]# 
[root@localhost SDK-V1.10.1.7]# 

# 配置IP
# ip addr add <IP>/<掩码> dev <网卡名>

[root@localhost SDK-V1.10.1.7]# 
[root@localhost SDK-V1.10.1.7]# ip addr add 11.1.1.73/24 dev swift1f0
[root@localhost SDK-V1.10.1.7]# 
[root@localhost SDK-V1.10.1.7]# ifconfig
enp5s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.1.22.73  netmask 255.255.255.0  broadcast 10.1.22.255
        inet6 fe80::5a11:22ff:fe9e:2888  prefixlen 64  scopeid 0x20<link>
        ether 58:11:22:9e:28:88  txqueuelen 1000  (Ethernet)
        RX packets 101034  bytes 113221673 (107.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 50314  bytes 4166866 (3.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 106  bytes 11330 (11.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 106  bytes 11330 (11.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

swift1f0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 11.1.1.73  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 32:74:4f:81:e0:e2  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 847  dropped 0  overruns 0  frame 847
        TX packets 8  bytes 858 (858.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:56:07:08  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost SDK-V1.10.1.7]# 

3.2 验证配置

3.2.1 在网络命令空间外接口启动iperf3

#启动 iperf3 Server
#  iperf3 -B <网络命令空间外网卡IP> -p <Port> -i 1 -s


[root@localhost SDK-V1.10.1.7]#
[root@localhost SDK-V1.10.1.7]# iperf3 -B 11.1.1.73 -p 54321 -i 1 -s
-----------------------------------------------------------
Server listening on 54321
-----------------------------------------------------------

3.2.2 网络命令空间内启动iperf3 client

# 网络命令空间内 启动 ipfer3 client

# 1. 进入 网络命令空间
     #  ip netns exec netABC bash

# 2. 启动iperf3 client
     # iperf3 -c <iperf3 Server IP> -p <Server Port> -t <发包时间>   # 发包时间单位 S

[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# ip netns exec netABC bash
[root@localhost ~]# 
[root@localhost ~]# ip netns list
netABC
[root@localhost ~]# 
[root@localhost ~]# ip route list
11.1.1.0/24 dev swift1f1 proto kernel scope link src 11.1.1.77 
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# iperf3 -c 11.1.1.73 -p 54321 -t 10

到此已完成网络命令空间配置

相关推荐
迃幵chen1 小时前
高可用虚拟IP-keepalived
linux·服务器·tcp/ip
fendouweiqian1 小时前
宝塔面板 php8.0 安装 fileinfo 拓展失败
php·宝塔
猫猫的小茶馆2 小时前
【IO编程】标准IO和文件IO的对比
linux·c语言·嵌入式硬件·microsoft·嵌入式实时数据库
学Linux的语莫2 小时前
k8s之pod生命周期
linux·运维·docker·容器·kubernetes
启明真纳2 小时前
k8s物料清单工具——KubeClarity
linux·运维·云原生·容器·kubernetes·云计算
wanhengidc2 小时前
国内大带宽服务器的应用场景
运维·服务器
睡美人的小仙女1273 小时前
多个表单使用相同的 ref 和 rules,表单验证规则不生效
服务器·前端·javascript
誓约酱3 小时前
Linux下文件操作相关接口
linux·运维·服务器·c语言·c++·后端
胡图蛋.3 小时前
什么是MVCC
java·服务器·数据库