在实验环境中,一直想在一台机器上模拟出多个客户端,这样在搭环境的时候就可以不用使用那么多的机器。但是,仅仅能在一个网卡上模拟出一个IP有时是不行的,需要mac地址也不一样。
对于这个需求,我一直想咋整,一直不知道,在优快云和Chinaunix上也发贴问,也没见着回的。而我知道,virtualBox和WM等虚拟机是可以实现的,它的虚拟机里发出数据包的mac就是和系统的mac不一样,也就是说,这个是可以实现的,只是我不知道方法而已。
后来,终于google到了一个叫mac-vlan的补丁(http://www.candelatech.com/~greear/vlan.html),mac-vlan代码在内核2.6.29版本及以后已经加入内核。它可以被ip工具集操作。
方法:
1.加载8021q模块:
#sudo modprobe 8021q
2. 启用目标网卡,在这块网卡上配置一个vlan。
#sudo ifconfig eth0 up
3.配置出一个vlan:
#sudo vconfig add eth0 2
Added VLAN with VID == 2 to IF -:eth0:-
系统会生成一个属于vlan2的网卡,可以用ifconfig -a来查看:
[moldao@moldao python]$ ifconfig eth0.2
eth0.2 Link encap:Ethernet HWaddr 00:1F:16:2E:28:C7
BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
- Implements 802.1Q VLAN spec.
- Can support up to 4094 VLANs per ethernet interface.
- Scales well in critical paths: O(n), where n is the number of PHYSICAL ethernet interfaces, and that is only on ingress. O(1) in every other critical path, as far as I know.
- Supports MULTICAST
- Can change MAC address of VLAN.
- Multiple naming conventions supported, and adjustable at runtime.
- Optional header-reordering, to make the VLAN interface look JUST LIKE an Ethernet interface. This fixes some problems with DHCPd and anything else that uses a SOCK_PACKET socket. Default setting is off, which works for every other protocol I know about, and is slightly faster.