欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Linux虚拟网络设备

时间:2023-06-27
一、tun/tap(虚拟网卡)

TUN/TAP设备浅析(一) – 原理浅析 - 简书 (jianshu.com)

(3条消息) Linux-虚拟网络设备-tun/tap_sld880311的专栏-CSDN博客_linux tap

理解Linux虚拟网卡设备tun/tap的一切 | 骏马金龙 (junmajinlong.com)

Linux-虚拟网络设备-tun/tap_sld880311的专栏-CSDN博客_linux tap

1、特点

TUN 和 TAP 设备是 Linux 内核虚拟网络设备,纯软件实现。

TUN(TUNnel)设备模拟网络层设备,处理三层报文如 IP 报文。TAP 设备模拟链路层设备,处理二层报文,比如以太网帧。

2、使用方法

Linux 提供了一些命令行程序方便我们来创建持久化的tun/tap设备,但是如果没有应用程序打开对应的文件描述符,tun/tap的状态一直会是DOWN,还好的是这并不会影响我们把它当作普通网卡去使用。

onlylove@ubuntu:~$ ip tuntap helpUsage: ip tuntap { add | del | show | list | lst | help } [ dev PHYS_DEV ][ mode { tun | tap } ] [ user USER ] [ group GROUP ][ one_queue ] [ pi ] [ vnet_hdr ] [ multi_queue ] [ name NAME ]Where:USER := { STRING | NUMBER }GROUP := { STRING | NUMBER }onlylove@ubuntu:~$

# 创建 tap ip tuntap add dev tap0 mode tap # 创建 tunip tuntap add dev tun0 mode tun # 删除 tapip tuntap del dev tap0 mode tap# 删除 tunip tuntap del dev tun0 mode tun

3、示例 1、tap(二层网络设备)

# 添加 tap 网卡sudo ip tuntap add dev tap0 mode tap# 启动 tap 网卡sudo ip link set dev tap0 up# 给 tap 设置 ip 地址sudo ip address add dev tap0 192.168.1.128/24# 显示所有的路由信息ip route show# 删除网卡sudo ip tuntap del dev tap0 mode tap

onlylove@ubuntu:~$ ifconfig -aens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 13906 bytes 7451463 (7.4 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 5427 bytes 729793 (729.7 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 2665 bytes 155516 (155.5 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2665 bytes 155516 (155.5 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0onlylove@ubuntu:~$ clearonlylove@ubuntu:~$ ifconfigens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 13988 bytes 7457366 (7.4 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 5470 bytes 734415 (734.4 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 2665 bytes 155516 (155.5 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2665 bytes 155516 (155.5 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0onlylove@ubuntu:~$ clearonlylove@ubuntu:~$ onlylove@ubuntu:~$ onlylove@ubuntu:~$ onlylove@ubuntu:~$ ifconfig -aens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 14031 bytes 7460576 (7.4 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 5498 bytes 738063 (738.0 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 2665 bytes 155516 (155.5 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2665 bytes 155516 (155.5 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0onlylove@ubuntu:~$ ip route showdefault via 192.168.6.2 dev ens33 proto static metric 100 169.254.0.0/16 dev ens33 scope link metric 1000 192.168.6.0/24 dev ens33 proto kernel scope link src 192.168.6.129 metric 100 onlylove@ubuntu:~$ onlylove@ubuntu:~$ sudo ip tuntap add dev tap0 mode taponlylove@ubuntu:~$ onlylove@ubuntu:~$ ifconfigens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 14098 bytes 7465506 (7.4 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 5535 bytes 742715 (742.7 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 2665 bytes 155516 (155.5 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2665 bytes 155516 (155.5 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0onlylove@ubuntu:~$ onlylove@ubuntu:~$ ifconfig -aens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 14125 bytes 7467546 (7.4 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 5555 bytes 745387 (745.3 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 2665 bytes 155516 (155.5 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2665 bytes 155516 (155.5 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0tap0: flags=4098 mtu 1500 ether 52:19:9d:b8:18:d7 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 0onlylove@ubuntu:~$ onlylove@ubuntu:~$ sudo ip link set dev tap0 uponlylove@ubuntu:~$ onlylove@ubuntu:~$ ifconfigens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 14165 bytes 7470368 (7.4 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 5573 bytes 748423 (748.4 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 2665 bytes 155516 (155.5 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2665 bytes 155516 (155.5 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0tap0: flags=4099 mtu 1500 ether 52:19:9d:b8:18:d7 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 0onlylove@ubuntu:~$ onlylove@ubuntu:~$ sudo ip address add dev tap0 192.168.1.128/24onlylove@ubuntu:~$ onlylove@ubuntu:~$ ifconfigens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 14263 bytes 7478478 (7.4 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 5648 bytes 756811 (756.8 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 2675 bytes 156336 (156.3 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2675 bytes 156336 (156.3 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0tap0: flags=4099 mtu 1500 inet 192.168.1.128 netmask 255.255.255.0 broadcast 0.0.0.0 ether 52:19:9d:b8:18:d7 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 0onlylove@ubuntu:~$ onlylove@ubuntu:~$ ip route showdefault via 192.168.6.2 dev ens33 proto static metric 100 169.254.0.0/16 dev ens33 scope link metric 1000 192.168.1.0/24 dev tap0 proto kernel scope link src 192.168.1.128 linkdown 192.168.6.0/24 dev ens33 proto kernel scope link src 192.168.6.129 metric 100 onlylove@ubuntu:~$ onlylove@ubuntu:~$ sudo ip tuntap del dev tap0 mode taponlylove@ubuntu:~$ onlylove@ubuntu:~$ ifconfig -aens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 14317 bytes 7482917 (7.4 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 5684 bytes 761877 (761.8 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 2693 bytes 158036 (158.0 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2693 bytes 158036 (158.0 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0onlylove@ubuntu:~$

2、tun(三层网络设备)

# 添加 tun 网卡sudo ip tuntap add dev tun0 mode tun# 启动 tun 网卡sudo ip link set dev tun0 up# 给 tun 设置 ip 地址sudo ip address add dev tun0 192.168.1.128/24# 显示所有的路由信息ip route show# 删除网卡sudo ip tuntap del dev tun0 mode tun

onlylove@ubuntu:~$ ifconfig -aens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 14987 bytes 7532982 (7.5 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 6118 bytes 813370 (813.3 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 2735 bytes 162084 (162.0 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2735 bytes 162084 (162.0 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0onlylove@ubuntu:~$ onlylove@ubuntu:~$ ip route showdefault via 192.168.6.2 dev ens33 proto static metric 100 169.254.0.0/16 dev ens33 scope link metric 1000 192.168.6.0/24 dev ens33 proto kernel scope link src 192.168.6.129 metric 100 onlylove@ubuntu:~$ onlylove@ubuntu:~$ sudo ip tuntap add dev tun0 mode tunonlylove@ubuntu:~$ onlylove@ubuntu:~$ ifconfigens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 15099 bytes 7541904 (7.5 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 6192 bytes 821668 (821.6 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 2737 bytes 162256 (162.2 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2737 bytes 162256 (162.2 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0onlylove@ubuntu:~$ onlylove@ubuntu:~$ ifconfig -aens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 15127 bytes 7543974 (7.5 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 6209 bytes 824160 (824.1 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 2737 bytes 162256 (162.2 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2737 bytes 162256 (162.2 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0tun0: flags=4240 mtu 1500 unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 500 (UNSPEC) 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 0onlylove@ubuntu:~$ onlylove@ubuntu:~$ sudo ip link set dev tun0 uponlylove@ubuntu:~$ onlylove@ubuntu:~$ ifconfigens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 15160 bytes 7546406 (7.5 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 6234 bytes 827716 (827.7 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 2737 bytes 162256 (162.2 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2737 bytes 162256 (162.2 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0tun0: flags=4241 mtu 1500 unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 500 (UNSPEC) 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 0onlylove@ubuntu:~$ onlylove@ubuntu:~$ onlylove@ubuntu:~$ sudo ip address add dev tun0 192.168.1.128/24onlylove@ubuntu:~$ onlylove@ubuntu:~$ ifconfigens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 15194 bytes 7549068 (7.5 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 6255 bytes 831098 (831.0 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 2745 bytes 162904 (162.9 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2745 bytes 162904 (162.9 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0tun0: flags=4241 mtu 1500 inet 192.168.1.128 netmask 255.255.255.0 destination 192.168.1.128 unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 500 (UNSPEC) 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 0onlylove@ubuntu:~$ onlylove@ubuntu:~$ ip route showdefault via 192.168.6.2 dev ens33 proto static metric 100 169.254.0.0/16 dev ens33 scope link metric 1000 192.168.1.0/24 dev tun0 proto kernel scope link src 192.168.1.128 linkdown 192.168.6.0/24 dev ens33 proto kernel scope link src 192.168.6.129 metric 100 onlylove@ubuntu:~$ onlylove@ubuntu:~$ ifconfigens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 15229 bytes 7551916 (7.5 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 6278 bytes 834964 (834.9 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 2751 bytes 163632 (163.6 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2751 bytes 163632 (163.6 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0tun0: flags=4241 mtu 1500 inet 192.168.1.128 netmask 255.255.255.0 destination 192.168.1.128 unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 500 (UNSPEC) 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 0onlylove@ubuntu:~$ onlylove@ubuntu:~$ sudo ip tuntap del dev tun0 mode tunonlylove@ubuntu:~$ onlylove@ubuntu:~$ ifconfig -aens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 15269 bytes 7555038 (7.5 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 6302 bytes 838604 (838.6 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 2759 bytes 164280 (164.2 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2759 bytes 164280 (164.2 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0onlylove@ubuntu:~$

二、veth

Linux虚拟网络设备之veth - SegmentFault 思否

Linux-虚拟网络设备-veth pair_sld880311的专栏-CSDN博客_linux veth

1、veth设备的特点

1、veth和其它的网络设备都一样,一端连接的是内核协议栈。

2、veth设备是成对出现的,另一端两个设备彼此相连。

3、一个设备收到协议栈的数据发送请求后,会将数据发送到另一个设备上去。

2、使用方法

onlylove@ubuntu:~$ ip link helpUsage: ip link add [link DEV] [ name ] NAME [ txqueuelen PACKETS ] [ address LLADDR ] [ broadcast LLADDR ] [ mtu MTU ] [index IDX ] [ numtxqueues QUEUE_COUNT ] [ numrxqueues QUEUE_COUNT ] type TYPE [ ARGS ]ip link delete { DEVICE | dev DEVICE | group DEVGROUP } type TYPE [ ARGS ]ip link set { DEVICE | dev DEVICE | group DEVGROUP }[ { up | down } ][ type TYPE ARGS ][ arp { on | off } ][ dynamic { on | off } ][ multicast { on | off } ][ allmulticast { on | off } ][ promisc { on | off } ][ trailers { on | off } ][ carrier { on | off } ][ txqueuelen PACKETS ][ name NEWNAME ][ address LLADDR ][ broadcast LLADDR ][ mtu MTU ][ netns { PID | NAME } ][ link-netns NAME | link-netnsid ID ][ alias NAME ][ vf NUM [ mac LLADDR ] [ vlan VLANID [ qos VLAN-QOS ] [ proto VLAN-PROTO ] ] [ rate TXRATE ] [ max_tx_rate TXRATE ] [ min_tx_rate TXRATE ] [ spoofchk { on | off} ] [ query_rss { on | off} ] [ state { auto | enable | disable} ] ] [ trust { on | off} ] ] [ node_guid { eui64 } ] [ port_guid { eui64 } ][ { xdp | xdpgeneric | xdpdrv | xdpoffload } { off | object FILE [ section NAME ] [ verbose ] | pinned FILE } ][ master DEVICE ][ vrf NAME ][ nomaster ][ addrgenmode { eui64 | none | stable_secret | random } ][ protodown { on | off } ][ gso_max_size BYTES ] | [ gso_max_segs PACKETS ]ip link show [ DEVICE | group GROUP ] [up] [master DEV] [vrf NAME] [type TYPE]ip link xstats type TYPE [ ARGS ]ip link afstats [ dev DEVICE ]ip link property add dev DEVICE [ altname NAME .、]ip link property del dev DEVICE [ altname NAME .、]ip link help [ TYPE ]TYPE := { vlan | veth | vcan | vxcan | dummy | ifb | macvlan | macvtap | bridge | bond | team | ipoib | ip6tnl | ipip | sit | vxlan | gre | gretap | erspan | ip6gre | ip6gretap | ip6erspan | vti | nlmon | team_slave | bond_slave | bridge_slave | ipvlan | ipvtap | geneve | vrf | macsec | netdevsim | rmnet | xfrm }onlylove@ubuntu:~$

onlylove@ubuntu:~$ ip link help vethUsage: ip link type veth [peer ]To get type 'ip link add help'onlylove@ubuntu:~$

3、示例

# 添加 veth0 和 veth1sudo ip link add veth0 type veth peer name veth1# 为 veth0 配置 ip 地址sudo ip addr add 192.168.2.11/24 dev veth0# 为 veth1 配置 ip 地址sudo ip addr add 192.168.2.5/24 dev veth1# 启动 vteh0sudo ip link set veth0 up# 启动 vteh1sudo ip link set veth1 up# 删除 veth0 和 veth1sudo ip link delete veth0 type veth peer name veth1

onlylove@ubuntu:~$ ifconfig -aens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 22931 bytes 15105317 (15.1 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8310 bytes 1051491 (1.0 MB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 2961 bytes 181332 (181.3 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2961 bytes 181332 (181.3 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0onlylove@ubuntu:~$ onlylove@ubuntu:~$ sudo ip link add veth0 type veth peer name veth1[sudo] password for onlylove: onlylove@ubuntu:~$ onlylove@ubuntu:~$ ifconfigens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 22984 bytes 15109265 (15.1 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8340 bytes 1055343 (1.0 MB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 2961 bytes 181332 (181.3 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2961 bytes 181332 (181.3 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0onlylove@ubuntu:~$ onlylove@ubuntu:~$ ifconfig -aens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 23012 bytes 15111335 (15.1 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8357 bytes 1057873 (1.0 MB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 2961 bytes 181332 (181.3 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2961 bytes 181332 (181.3 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0veth0: flags=4098 mtu 1500 ether 1a:c5:9b:ac:82:a2 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 0veth1: flags=4098 mtu 1500 ether fa:90:44:97:74:93 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 0onlylove@ubuntu:~$ onlylove@ubuntu:~$ sudo ip addr add 192.168.2.11/24 dev veth0onlylove@ubuntu:~$ sudo ip addr add 192.168.2.5/24 dev veth1onlylove@ubuntu:~$ sudo ip link set veth0 uponlylove@ubuntu:~$ sudo ip link set veth1 uponlylove@ubuntu:~$ onlylove@ubuntu:~$ ifconfigens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 23071 bytes 15116421 (15.1 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8402 bytes 1063497 (1.0 MB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 3001 bytes 184572 (184.5 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3001 bytes 184572 (184.5 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0veth0: flags=4163 mtu 1500 inet 192.168.2.11 netmask 255.255.255.0 broadcast 0.0.0.0 inet6 fe80::18c5:9bff:feac:82a2 prefixlen 64 scopeid 0x20 ether 1a:c5:9b:ac:82:a2 txqueuelen 1000 (Ethernet) RX packets 25 bytes 3011 (3.0 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 11 bytes 1196 (1.1 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0veth1: flags=4163 mtu 1500 inet 192.168.2.5 netmask 255.255.255.0 broadcast 0.0.0.0 inet6 fe80::f890:44ff:fe97:7493 prefixlen 64 scopeid 0x20 ether fa:90:44:97:74:93 txqueuelen 1000 (Ethernet) RX packets 11 bytes 1196 (1.1 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 25 bytes 3011 (3.0 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0onlylove@ubuntu:~$ onlylove@ubuntu:~$ sudo ip link delete veth0 type veth peer name veth1onlylove@ubuntu:~$ onlylove@ubuntu:~$ ifconfigens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 23106 bytes 15119275 (15.1 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8425 bytes 1067607 (1.0 MB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 3017 bytes 185868 (185.8 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3017 bytes 185868 (185.8 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0onlylove@ubuntu:~$

三、bridge(虚拟交换机)

Linux虚拟网络设备之bridge(桥) - SegmentFault 思否

Linux-虚拟网络设备-LinuxBridge_sld880311的专栏-CSDN博客_linux 虚拟交换机

1、特点

bridge是一个虚拟网络设备,所以具有网络设备的特征,可以配置IP、MAC地址等;其次,bridge是一个虚拟交换机,和物理交换机有类似的功能。

2、使用方法

onlylove@ubuntu:~$ ip link helpUsage: ip link add [link DEV] [ name ] NAME [ txqueuelen PACKETS ] [ address LLADDR ] [ broadcast LLADDR ] [ mtu MTU ] [index IDX ] [ numtxqueues QUEUE_COUNT ] [ numrxqueues QUEUE_COUNT ] type TYPE [ ARGS ]ip link delete { DEVICE | dev DEVICE | group DEVGROUP } type TYPE [ ARGS ]ip link set { DEVICE | dev DEVICE | group DEVGROUP }[ { up | down } ][ type TYPE ARGS ][ arp { on | off } ][ dynamic { on | off } ][ multicast { on | off } ][ allmulticast { on | off } ][ promisc { on | off } ][ trailers { on | off } ][ carrier { on | off } ][ txqueuelen PACKETS ][ name NEWNAME ][ address LLADDR ][ broadcast LLADDR ][ mtu MTU ][ netns { PID | NAME } ][ link-netns NAME | link-netnsid ID ][ alias NAME ][ vf NUM [ mac LLADDR ] [ vlan VLANID [ qos VLAN-QOS ] [ proto VLAN-PROTO ] ] [ rate TXRATE ] [ max_tx_rate TXRATE ] [ min_tx_rate TXRATE ] [ spoofchk { on | off} ] [ query_rss { on | off} ] [ state { auto | enable | disable} ] ] [ trust { on | off} ] ] [ node_guid { eui64 } ] [ port_guid { eui64 } ][ { xdp | xdpgeneric | xdpdrv | xdpoffload } { off | object FILE [ section NAME ] [ verbose ] | pinned FILE } ][ master DEVICE ][ vrf NAME ][ nomaster ][ addrgenmode { eui64 | none | stable_secret | random } ][ protodown { on | off } ][ gso_max_size BYTES ] | [ gso_max_segs PACKETS ]ip link show [ DEVICE | group GROUP ] [up] [master DEV] [vrf NAME] [type TYPE]ip link xstats type TYPE [ ARGS ]ip link afstats [ dev DEVICE ]ip link property add dev DEVICE [ altname NAME .、]ip link property del dev DEVICE [ altname NAME .、]ip link help [ TYPE ]TYPE := { vlan | veth | vcan | vxcan | dummy | ifb | macvlan | macvtap | bridge | bond | team | ipoib | ip6tnl | ipip | sit | vxlan | gre | gretap | erspan | ip6gre | ip6gretap | ip6erspan | vti | nlmon | team_slave | bond_slave | bridge_slave | ipvlan | ipvtap | geneve | vrf | macsec | netdevsim | rmnet | xfrm }onlylove@ubuntu:~$

onlylove@ubuntu:~$ ip link help bridgeUsage: ..、bridge [ fdb_flush ] [ forward_delay FORWARD_DELAY ] [ hello_time HELLO_TIME ] [ max_age MAX_AGE ] [ ageing_time AGEING_TIME ] [ stp_state STP_STATE ] [ priority PRIORITY ] [ group_fwd_mask MASK ] [ group_address ADDRESS ] [ vlan_filtering VLAN_FILTERING ] [ vlan_protocol VLAN_PROTOCOL ] [ vlan_default_pvid VLAN_DEFAULT_PVID ] [ vlan_stats_enabled VLAN_STATS_ENABLED ] [ vlan_stats_per_port VLAN_STATS_PER_PORT ] [ mcast_snooping MULTICAST_SNOOPING ] [ mcast_router MULTICAST_ROUTER ] [ mcast_query_use_ifaddr MCAST_QUERY_USE_IFADDR ] [ mcast_querier MULTICAST_QUERIER ] [ mcast_hash_elasticity HASH_ELASTICITY ] [ mcast_hash_max HASH_MAX ] [ mcast_last_member_count LAST_MEMBER_COUNT ] [ mcast_startup_query_count STARTUP_QUERY_COUNT ] [ mcast_last_member_interval LAST_MEMBER_INTERVAL ] [ mcast_membership_interval MEMBERSHIP_INTERVAL ] [ mcast_querier_interval QUERIER_INTERVAL ] [ mcast_query_interval QUERY_INTERVAL ] [ mcast_query_response_interval QUERY_RESPONSE_INTERVAL ] [ mcast_startup_query_interval STARTUP_QUERY_INTERVAL ] [ mcast_stats_enabled MCAST_STATS_ENABLED ] [ mcast_igmp_version IGMP_VERSION ] [ mcast_mld_version MLD_VERSION ] [ nf_call_iptables NF_CALL_IPTABLES ] [ nf_call_ip6tables NF_CALL_IP6TABLES ] [ nf_call_arptables NF_CALL_ARPTABLES ]Where: VLAN_PROTOCOL := { 802.1Q | 802.1ad }onlylove@ubuntu:~$

3、示例

# 创建 bridgesudo ip link add name br0 type bridge# 启动 bridgesudo ip link set br0 up# 查看 bridge 上连接的设备sudo bridge link# 删除 bridgesudo ip link delete name br0 type bridge# 将 xxx 设备连接到 bridge 上sudo ip link set dev xxx master br0

onlylove@ubuntu:~$ ifconfig -aens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 17478 bytes 7716741 (7.7 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 7699 bytes 992070 (992.0 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 2932 bytes 178733 (178.7 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2932 bytes 178733 (178.7 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0onlylove@ubuntu:~$ onlylove@ubuntu:~$ sudo ip link add name br0 type bridgeonlylove@ubuntu:~$ onlylove@ubuntu:~$ ifconfigens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 17506 bytes 7718851 (7.7 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 7719 bytes 994952 (994.9 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 2932 bytes 178733 (178.7 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2932 bytes 178733 (178.7 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0onlylove@ubuntu:~$ onlylove@ubuntu:~$ ifconfig -abr0: flags=4098 mtu 1500 ether 4a:dd:f2:e6:a5:ba 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 0ens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 17523 bytes 7720059 (7.7 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 7729 bytes 996822 (996.8 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 2932 bytes 178733 (178.7 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2932 bytes 178733 (178.7 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0onlylove@ubuntu:~$ onlylove@ubuntu:~$ onlylove@ubuntu:~$ sudo ip link set br0 uponlylove@ubuntu:~$ onlylove@ubuntu:~$ ifconfigbr0: flags=4163 mtu 1500 inet6 fe80::48dd:f2ff:fee6:a5ba prefixlen 64 scopeid 0x20 ether 4a:dd:f2:e6:a5:ba txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 16 bytes 1954 (1.9 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0ens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 17558 bytes 7722787 (7.7 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 7754 bytes 1000380 (1.0 MB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 2940 bytes 179381 (179.3 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2940 bytes 179381 (179.3 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0onlylove@ubuntu:~$ onlylove@ubuntu:~$ onlylove@ubuntu:~$ sudo bridge linkonlylove@ubuntu:~$ onlylove@ubuntu:~$ sudo ip link delete name br0 type bridgeonlylove@ubuntu:~$ onlylove@ubuntu:~$ ifconfig -aens33: flags=4163 mtu 1500 inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255 inet6 fe80::20c:29ff:feb8:de3b prefixlen 64 scopeid 0x20 ether 00:0c:29:b8:de:3b txqueuelen 1000 (Ethernet) RX packets 17609 bytes 7726705 (7.7 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 7787 bytes 1004970 (1.0 MB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 2948 bytes 180029 (180.0 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2948 bytes 180029 (180.0 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0onlylove@ubuntu:~$

四、OpenvSwitch

Linux-虚拟网络设备-OpenvSwitch(持续更新)_sld880311的专栏-CSDN博客_linux openvswitch

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。