How to make tap interfaces persistent after reboot?

36,092

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.

Share:
36,092

Related videos on Youtube

AJN
Author by

AJN

Updated on September 18, 2022

Comments

  • AJN
    AJN almost 2 years

    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?

  • velis
    velis over 6 years
    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
  • MariusMatutiae
    MariusMatutiae over 6 years
    @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?
  • velis
    velis over 6 years
    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
    velis over 6 years
    Turns out I was only missing an auto tap0 stanza. Gist updated accordingly.