ubuntu中VETH虚拟接口的示例分析
这篇文章将为大家详细讲解有关ubuntu中VETH虚拟接口的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
恩平网站建设公司成都创新互联公司,恩平网站设计制作,有大型网站制作公司丰富经验。已为恩平1000多家提供企业网站建设服务。企业网站搭建\外贸营销网站建设要多少钱,请找那个售后服务好的恩平做网站的公司定做!
简介
virtual ethernet pair (VETH pair)
Veth是Linux中一种虚拟出来的网络设备,veth设备总是成对出现,所以一般也叫veth-pair。数据会从一头进入另一头出去,所以类似于网线的作用。
用途:
由于veth的“网线”特性,它常常充当着一个桥梁,连接着各种虚拟网络设备。常见用途是连接两个netwok namespace,或者连接Linux-Bridge、OVS 之类的(现在市面上的容器多是使用veth连接的,另外还有tun/tap接口多用于虚拟机,他俩的区别,veth:相当于两头是水晶头的一根网段;tun/tap:相当于一头是水晶头,一头是usb -_-)
试验
Ubuntu18.04
基本命令
ip link add tap1 type veth peer name tap2 ip netns add red ip netns add blue ip link set tap1 netns red ip link set tap2 netns blue ip netns exec red ip a ip netns exec red ip link set tap1 up ip netns exec red ifconfig tap1 192.168.1.2/24 ip netns exec blue ip a ip netns exec blue ip link set tap2 up ip netns exec blue ifconfig tap2 192.168.1.3/24 ip netns exec red ping 192.168.1.3 ip netns exec blue ping 192.168.1.2 # 删除一端,另一端会自动删除 ip link del tap1 # 删除ns,会连带veth一起删除 ip nets del red
过程
创建veth接口,可见多了tap1和tap2接口
root@ubuntu:~# ip link add tap1 type veth peer name tap2 root@ubuntu:~# ip a 1: lo:mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: mtu 1500 qdisc mq state DOWN group default qlen 1000 link/ether 00:50:56:ab:c5:2a brd ff:ff:ff:ff:ff:ff 3: tap2@tap1: mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether 8e:0b:86:3f:f4:79 brd ff:ff:ff:ff:ff:ff 4: tap1@tap2: mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether 86:bd:8e:03:29:68 brd ff:ff:ff:ff:ff:ff
创建命名空间
root@ubuntu:~# ip netns add red root@ubuntu:~# ip netns add blue root@ubuntu:~# ip netns blue red
接口接入命名空间,再ip a查看,已经看不到tap1和tap2,因为去到各自的命名空间去了
root@ubuntu:~# ip link set tap1 netns red root@ubuntu:~# ip link set tap2 netns blue root@ubuntu:~# ip a 1: lo:mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: mtu 1500 qdisc mq state DOWN group default qlen 1000 link/ether 00:50:56:ab:c5:2a brd ff:ff:ff:ff:ff:ff root@ubuntu:~#
在各命名空间里启动接口,设置IP
tap1
root@ubuntu:~# ip netns exec red ip a 1: lo:mtu 65536 qdisc noop state DOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 6: tap1@if5: mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether 86:bd:8e:03:29:68 brd ff:ff:ff:ff:ff:ff link-netnsid 1 root@ubuntu:~# ip netns exec red ip link set tap1 up root@ubuntu:~# ip netns exec red ifconfig tap1 192.168.1.2/24 root@ubuntu:~# ip netns exec red ip a 1: lo: mtu 65536 qdisc noop state DOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 6: tap1@if5: mtu 1500 qdisc noqueue state LOWERLAYERDOWN group default qlen 1000 link/ether 86:bd:8e:03:29:68 brd ff:ff:ff:ff:ff:ff link-netnsid 1 inet 192.168.1.2/24 brd 192.168.1.255 scope global tap1 valid_lft forever preferred_lft forever
tap2
root@ubuntu:~# ip netns exec blue ip a 1: lo:mtu 65536 qdisc noop state DOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 5: tap2@if6: mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether 8e:0b:86:3f:f4:79 brd ff:ff:ff:ff:ff:ff link-netnsid 0 root@ubuntu:~# ip netns exec blue ip link set tap2 up root@ubuntu:~# ip netns exec blue ifconfig tap2 192.168.1.3/24 root@ubuntu:~# ip netns exec blue ip a 1: lo: mtu 65536 qdisc noop state DOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 5: tap2@if6: mtu 1500 qdisc noqueue state UP group default qlen 1000 link/ether 8e:0b:86:3f:f4:79 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 192.168.1.3/24 brd 192.168.1.255 scope global tap2 valid_lft forever preferred_lft forever inet6 fe80::8c0b:86ff:fe3f:f479/64 scope link valid_lft forever preferred_lft forever
ping测试
root@ubuntu:~# ip netns exec red ping 192.168.1.3 PING 192.168.1.3 (192.168.1.3) 56(84) bytes of data. 64 bytes from 192.168.1.3: icmp_seq=1 ttl=64 time=0.047 ms 64 bytes from 192.168.1.3: icmp_seq=2 ttl=64 time=0.035 ms ^C --- 192.168.1.3 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1006ms rtt min/avg/max/mdev = 0.035/0.041/0.047/0.006 ms root@ubuntu:~# root@ubuntu:~# root@ubuntu:~# ip netns exec blue ping 192.168.1.2 PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data. 64 bytes from 192.168.1.2: icmp_seq=1 ttl=64 time=0.021 ms 64 bytes from 192.168.1.2: icmp_seq=2 ttl=64 time=0.037 ms 64 bytes from 192.168.1.2: icmp_seq=3 ttl=64 time=0.031 ms ^C --- 192.168.1.2 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2050ms rtt min/avg/max/mdev = 0.021/0.029/0.037/0.009 ms
关于“ubuntu中VETH虚拟接口的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
分享文章:ubuntu中VETH虚拟接口的示例分析
文章出自:http://myzitong.com/article/jdgees.html