TAP (as in network tap) simulates an Ethernet device and it operates with layer 2 packets such as Ethernet frames. TUN (as in network TUNnel) simulates a network layer device and it operates with layer 3 packets such as IP packets. TAP is used to create a network bridge, while TUN is used with routing.
Packets sent by an operating system via a TUN/TAP device are delivered to a user-space program that attaches itself to the device. A user-space program may also pass packets into a TUN/TAP device. In this case TUN/TAP device delivers (or "injects") these packets to the operating system network stack thus emulating their reception from an external source.
~# apt-get install uml-utilities bridge-utils
uml-utilities提供tunctl,用来生成tun接口(三层)
bridge_utils提供brctl,用来生成tap接口(二层)
tunctl语法
~# tunctl /?
Create: tunctl [-b] [-u owner] [-t device-name] [-f tun-clone-device]
Delete: tunctl -d device-name [-f tun-clone-device]
The default tun clone device is /dev/net/tun - some systems use
/dev/misc/net/tun instead
-b will result in brief output (just the device name)
brctl的语法
~# brctl --help
Usage: brctl [commands]
commands:
addbr <bridge> add bridge
delbr <bridge> delete bridge
addif <bridge> <device> add interface to bridge
delif <bridge> <device> delete interface from bridge
setageing <bridge> <time> set ageing time
setbridgeprio <bridge> <prio> set bridge priority
setfd <bridge> <time> set bridge forward delay
sethello <bridge> <time> set hello time
setmaxage <bridge> <time> set max message age
setpathcost <bridge> <port> <cost> set path cost
setportprio <bridge> <port> <prio> set port priority
show show a list of bridges
showmacs <bridge> show a list of mac addrs
showstp <bridge> show bridge stp info
stp <bridge> {on|off} turn stp on/off
创建一个虚拟接口green
更改/etc/network/interfaces
~#vi /etc/network/interfaces
auto br0
iface br0 inet static
bridge_ports eth0
address 192.168.1.3
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 61.177.7.1
~#/etc/init.d/networking restart
~# tunctl -u wxt -t green -f /dev/net/tun
Set 'green' persistent and owned by uid 1000
~# brctl addif br0 green
~# ifconfig green 192.168.1.6 netmask 255.255.255.0 broadcast 192.168.1.255 up
~# ifconfig green
green Link encap:Ethernet HWaddr 00:FF:BD:35:60:03
inet addr:192.168.1.6 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::2ff:bdff:fe35:6003/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:44385 overruns:0 carrier:0
collisions:0 txqueuelen:500
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)