利用TUN创建虚拟网络(每一步详细讲解)

您所在的位置:网站首页 linux怎么开启网口 利用TUN创建虚拟网络(每一步详细讲解)

利用TUN创建虚拟网络(每一步详细讲解)

2024-07-16 21:11| 来源: 网络整理| 查看: 265

利用TUN创建虚拟网络 前言

TUN/TAP是操作Linux操作系统内核中的虚拟网络设备。如果想要具体了解的可以去问“度娘”,哪里介绍的专业多了,我就简单的创建一个TAP网卡,通过本地来检查TAP网卡的功能

1、检查TUN模块

modinfo tun

#一般只要Linux内核在2.4后的都有tun/tap模块的,如果内核版本太低的话就要手动更新了

[root@localhost ~]# modinfo tun filename: /lib/modules/3.10.0-1062.el7.x86_64/kernel/drivers/net/tun.ko.xz alias: devname:net/tun alias: char-major-10-200 license: GPL author: (C) 1999-2004 Max Krasnyansky description: Universal TUN/TAP device driver retpoline: Y rhelversion: 7.7 srcversion: 3DDCBF9520BF658006099B2 depends: intree: Y vermagic: 3.10.0-1062.el7.x86_64 SMP mod_unload modversions signer: CentOS Linux kernel signing key sig_key: 51:08:4E:41:88:03:02:BE:5C:B0:74:AC:0D:A3:FE:10:23:3B:7F:1C sig_hashalgo: sha256

2、加载TUN模块

modprobe tun [root@localhost ~]# modprobe tun [root@localhost ~]#

#查看我们是否加载成功

lsmod | grep tun [root@localhost ~]# lsmod | grep tun tun 32026 0 [root@localhost ~]#

3、安装管理工具tunctl(重点,敲黑板!!)

#方式一,通过rpm包进行安装,这个需要把rpm包上传到本地

[root@localhost ~]# ls anaconda-ks.cfg tunctl-1.5-26.14.x86_64.rpm yum -y localinstall tunctl-1.5-26.14.x86_64.rpm [root@localhost ~]# yum -y localinstall tunctl-1.5-26.14.x86_64.rpm Loaded plugins: fastestmirror Examining tunctl-1.5-26.14.x86_64.rpm: tunctl-1.5-26.14.x86_64 tunctl-1.5-26.14.x86_64.rpm: does not update installed package. Nothing to do #这里我下好了

#方式二、通过配置yum下载,

cat [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 > EOF [root@localhost ~]# yum -y --enablerepo=nux-misc install tunctl Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile file:///opt/centos/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /opt/centos/repodata/repomd.xml" Trying other mirror. nux-misc | 2.9 kB 00:00:00 nux-misc/primary_db | 63 kB 00:00:01 Package matching tunctl-1.5-12.el7.nux.x86_64 already installed. Checking for update. Nothing to do #这里只是只需简单两步,这里也是下好的

4、创建虚拟网卡(命令随意)

tunctl -b -u root -t tap0 [root@localhost ~]# tunctl -b -u root -t tap0 tap0 #这里可以看见创建好了

5、为虚拟网卡分配临时的ip

ifconfig tap0 10.0.1.2/24

6、愉快的创建完后我们可以查看了

[root@localhost ~]# ifconfig tap0 tap0: flags=4099 mtu 1500 inet 10.0.1.2 netmask 255.255.255.0 broadcast 10.0.1.255 ether f2:b1:d6:3f:6d:36 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 #从上面我们可以查看网卡的基本信息了,像IP,掩码,等

7、检查联通性

ping 10.0.1.2 [root@localhost ~]# ping 10.0.1.2 PING 10.0.1.2 (10.0.1.2) 56(84) bytes of data. 64 bytes from 10.0.1.2: icmp_seq=1 ttl=64 time=0.028 ms 64 bytes from 10.0.1.2: icmp_seq=2 ttl=64 time=0.035 ms 64 bytes from 10.0.1.2: icmp_seq=3 ttl=64 time=0.045 ms 64 bytes from 10.0.1.2: icmp_seq=4 ttl=64 time=0.038 ms 64 bytes from 10.0.1.2: icmp_seq=5 ttl=64 time=0.069 ms #可以看到本地是可以访问的,希望大家也能完美运行哦

8、删除网卡

tunctl -d tap0 [root@localhost ~]# tunctl -d tap0 Set 'tap0' nonpersistent [root@localhost ~]# ifconfig tap0 tap0: error fetching interface information: Device not found [root@localhost ~]# #用完不需要了就可以删除哦,哈哈 最后呢希望对大家有帮助,欢迎留言问题哦,会尽我所能为大家解决呢。


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3