本文将详细解释ubuntu中VETH虚拟接口的实例分析。边肖觉得挺实用的,就分享给大家参考。希望你看完这篇文章能有所收获。
简介
虚拟以太网对(VETH对)
Veth是Linux中的一个虚拟网络设备。Veth设备总是成对出现,因此通常也称为veth-pair。数据会从一端传到另一端,所以它就像一根网线。
使用:
由于veth的“网线”特性,它经常充当连接各种虚拟网络设备的桥梁。常见的用途是连接两个网络命名空间,或者连接Linux-Bridge、OVS等。(目前市面上大部分容器都是通过veth连接,tun/tap接口多用于虚拟机。两者的区别在于veth相当于两端都有水晶头的网段;Tun/tap:相当于一个水晶头和usb -_-)
试验
Ubuntu18.04
基本命令
iplinkaddtap 1 typevetpeernametap 2
ipnetnsaddred
ipnetnsaddblue
iplinkssettap 1 netnsred
iplinksettap2netnsblue
ipnetnsexecredipa
ipnetnsexecrediplinksettap1up
ipnetnsexecredifconfigtap 1192 . 168 . 1 . 2/24
ipnetnsexecblueipa
ipnetnsexecblueiplinksettap2up
ipnetnsexecblueifconfigtap 2192 . 168 . 1 . 3/24
ipnetnsexecredding 192 . 168 . 1 . 3
ipnetnsexecblueping192.168.1.2
#删除一端,另一端会自动删除。
iplinkdeltap1
#删除ns,它将与veth一起删除。
ipdesireld
过程
创建了veth接口,显示有很多tap1和tap2接口。
root @ Ubuntu : ~ # iplinkaddtap 1 typevethpeernametap 2
root@ubuntu:~#ipa
1:lo:LOOPBACK,UP,LOWER _ upmt u 65536 qdiscnooqueuestatunknowledgdefaultqlen 1000
链接/环回00:000:000:00:003360000 brd 003360003360003360000033600003360000:00336000:00:0003:00
inet127.0.0.1/8scopehostlo
valid _ lft forever preferred _ lft forever
inet6:1/128scopehost
valid _ lft forever preferred _ lft forever
2:以太网广播、组播
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虚拟接口的示例分析”这篇文章就分享到这里了,希望
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/98528.html