tunctl添加虚拟网卡TUN/TAP与brctl添加网桥
http://blog.csdn.net/zhaihaifei/article/details/23168621
1 Linux添加虚拟网卡三种方法
方法1: ifconfig命令创建\删除虚拟网卡
- ifconfig eth0:0 192.168.1.10 netmask 255.255.255.0 up
- ifconfig eth0:0 down
方法2: 修改网卡配置文件
在ubuntu下,修改网卡的配置文件/etc/network/interfaces:- cat /etc/network/interfaces
- auto eth0
- iface eth0 inet dhcp
- auto eth0:1
- iface eth0:1 inet static
- address 192.168.11.1
- netmask 255.255.255.0
- gateway 192.168.11.1
- auto eth0:2
- iface eth0:2 inet static
- address 192.168.12.1
- netmask 255.255.255.0
- gateway 192.168.12.1
- service networking restart
RHEL系统中,网卡IP配置的文件在/etc/sysconfig/network-scripts/下,文件分别为ehtx或ethx:x,执行命令如下:
- #cd /etc/sysconfig/network-scripts/
- #cp ifcfg-eth0 ifcfg-eth0:1
- #vi ifcfg-eth0:1
- DEVICE=eth0
- BOOTPROTO=none
- HWADDR=00:19:D1:24:2A:EC
- ONBOOT=yes
- DHCP_HOSTNAME=zhongqg.localdomain
- IPADDR=192.168.1.55
- NETMASK=255.255.252.0
- GATEWAY=192.168.0.1
- TYPE=Ethernet
- USERCTL=no
- IPV6INIT=no
- PEERDNS=yes
- #ifup eth0:1
方法3:创建tap
前两种方法都有一个特点,创建的网卡可有不同的ip地址,但是Mac地址相同,无法用来创建虚拟机。使用命令tunctl添加虚拟网卡tap。
2 tunctl安装与使用
2.1 安装apt-get install uml-utilities
- root@host:~# tunctl
- The program 'tunctl' is currently not installed. You can install it by typing:
- apt-get install uml-utilities
- root@host:~# apt-get install uml-utilities
- Reading package lists... Done
- Building dependency tree
- Reading state information... Done
- Suggested packages:
- user-mode-linux
- The following NEW packages will be installed:
- uml-utilities
- 0 upgraded, 1 newly installed, 0 to remove and 105 not upgraded.
- Need to get 61.9 kB of archives.
- After this operation, 267 kB of additional disk space will be used.
- Get:1 http://us.archive.ubuntu.com/ubuntu/ trusty/universe uml-utilities amd64 20070815-1.3ubuntu1 [61.9 kB]
- Fetched 61.9 kB in 2s (27.7 kB/s)
- Selecting previously unselected package uml-utilities.
- (Reading database ... 60223 files and directories currently installed.)
- Preparing to unpack .../uml-utilities_20070815-1.3ubuntu1_amd64.deb ...
- Unpacking uml-utilities (20070815-1.3ubuntu1) ...
- Processing triggers for ureadahead (0.100.0-16) ...
- ureadahead will be reprofiled on next reboot
- Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
- Setting up uml-utilities (20070815-1.3ubuntu1) ...
- * Starting User-mode networking switch uml_switch [ OK ]
- Processing triggers for ureadahead (0.100.0-16) ...
2.2 使用tunctl
NAMEtunctl — create and manage persistent TUN/TAP interfaces
SYNOPSIS
tunctl [-f tun-clone-device] [-u owner] [-t device-name]
tunctl [-f tun-clone-device] -d device-name
DESCRIPTION
This manual page documents briefly the tunctl command.
This manual page was written for the Debian GNU/Linux distribution because the original program does not have a manual
page. Instead, it has documentation in HTML format; see below.
tunctl allows the host sysadmin to preconfigure a TUN/TAP device for use by a particular user. That user may open and
use the device, but may not change any aspects of the host side of the interface.
USAGE
To create an interface for use by a particular user, invoke tunctl without the -d option:
tunctl -u someuser #Set 'tap0' persistent and owned by 'someuser'
Then, configure the interface as normal:
ifconfig tap0 192.168.0.254 up
route add -host 192.168.0.253 dev tap0
bash -c 'echo 1 > /proc/sys/net/ipv4/conf/tap0/proxy_arp'
arp -Ds 192.168.0.253 eth0 pub
To delete the interface, use the -d option:
tunctl -d tap0 #Set 'tap0' nonpersistent
3 TUN/TAP
tun/tap 驱动程序实现了虚拟网卡的功能,tun表示虚拟的是点对点设备,tap表示虚拟的是以太网设备,这两种设备针对网络包实施不同的封装。 利用tun/tap 驱动,可以将tcp/ip协议栈处理好的网络分包传给任何一个使用tun/tap驱动的进程,由进程重新处理后再发到物理链路中。3.1 Tun/Tap驱动程序工作原理
做为虚拟网卡驱动,Tun/Tap驱动程序的数据接收和发送并不直接和真实网卡打交道, 而是在Linux内核中添加了一个TUN/TAP虚拟网络设备的驱动程序和一个与之相关连的字符设备 /dev/net/tun,字符设备tun作为用户空间和内核空间交换数据的接口。当内核将数据包发送到虚拟网络设备时,数据包被保存在设备相关的一个队 列中,直到用户空间程序通过打开的字符设备tun的描述符读取时,它才会被拷贝到用户空间的缓冲区中,其效果就相当于,数据包直接发送到了用户空间。通过 系统调用write发送数据包时其原理与此类似。
在linux下,要实现 内核空间 和 用户空间 数据的交互,有多种方式:可以通用socket创建特殊套接字,利用套接字实现数据交互;通过proc文件系统创建文件来进行数据交互;还可以使用设备文件的方式,访问设备文件会调用设备驱动相应的例程,设备驱动本身就是 内核空间 和 用户空间 的一个接口,Tun/tap驱动就是利用设备文件实现 用户空间 和 内核空间 的数据交互。
从结构上来说,Tun/tap驱动并不单纯是实现网卡驱动,同时它还实现了字符设备驱动部分。以字符设备的方式连接用户空间和内核空间。
Tun/tap 驱动程序中包含两个部分,一部分是字符设备驱动,还有一部分是网卡驱动部分。利用网卡驱动部分接收来自TCP/IP协议栈的网络分包并发送或者反过来将接收到的网络分包传给协议栈处理,而字符驱动部分则将网络分包在 用户空间和内核空间 之间传送,模拟物理链路的数据接收和发送。Tun/tap驱动很好的实现了两种驱动的结合。
3.2 设置
3.2.1 确认内核是否支持tun/tap
确认内核是否有tun模块- [root@hunterfu]# modinfo tun
- filename: /lib/modules/2.6.34.7-56.fc13.i686.PAE/kernel/drivers/net/tun.ko
- alias: char-major-10-200
- license: GPL
- author: (C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>
- description: Universal TUN/TAP device driver
- srcversion: 880DE258930FE60D765B735
- depends:
- vermagic: 2.6.34.7-56.fc13.i686.PAE SMP mod_unload 686
加载内核模块
- [root@hunterfu ~]# modprobe tun
- [root@hunterfu ~]# lsmod | grep tun
- tun 10548 1
3.2.2 创建和配置虚拟网卡
确认是否有tunctl命令,如果没有通过yum安装即可apt-get install uml-utilities 或 yum install tunctl
创建虚拟网卡设备
tunctl -t tap0 -u root
设置虚拟网卡
ifconfig tap0 192.168.0.1 netmask 255.255.255.0 promisc
经过如上操作后,虚拟网卡已经建立和配置好了。
3.2.3 作为系统服务随系统自动启动创建虚拟网卡
编写配置脚本(符合chkconfig规范)- [root@hunterfu ~]# cat /etc/init.d/config_tap
- #!/bin/bash
- #
- # config_tap Start up the tun/tap virtual nic
- #
- # chkconfig: 2345 55 25
- USER="root"
- TAP_NETWORK="192.168.0.1"
- TAP_DEV_NUM=0
- DESC="TAP config"
- do_start() {
- if [ ! -x /usr/sbin/tunctl ]; then
- echo "/usr/sbin/tunctl was NOT found!"
- exit 1
- fi
- tunctl -t tap$TAP_DEV_NUM -u root
- ifconfig tap{TAP_NETWORK} netmask 255.255.255.0 promisc
- ifconfig tap$TAP_DEV_NUM
- }
- do_stop() {
- ifconfig tap$TAP_DEV_NUM down
- }
- do_restart() {
- do_stop
- do_start
- }
- check_status() {
- ifconfig tap$TAP_DEV_NUM
- }
- case $1 in
- start) do_start;;
- stop) do_stop;;
- restart) do_restart;;
- status)
- echo "Status of $DESC: "
- check_status
- exit "$?"
- ;;
- *)
- echo "Usage: $0 {start|stop|restart|status}"
- exit 1
- esac
可以根据具体需求修改此脚本
加入到系统服务中
- [root@hunterfu ~]# chkconfig --add config_tap
- [root@hunterfu ~]# chkconfig --level 345 config_tap on
4 tunctl添加tap并用brctl添加到网桥
- root@host# ifconfig -a
- eth0 Link encap:Ethernet HWaddr fa:16:3e:7b:4e:e1
- inet addr:192.168.33.24 Bcast:192.168.47.255 Mask:255.255.240.0
- inet6 addr: fe80::f816:3eff:fe7b:4ee1/64 Scope:Link
- UP BROADCAST RUNNING MULTICAST MTU:1454 Metric:1
- RX packets:5922 errors:0 dropped:0 overruns:0 frame:0
- TX packets:2002 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:1000
- RX bytes:503405 (503.4 KB) TX bytes:322612 (322.6 KB)
- lo Link encap:Local Loopback
- inet addr:127.0.0.1 Mask:255.0.0.0
- inet6 addr: ::1/128 Scope:Host
- UP LOOPBACK RUNNING MTU:65536 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)
- root@host# tunctl
- Set 'tap0' persistent and owned by uid 0
- root@host# ifconfig -a
- eth0 Link encap:Ethernet HWaddr fa:16:3e:7b:4e:e1
- inet addr:192.168.33.24 Bcast:192.168.47.255 Mask:255.255.240.0
- inet6 addr: fe80::f816:3eff:fe7b:4ee1/64 Scope:Link
- UP BROADCAST RUNNING MULTICAST MTU:1454 Metric:1
- RX packets:6089 errors:0 dropped:0 overruns:0 frame:0
- TX packets:2057 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:1000
- RX bytes:514735 (514.7 KB) TX bytes:339978 (339.9 KB)
- lo Link encap:Local Loopback
- inet addr:127.0.0.1 Mask:255.0.0.0
- inet6 addr: ::1/128 Scope:Host
- UP LOOPBACK RUNNING MTU:65536 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)
- tap0 Link encap:Ethernet HWaddr 7e:13:f1:29:c0:ed
- 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:500
- RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
- root@host# tunctl
- Set 'tap1' persistent and owned by uid 0
- root@host# ifconfig -a
- eth0 Link encap:Ethernet HWaddr fa:16:3e:7b:4e:e1
- inet addr:192.168.33.24 Bcast:192.168.47.255 Mask:255.255.240.0
- inet6 addr: fe80::f816:3eff:fe7b:4ee1/64 Scope:Link
- UP BROADCAST RUNNING MULTICAST MTU:1454 Metric:1
- RX packets:6323 errors:0 dropped:0 overruns:0 frame:0
- TX packets:2116 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:1000
- RX bytes:530988 (530.9 KB) TX bytes:347960 (347.9 KB)
- lo Link encap:Local Loopback
- inet addr:127.0.0.1 Mask:255.0.0.0
- inet6 addr: ::1/128 Scope:Host
- UP LOOPBACK RUNNING MTU:65536 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)
- tap0 Link encap:Ethernet HWaddr 7e:13:f1:29:c0:ed
- 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:500
- RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
- tap1 Link encap:Ethernet HWaddr 7e:6a:fc:31:18:35
- 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:500
- RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
- root@host# brctl show
- bridge name bridge id STP enabled interfaces
- root@host# ifconfig tap0 0.0.0.0
- root@host# ifconfig tap1 0.0.0.0
- root@host# ifconfig -a
- eth0 Link encap:Ethernet HWaddr fa:16:3e:7b:4e:e1
- inet addr:192.168.33.24 Bcast:192.168.47.255 Mask:255.255.240.0
- inet6 addr: fe80::f816:3eff:fe7b:4ee1/64 Scope:Link
- UP BROADCAST RUNNING MULTICAST MTU:1454 Metric:1
- RX packets:7246 errors:0 dropped:0 overruns:0 frame:0
- TX packets:2525 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:1000
- RX bytes:595705 (595.7 KB) TX bytes:545518 (545.5 KB)
- lo Link encap:Local Loopback
- inet addr:127.0.0.1 Mask:255.0.0.0
- inet6 addr: ::1/128 Scope:Host
- UP LOOPBACK RUNNING MTU:65536 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)
- tap0 Link encap:Ethernet HWaddr 7e:13:f1:29:c0:ed
- UP 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:500
- RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
- tap1 Link encap:Ethernet HWaddr 7e:6a:fc:31:18:35
- UP 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:500
- RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
- root@host# brctl addbr br-zhai
- root@host# brctl addif br-zhai tap0
- root@host# brctl addif br-zhai tap1
- root@host# ifconfig -a
- br-zhai Link encap:Ethernet HWaddr 7e:13:f1:29:c0:ed
- 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)
- eth0 Link encap:Ethernet HWaddr fa:16:3e:7b:4e:e1
- inet addr:192.168.33.24 Bcast:192.168.47.255 Mask:255.255.240.0
- inet6 addr: fe80::f816:3eff:fe7b:4ee1/64 Scope:Link
- UP BROADCAST RUNNING MULTICAST MTU:1454 Metric:1
- RX packets:7515 errors:0 dropped:0 overruns:0 frame:0
- TX packets:2616 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:1000
- RX bytes:615539 (615.5 KB) TX bytes:561216 (561.2 KB)
- lo Link encap:Local Loopback
- inet addr:127.0.0.1 Mask:255.0.0.0
- inet6 addr: ::1/128 Scope:Host
- UP LOOPBACK RUNNING MTU:65536 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)
- tap0 Link encap:Ethernet HWaddr 7e:13:f1:29:c0:ed
- UP 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:500
- RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
- tap1 Link encap:Ethernet HWaddr 7e:6a:fc:31:18:35
- UP 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:500
- RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
- root@host# brctl show
- bridge name bridge id STP enabled interfaces
- br-zhai 8000.7e13f129c0ed no tap0
- tap1
- root@host# ifconfig br-zhai 192.168.9.1 up
- root@host# ifconfig br-zhai
- br-zhai Link encap:Ethernet HWaddr 7e:13:f1:29:c0:ed
- inet addr:192.168.9.1 Bcast:192.168.9.255 Mask:255.255.255.0
- UP 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)
- root@host# brctl showmacs br-zhai
- port no mac addr is local? ageing timer
- 1 7e:13:f1:29:c0:ed yes 0.00
- 2 7e:6a:fc:31:18:35 yes 0.00
- root@host# brctl
- 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
- hairpin <bridge> <port> {on|off} turn hairpin on/off
- 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 [ <bridge> ] 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
- root@host# ifconfig tap0 promisc
- root@host# ifconfig
- br-zhai Link encap:Ethernet HWaddr 7e:13:f1:29:c0:ed
- inet addr:192.168.9.1 Bcast:192.168.9.255 Mask:255.255.255.0
- UP 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)
- eth0 Link encap:Ethernet HWaddr fa:16:3e:7b:4e:e1
- inet addr:192.168.33.24 Bcast:192.168.47.255 Mask:255.255.240.0
- inet6 addr: fe80::f816:3eff:fe7b:4ee1/64 Scope:Link
- UP BROADCAST RUNNING MULTICAST MTU:1454 Metric:1
- RX packets:115463 errors:0 dropped:0 overruns:0 frame:0
- TX packets:6834 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:1000
- RX bytes:11942923 (11.9 MB) TX bytes:1083602 (1.0 MB)
- lo Link encap:Local Loopback
- inet addr:127.0.0.1 Mask:255.0.0.0
- inet6 addr: ::1/128 Scope:Host
- UP LOOPBACK RUNNING MTU:65536 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)
- tap0 Link encap:Ethernet HWaddr 7e:13:f1:29:c0:ed
- UP BROADCAST PROMISC 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:500
- RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
- tap1 Link encap:Ethernet HWaddr 7e:6a:fc:31:18:35
- UP 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:500
- RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
参考:
1 Linux操作系统下Virtual Box的网络设置:http://www.51cto.com/art/200709/56461.htm
多网卡绑定、
IP
别名
2 Linux多网卡绑定、IP别名:https://wenku.baidu.com/view/6a4c0e6f31126edb6f1a10a4.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通