Creating tap/tun devices with IP tuntap and tunctl as detailed in Linux network tools

Time:2019-5-10

This article was first published in my public number.CloudDeveloper(ID: cloud_dev)Focus on dry goods sharing. There are a lot of books and videos in the number. Backstage reply「1024」You can get it. Welcome your attention. Two-dimensional code can be scanned at the end.

In the previous article, we have introduced the basic principles of tap/tun, and this article will show you how to use tools.tunctlandip tuntapTo create and use tap/tun devices.

tunctl

install

First of allcentosInstallation in Environmenttunctl

[[email protected] ~]# 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

ubuntuyesapt-get install uml-utilities

man tunctlSeetunctlThe manual is used as follows:

Synopsis
tunctl [ OPTIONS ] [ -u owner ] [-g group] [ -t device-name ]
  • – The u parameter specifies the user name, indicating that the interface is only controlled by the user, and that what happens to the interface does not affect the interface of the system.
  • – G Specifies a set of users
  • – t specifies the tap / Tun device name to create.

[OPTIONS]Part:

  • – B Simple Print Created Interface Name
  • – n Create Tun devices
  • – P creates a tap device, which is created by default
  • – F tun-clone-device specifies the file name corresponding to the Tun device by default/dev/net/tunSome systems are/dev/misc/net/tun
  • – D interfacename deletes the specified interface

Use

Common usage:

Create tap interface by default:

tunctl

The above is equivalent totunctl -p

For usersuserCreate a tap interface:

# tunctl -u user

Create Tun interface:

tunctl -n

Configure IP for the interface and enable:

# ifconfig tap0 192.168.0.254 up

Add routing to the interface:

# route add -host 192.168.0.1 dev tap0

Delete interface:

# tunctl -d tap0

ip tuntap

install

Command line inputip helpSeeipIs the command supported?tuntapTools, if supported, will be displayedtuntapOptions:

[[email protected] ~]# ip help
Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }
       ip [ -force ] -batch filename
where  OBJECT := { link | addr | addrlabel | route | rule | neigh | ntable |
                   tunnel | tuntap | maddr | mroute | mrule | monitor | xfrm |
                   netns | l2tp | tcp_metrics | token }

Upgrade or download the latest if not supportediproute2Toolkit, or use the one described abovetunctlTools.

Use

inputip tuntap helpView the Detailed Use Commands:

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

Where: USER  := { STRING | NUMBER }
       GROUP := { STRING | NUMBER }

Common usage:

Create tap/tun devices:

IP tuntap add dev tap0 mod tap # create tap 
IP tuntap add dev tun0 mod Tun # create Tun

Delete tap/tun devices:

IP tuntap del dev tap0 mod tap # delete tap 
IP tuntap del dev tun0 mod Tun # delete Tun

PS: userandgroupParameters andtunctlThe – U and – G parameters are the same.

We recommend using the above two tools.ip tuntapOne is becauseiproute2More complete updates have gradually replaced some of the older tools. Another reason is thattunctlIn someDebianClass is not fully supported on the system.

summary

tunctlandip tuntapCommon usage.

More recommendedip tuntapTools.


My Public NumberCloudDeveloper(ID: cloud_dev)There are a lot of books and videos in the number. Backstage reply「1024」You can get it. The content you share includes but is not limited to cloud computing virtualization, container, OpenStack, K8S, fog computing, network, tools, SDN, OVS, DPDK, Linux, Go, Python, C/C++ programming technology and so on. Welcome your attention.

Creating tap/tun devices with IP tuntap and tunctl as detailed in Linux network tools