~阿弥陀佛~

人生能有多少天是按照自己的想法去活着

ip netns手动创建虚拟网卡

手动创建虚拟网卡,测试网络通讯模型时轻量,模拟物理桥 nat桥易如反掌,前提是要借助软交换机bridge-utils来模拟

学会使用ip netns命令,

[root@node-1 ~]# rpm -q iproute  (查看是否有相关安装)

iproute-4.11.0-30.el7.x86_64

[root@node-1 ~]#

[root@node-1 ~]# ip help

Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }

       ip [ -force ] -batch filename

where  OBJECT := { link | address | addrlabel | route | rule | neigh | ntable |

                   tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm |

                   netns | l2tp | fou | macsec | tcp_metrics | token | netconf | ila |

                   vrf }

       OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |

                    -h[uman-readable] | -iec |

                    -f[amily] { inet | inet6 | ipx | dnet | mpls | bridge | link } |

                    -4 | -6 | -I | -D | -B | -0 |

                    -l[oops] { maximum-addr-flush-attempts } | -br[ief] |

                    -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |

                    -rc[vbuf] [size] | -n[etns] name | -a[ll] | -c[olor]}

[root@node-1 ~]# ip netns help

Usage: ip netns list   (列出网络名称空间)

       ip netns add NAME *(添加网络名称空间)

       ip netns set NAME NETNSID (设置网络名称空间的sid,另外可以将网络设备挪到某个名称空间使用ip link)

       ip [-all] netns delete [NAME]

       ip netns identify [PID]

       ip netns pids NAME

       ip [-all] netns exec [NAME] cmd … (在网络名称空间中执行命令 )

       ip netns monitor

       ip netns list-id

[root@master-1 ~]# ip netns add r1  (添加r1名称空间)

[root@master-1 ~]# ip netns add r2

[root@master-1 ~]# ip netns list

r2

r1

[root@master-1 ~]# ip netns exec r1 ifconfig  (无返回)

[root@master-1 ~]#、

[root@master-1 ~]# ip netns exec r1 ifconfig -a (只有lo)

lo: flags=8<LOOPBACK>  mtu 65536

        loop  txqueuelen 1000  (Local Loopback)

        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@master-1 ~]#

 

可以使用ip命令创建虚拟网卡对儿,然后手动分配到网络名称空间中

[root@master-1 ~]# ip link add name veth1.1 type veth peer name veth1.2  (peer name另外一半的名字)

[root@master-1 ~]# ip link show  (成对儿出现在宿主机上)

10: veth1.2@veth1.1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000

    link/ether ee:f4:ab:af:df:04 brd ff:ff:ff:ff:ff:ff

11: veth1.1@veth1.2: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000

link/ether 52:6e:b9:f7:87:22 brd ff:ff:ff:ff:ff:ff

 

 

[root@master-1 ~]# ip link set dev veth1.2 netns r1  (将veth1.2挪到r1名称空间内)

[root@master-1 ~]# ip link sh

11: veth1.1@if10: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000

link/ether 52:6e:b9:f7:87:22 brd ff:ff:ff:ff:ff:ff link-netnsid 2

[root@master-1 ~]# ip netns exec r1 ifconfig -a  (显示已经分配到r1名称空间)

lo: flags=8<LOOPBACK>  mtu 65536

        loop  txqueuelen 1000  (Local Loopback)

        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

 

veth1.2: flags=4098<BROADCAST,MULTICAST>  mtu 1500

        ether ee:f4:ab:af:df:04  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@master-1 ~]#

[root@master-1 ~]# ip netns exec r1 ip link set dev veth1.2 name eth0 (将r1名称空间内的veth1.2改名为eth0)

[root@master-1 ~]#

[root@master-1 ~]# ifconfig veth1.1  10.1.0.1/24 up (激活物理机veth1.1)

[root@master-1 ~]#

[root@master-1 ~]# ip netns exec r1 ifconfig eth0 10.1.0.2/24 up  (激活r1名称空间中的eth0)

[root@master-1 ~]#

[root@master-1 ~]# ip netns exec r1 ifconfig

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 10.1.0.2  netmask 255.255.255.0  broadcast 10.1.0.255

        inet6 fe80::ecf4:abff:feaf:df04  prefixlen 64  scopeid 0x20<link>

        ether ee:f4:ab:af:df:04  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@master-1 ~]#

 

[root@master-1 ~]# ping 10.1.0.2 (物理机ping r1名称空间的网卡地址)

PING 10.1.0.2 (10.1.0.2) 56(84) bytes of data.

64 bytes from 10.1.0.2: icmp_seq=1 ttl=64 time=0.108 ms

64 bytes from 10.1.0.2: icmp_seq=2 ttl=64 time=0.091 ms          

 

[root@master-1 ~]# ip link set dev veth1.1 netns r2 (将veth1.1挪到r2空间)

[root@master-1 ~]# ip netns exec r2 ifconfig  -a

lo: flags=8<LOOPBACK>  mtu 65536

        loop  txqueuelen 1000  (Local Loopback)

        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

 

veth1.1: flags=4098<BROADCAST,MULTICAST>  mtu 1500

        ether 52:6e:b9:f7:87:22  txqueuelen 1000  (Ethernet)

        RX packets 15  bytes 1146 (1.1 KiB)

 

[root@master-1 ~]# ip netns exec r2 ifconfig veth1.1 10.1.0.3/24  up (激活r2中的veth1.1)

[root@master-1 ~]#

[root@master-1 ~]# ip netns exec r2 ping 10.1.0.2   (在r2中pingr1地址

PING 10.1.0.2 (10.1.0.2) 56(84) bytes of data.

64 bytes from 10.1.0.2: icmp_seq=1 ttl=64 time=0.060 ms

Next Post

Previous Post

Leave a Reply

© 2021 ~阿弥陀佛~

ICP备案号: 京ICP备19038630号

版权所有@ 王力翔

耗时 0.205 秒 | 查询 43 次 | 内存 22.34 MB