这篇文章将为大家详细讲解有关ubuntu中VETH虚拟接口的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
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: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST> 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: <BROADCAST,MULTICAST,M-DOWN> 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: <BROADCAST,MULTICAST,M-DOWN> 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: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST> 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: <LOOPBACK> 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: <BROADCAST,MULTICAST> 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: <LOOPBACK> 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: <NO-CARRIER,BROADCAST,MULTICAST,UP> 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: <LOOPBACK> 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: <BROADCAST,MULTICAST> 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: <LOOPBACK> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> 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虚拟接口的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。