How to make tap interfaces persistent after reboot?

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?


Solution 1:

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.