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

利用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 <maxk@qualcomm.com>
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 << EOF > /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
EOF
yum -y --enablerepo=nux-misc install tunctl
[root@localhost ~]# cat << EOF > /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
> 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<UP,BROADCAST,MULTICAST>  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 ~]# 
#用完不需要了就可以删除哦,哈哈
最后呢希望对大家有帮助,欢迎留言问题哦,会尽我所能为大家解决呢。
  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在 Linux 中,可以使用 golang 来创建、启动、关闭和删除 tun 虚拟网卡。下面是一个简单的代码示例: ```golang package main import ( "log" "os/exec" ) const ( // tun 设备名称 tunName = "mytun" // tun 设备 IP 地址 tunIP = "10.0.0.1/24" ) func createTun() { // 创建 tun 设备 cmd := exec.Command("ip", "tuntap", "add", tunName, "mode", "tun") if err := cmd.Run(); err != nil { log.Fatalf("Failed to create tun device: %v", err) } } func startTun() { // 设置 tun 设备 IP 地址并启动 cmd := exec.Command("ip", "addr", "add", tunIP, "dev", tunName) if err := cmd.Run(); err != nil { log.Fatalf("Failed to set tun device IP address: %v", err) } cmd = exec.Command("ip", "link", "set", tunName, "up") if err := cmd.Run(); err != nil { log.Fatalf("Failed to start tun device: %v", err) } } func stopTun() { // 停止 tun 设备 cmd := exec.Command("ip", "link", "set", tunName, "down") if err := cmd.Run(); err != nil { log.Fatalf("Failed to stop tun device: %v", err) } } func deleteTun() { // 删除 tun 设备 cmd := exec.Command("ip", "tuntap", "del", tunName, "mode", "tun") if err := cmd.Run(); err != nil { log.Fatalf("Failed to delete tun device: %v", err) } } func main() { createTun() startTun() // do something with the tun device stopTun() deleteTun() } ``` 在上面的示例中,我们首先定义了 tun 设备名称和 IP 地址。然后,我们实现了四个函数:`createTun()`、`startTun()`、`stopTun()` 和 `deleteTun()`。这些函数分别用于创建、启动、停止和删除 tun 设备。 在 `createTun()` 函数中,我们使用 `ip tuntap add` 命令来创建 tun 设备。在 `startTun()` 函数中,我们使用 `ip addr add` 命令来设置 tun 设备的 IP 地址,并使用 `ip link set` 命令启动 tun 设备。在 `stopTun()` 函数中,我们使用 `ip link set` 命令停止 tun 设备。在 `deleteTun()` 函数中,我们使用 `ip tuntap del` 命令删除 tun 设备。 最后,在 `main()` 函数中,我们按照创建、启动、停止、删除的顺序调用这些函数,以完成对 tun 设备的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

huhy~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值