24

Some tasks requires having tap interfaces configured + assign ownership. So, I am doing it manually:

sudo tuntap -u <username>
sudo ifconfig tap0 up
sudo ip a a 192.168.1.1/24 dev tap0

or using

ip tuntap add dev tap0 mode tap user <username>

How can I make tap interfaces configuration peristent after reboots without adding these commands to a shell script and add to startup

What I have in mind is doing it through /etc/network/interfaces like the following:

iface tap1 inet static
address 192.168.1.121
netmask 255.255.255.0
pre-up /usr/sbin/tunctl -u ajn -t tap1

But for some reason, it doesn't work.

Any ideas?

21

I cannot see, for the life of me, why this question should be down-voted. It is clear, correct, it has a well-defined answer. I have upvoted it.

You are using obsolete utilities like tunctl, you should use ip instead. The correct stanza for /etc/network/interfaces is:

    iface tap1 inet manual 
    pre-up ip tuntap add tap1 mode tap user root
    pre-up ip addr add 192.168.1.121/24 dev tap1
    up ip link set dev tap1 up
    post-up ip route del 192.168.1.0/24 dev tap1 
    post-up ip route add 192.168.1.121/32 dev tap1
    post-down ip link del dev tap1

Your mistake was in using static instead of manual. The reason is that, since you are trying to give to the virtual interface an address in the same subnet as your main interfae (wlan0/eth0), when it tries automatically to add a local route,

    ip route add 192.168.1.0/24 dev tap1

it finds that such a route already exists, and it complains. If you use manual instead of static, you are allowed to delete this route, which is of course useless.

Also, you should add a route

     ip route add 192.168.1.121/32 dev tap1

to inform your kernel that there is an exception to the route

     ip route add 192.168.1.0/24 dev eth0/wlan0 

That's all.

| improve this answer | |
  • Something doesn't work for me here on ubuntu 17.10: my tun0 is not created - service networking status says that interface tun0 does not exist. Here's the interfaces file if anyone cares to take a look: gist.github.com/velis74/ab75a46893eaed8bd08b8c6292b2737a – velis Jan 8 '18 at 7:47
  • @velis Your new interface is called tap0, not tun0, that´s why it is not found. Please notice that tun and tap interfaces are fundamentally different, en.wikipedia.org/wiki/TUN/TAP, which one do you wish to create? – MariusMatutiae Jan 8 '18 at 9:03
  • Yes, it's called tap0. This answer is about creating a tap device, not a tun one. I fail to see how this can be the cause of my failure. The pre-up add device command executes perfectly from command line. – velis Jan 8 '18 at 10:31
  • Turns out I was only missing an auto tap0 stanza. Gist updated accordingly. – velis Jan 8 '18 at 10:51
0

There are a few more steps you might need to do:

  1. Add a new routing table Edit /etc/iproute2/rt_tables to add a new routing table. Call it routing table “rt2” and set its preferences to 1:
    55     local
    254     main
    253     default
    0       unspec
    1       rt2
  1. As explained in the previous answer, create a tap interface, but then you need to configure the new routing tables, and set routing rules. Add to /etc/network/interfaces:
   #create a tap interface and make it persistent
    iface tap1 inet manual 
    pre-up ip tuntap add tap1 mode tap user root
    pre-up ip addr add 192.168.1.121/24 dev tap1
    up ip link set dev tap1 up
    post-up ip route del 192.168.1.0/24 dev tap1 
    post-up ip route add 192.168.1.121/32 dev tap1
    post-down ip link del dev tap1

    #configure the new routing table so that network 192.168.1.0 can be reached through the tap1 interface
    post-up ip route add 192.168.1.0/24 dev tap1 src 192.168.1.121 table rt2

    #set the default gateway to be 192.168.1.10
    post-up ip route add default via 192.168.1.10 dev tap1 table rt2

    #set rules so that traffic from and to 192.168.1.121 use the rt2 routing table 
    post-up ip rule add from 192.168.1.121/24 table rt2
    post-up ip rule add to 192.168.1.121/24 table rt2
sudo ifup tap1

To test it:

ip route list table rt2
ip rule show
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.