跳转至

Linux 创建虚拟网卡

Ubuntu 安装tunctl 命令:

sudo apt-get install uml-utilities

确认内核是否加载run 模块

modinfo tun

zcat /proc/config.gz | grep TUN

方式一:通过tunctl创建虚拟网卡以及设置

tunctl -t tap0
ifconfig tap0 192.168.0.1 netmask 255.255.255.0 promisc

image-20240813172823798

方式二:通过ip tuntap 创建虚拟网卡以及设置

# ip help
Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }
       ip [ -force ] -batch filename
where  OBJECT := { link | address | addrlabel | route | rule | neigh | ntable |
                   tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm |
                   netns | l2tp | fou | macsec | tcp_metrics | token | netconf | ila |
                   vrf | sr }
       OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |
                    -h[uman-readable] | -iec |
                    -f[amily] { inet | inet6 | ipx | dnet | mpls | bridge | link } |
                    -4 | -6 | -I | -D | -B | -0 |
                    -l[oops] { maximum-addr-flush-attempts } | -br[ief] |
                    -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |
                    -rc[vbuf] [size] | -n[etns] name | -a[ll] | -c[olor]}

# ip tuntap help
Usage: ip tuntap { add | del } [ dev PHYS_DEV ] 
          [ mode { tun | tap } ] [ user USER ] [ group GROUP ]
          [ one_queue ] [ pi ] [ vnet_hdr ]

Where: USER  := { STRING | NUMBER }
       GROUP := { STRING | NUMBER }
ip tuntap add dev tap3 mod tap 
ifconfig tap3 192.168.9.1 netmask 255.255.255.0 promisc

image-20240814102726896

tunctl 安装和源码编译:

在线安装:

apt install uml-utilities

源码安装:

git clone https://github.com/gooselinux/tunctl.git
cd tunctl 
## 假如需要交叉编译,修改export CC=XXXXX 编译器 
############################
## 静态编译的话,Makefile 中添加
## $(CC) $(CFLAGS) $(LDFLAGS) -o $(BIN) $(BIN).c
##  make LDFLAGS="-static"
############################
make 

./tunctl --help

运行:

# ./tunctl
Set 'tap2' persistent and owned by uid 0

./tunctl -t tap7
ifconfig tap7 192.168.80.1 netmask 255.255.255.0 promisc