首页 > 系统相关 >CentOS 7 Tunctl 安装 虚拟网卡

CentOS 7 Tunctl 安装 虚拟网卡

时间:2022-11-16 21:05:05浏览次数:42  
标签:do TAP CentOS nux Tunctl DEV ifconfig 网卡 tap


在计算机网络中,TUN与TAP是操作系统内核中的虚拟网络设备。不同于普通靠硬件网路板卡实现的设备,这些虚拟的网络设备全部用软件实现,并向运行于操作系统上的软件提供与硬件的网络设备完全相同的功能。
TAP 等同于一个以太网设备,它操作第二层数据包如以太网数据帧。TUN模拟了网络层设备,操作第三层数据包比如IP数据封包。
操作系统通过TUN/TAP设备向绑定该设备的用户空间的程序发送数据,反之,用户空间的程序也可以像操作硬件网络设备那样,通过TUN/TAP设备发送数据。在后种情况下,TUN/TAP设备向操作系统的网络栈投递(或“注入”)数据包,从而模拟从外部接受数据的过程。

1、确认内核是否有 tun 模块

modinfo tun

2、安装 tunctl

# 配置 YUM 源
vim /etc/yum.repos.d/nux-misc.repo

[nux-misc]
name=Nux Misc
baseurl=http://li.nux.ro/download/nux/misc/el7/x86_64/
enabled=0
gpgcheck=1
gpgkey=http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro

 3、安装

yum --enablerepo=nux-misc install tunctl

4、创建 虚拟网卡 tap0

tunctl -t tap0 -u root


# 删除
tunctl -d tap0

5、设置 虚拟网卡 IP 和 子网码

ifconfig tap0 192.168.88.1 netmask 255.255.255.0 promisc

6、查看,成功显示

[root@localhost ~]# ifconfig tap0

tap0: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500
inet 192.168.88.1 netmask 255.255.255.0 broadcast 192.168.88.255
ether 56:34:da:0b:62:98 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 0

7、设置开机启动服务

vim /etc/init.d/tuntap

#!/bin/bash
#
# config_tap Start up the tun/tap virtual nic
#
# chkconfig: 2345 55 25

USER="root"
TAP_NETWORK="192.168.88.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_DEV_NUM ${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

8、加入系统服务

chmod +x /etc/init.d/tuntap

chkconfig --add tuntap
chkconfig --level 345 tuntap on

9、启动

service tuntap start

 

标签:do,TAP,CentOS,nux,Tunctl,DEV,ifconfig,网卡,tap
From: https://blog.51cto.com/u_14508118/5857670

相关文章