寫在前面
- 博文內容為
混雜模式
的簡單認知 - 理解不足小伙伴幫忙指正
認定一件事,即使拿十分力氣都無法完成,也要拿出十二分力氣去努力。 —《劍來》
網絡接口的混雜模式
混雜模式(Promiscuous mode)
,簡稱 Promisc mode
,俗稱監聽模式
。
混雜模式
通常被網絡管理員用來診斷網絡問題,但也會被無認證的、想偷聽網絡通信的人利用。根據維基百科的定義,混雜模式是指一個網卡會把它接收的所有網絡流量都交給CPU,而不是只把它想轉交的部分交給CPU
。
在 IEEE 802
定的網絡規范中,每個網絡幀都有一個目的MAC地址
。在非混雜模式下,網卡只會接收目的MAC地址
是它自己的單播幀
,以及多播及廣播幀
;在混雜模式下,網卡會接收經過它的所有幀!
使用ifconfig
或者netstat-i
命令查看一個網卡是否開啟了混雜模式,當輸出包含 PROMISC
時,表明該網絡接口處于混雜模式
。
liruilonger@cloudshell:~$ ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 10.88.0.3 netmask 255.255.0.0 broadcast 10.88.255.255ether d2:54:95:f4:14:99 txqueuelen 0 (Ethernet)RX packets 21025 bytes 162169296 (154.6 MiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 14137 bytes 20181898 (19.2 MiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
liruilonger@cloudshell:~$
netstat -i
命名查看
liruilonger@cloudshell:~$ netstat -i
Kernel Interface table
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
docker0 1460 0 0 0 0 0 0 0 0 BMU
eth0 1500 43104 0 0 0 31581 0 0 0 BMRU
lo 65536 28020 0 0 0 28020 0 0 0 LRU
tap0 1500 0 0 0 0 0 0 0 0 BMU
tap1 1500 0 0 0 0 0 0 0 0 BMU
啟用網卡的混雜模式
,可以使用下面這條命令:ifconfig eth0 promisc
liruilonger@cloudshell:~$ sudo ifconfig eth0 promisc
liruilonger@cloudshell:~$ ifconfig eth0
eth0: flags=4419<UP,BROADCAST,RUNNING,PROMISC,MULTICAST> mtu 1500inet 10.88.0.3 netmask 255.255.0.0 broadcast 10.88.255.255ether d2:54:95:f4:14:99 txqueuelen 0 (Ethernet)RX packets 21123 bytes 162180935 (154.6 MiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 14208 bytes 20193050 (19.2 MiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
可以看到多了一個 PROMISC 狀態
使網卡退出混雜模式
,可以使用下面這條命令:ifconfig eth0 -promisc
liruilonger@cloudshell:~$ sudo ifconfig eth0 -promisc
liruilonger@cloudshell:~$ ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 10.88.0.3 netmask 255.255.0.0 broadcast 10.88.255.255ether d2:54:95:f4:14:99 txqueuelen 0 (Ethernet)RX packets 23908 bytes 162504555 (154.9 MiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 16406 bytes 20537953 (19.5 MiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0liruilonger@cloudshell:~$ netstat -i
Kernel Interface table
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
docker0 1460 0 0 0 0 0 0 0 0 BMU
eth0 1500 43352 0 0 0 31769 0 0 0 BMPRU
lo 65536 28233 0 0 0 28233 0 0 0 LRU
tap0 1500 0 0 0 0 0 0 0 0 BMU
tap1 1500 0 0 0 0 0 0 0 0 BMU
liruilonger@cloudshell:~$
將網絡設備加入 Linux bridge
后,會自動進入混雜模式: 把一個 veth 虛擬設備添加到網橋
liruilonger@cloudshell:~$ sudo ip link add veth5 type veth
liruilonger@cloudshell:~$ sudo brctl addbr br5
liruilonger@cloudshell:~$ sudo brctl addif br5 veth5
liruilonger@cloudshell:~$ sudo dmesg | grep promiscuous
[ 2100.855052] device veth5 entered promiscuous mode
liruilonger@cloudshell:~$
如上所示,veth5
設備加入Linux bridge
后,可以通過查看內核日志看到 veth5
自動進入混雜模式,而且無法退出,直到將 veth5
從 Linux bridge
中移除。即使手動將網卡設置為非混雜模式,實際上還是沒有退出混雜模
liruilonger@cloudshell:~$ sudo brctl delif br5 veth5
liruilonger@cloudshell:~$ sudo dmesg | grep promiscuous
[ 2100.855052] device veth5 entered promiscuous mode
[ 2868.672747] device veth5 left promiscuous mode
liruilonger@cloudshell:~$
博文部分內容參考
? 文中涉及參考鏈接內容版權歸原作者所有,如有侵權請告知 😃
《 Kubernetes 網絡權威指南:基礎、原理與實踐》
? 2018-2024 liruilonger@gmail.com, All rights reserved. 保持署名-非商用-相同方式共享(CC BY-NC-SA 4.0)