# Use same host tunnel after server restart

**URL:** <https://forum.pritunl.com/t/use-same-host-tunnel-after-server-restart/3328>\
**Category:** Pritunl\
**Created:** [May 19, 2025, 9:04am UTC](https://forum.pritunl.com/t/use-same-host-tunnel-after-server-restart/3328 "2025-05-19T09:04:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kkalugeroff](https://forum.pritunl.com/user_avatar/forum.pritunl.com/kkalugeroff/32/1298_2.png) [@kkalugeroff](https://forum.pritunl.com/u/kkalugeroff)\
**Post date:** [May 19, 2025, 9:04am UTC](https://forum.pritunl.com/t/use-same-host-tunnel-after-server-restart/3328/1 "2025-05-19T09:04:18Z")

</div>

Is it possible to use same host tunnel every time Pritunl VPN server is restarted due to whatever reasons ? If so how can we achieve this? Basically I’m searching for functioanaility that can reserve the host tunnel somehow because I’m adding additional iptables rules used to limit the peer to peer communication.

Thanks!

---

<div class="post-metadata">

**Author:** ![zach](https://forum.pritunl.com/user_avatar/forum.pritunl.com/zach/32/1105_2.png) [@zach](https://forum.pritunl.com/u/zach)\
**Post date:** [May 20, 2025, 12:57am UTC](https://forum.pritunl.com/t/use-same-host-tunnel-after-server-restart/3328/2 "2025-05-20T00:57:22Z")

</div>

If you’re referring to the tunnel interface the best way to handle that is with the `server_start` and `server_stop` plugin handlers. This will include the `interface` and `interface_wg` variables that can then be used to create iptables rules. The [**plugin documentation**](https://docs.pritunl.com/docs/plugins) has more information on configuring plugins.

---

<div class="post-metadata">

**Author:** ![kkalugeroff](https://forum.pritunl.com/user_avatar/forum.pritunl.com/kkalugeroff/32/1298_2.png) [@kkalugeroff](https://forum.pritunl.com/u/kkalugeroff)\
**Post date:** [May 20, 2025, 7:39am UTC](https://forum.pritunl.com/t/use-same-host-tunnel-after-server-restart/3328/3 "2025-05-20T07:39:01Z")

</div>

I may not have explained my idea clearly earlier, so here’s an update: Every time a Pritunl VPN server starts, it assigns a random tunnel on the host where the Pritunl service is running. The issue I’m encountering is that each time the VPN server is stopped and restarted, a new host tunnel is assigned.

For example, the first time I start my Pritunl VPN server with the CIDR `172.16.0.0/24`, the Linux host assigns it the tunnel `tun10`. After starting the server, I decide to add new routes. To do this, I need to stop the server, add the routes, and then restart it. However, when I restart the server, instead of reusing `tun10`, a new tunnel, `tun11`, is assigned for the same VPN server.

Is there a way to configure the VPN server to reuse the same tunnel (e.g., `tun10`) instead of creating a new one? Is this behavior configurable at all?

---

<div class="post-metadata">

**Author:** ![zach](https://forum.pritunl.com/user_avatar/forum.pritunl.com/zach/32/1105_2.png) [@zach](https://forum.pritunl.com/u/zach)\
**Post date:** [May 20, 2025, 8:38am UTC](https://forum.pritunl.com/t/use-same-host-tunnel-after-server-restart/3328/4 "2025-05-20T08:38:24Z")

</div>

There isn’t a way to assign the same tunnel interface but it is included in the plugin calls.

---

<div class="post-metadata">

**Author:** ![kkalugeroff](https://forum.pritunl.com/user_avatar/forum.pritunl.com/kkalugeroff/32/1298_2.png) [@kkalugeroff](https://forum.pritunl.com/u/kkalugeroff)\
**Post date:** [May 20, 2025, 12:32pm UTC](https://forum.pritunl.com/t/use-same-host-tunnel-after-server-restart/3328/5 "2025-05-20T12:32:25Z")

</div>

How this can be used ? Could you provide an example ?

---

<div class="post-metadata">

**Author:** ![zach](https://forum.pritunl.com/user_avatar/forum.pritunl.com/zach/32/1105_2.png) [@zach](https://forum.pritunl.com/u/zach)\
**Post date:** [May 21, 2025, 1:54pm UTC](https://forum.pritunl.com/t/use-same-host-tunnel-after-server-restart/3328/6 "2025-05-21T13:54:21Z")

</div>

Run `sudo nano /var/lib/pritunl/plugins/custom_iptables.py` then add the code below. Then run `sudo systemctl restart pritunl`.

```python
from pritunl import logger
import subprocess

def server_start(host_id, host_name, server_id, server_name, network,
    network_wg, interface, interface_wg, **kwargs):

    logger.info('Adding custom iptables', 'plugin',
        server_name=server_name,
        interface=interface,
    )

    try:
        subprocess.check_call([
            'iptables',
            '-I', 'FORWARD',
            '-i', interface,
            '-p', 'tcp',
            '--dport', '22',
            '-j', 'DROP'
        ])
    except:
        logger.exception('Failed to add custom iptables', 'plugin',
            server_name=server_name,
            interface=interface,
        )
        raise

def server_stop(host_id, host_name, server_id, server_name, network,
    network_wg, interface, interface_wg, **kwargs):

    logger.info('Removing custom iptables', 'plugin',
        server_name=server_name,
        interface=interface,
    )

    try:
        subprocess.check_call([
            'iptables',
            '-D', 'FORWARD',
            '-i', interface,
            '-p', 'tcp',
            '--dport', '22',
            '-j', 'DROP'
        ])
    except:
        logger.exception('Failed to remove custom iptables', 'plugin',
            server_name=server_name,
            interface=interface,
        )
        raise

```

---

<div class="post-metadata">

**Author:** ![kkalugeroff](https://forum.pritunl.com/user_avatar/forum.pritunl.com/kkalugeroff/32/1298_2.png) [@kkalugeroff](https://forum.pritunl.com/u/kkalugeroff)\
**Post date:** [May 21, 2025, 2:29pm UTC](https://forum.pritunl.com/t/use-same-host-tunnel-after-server-restart/3328/7 "2025-05-21T14:29:16Z")

</div>

> [@zach](#):
>
> plugins

So, if I understand correctly, plugins provide additional functionality that takes effect after restarting the Pritunl service. Please correct me if I’m mistaken. This approach might work if you don’t require dynamically changing host tunnels each time the service restarts. However, how would I handle scenarios where I need to use the tunnel created by the Pritunl VPN server as input/output interfaces in my `iptables` rules?

---

<div class="post-metadata">

**Author:** ![zach](https://forum.pritunl.com/user_avatar/forum.pritunl.com/zach/32/1105_2.png) [@zach](https://forum.pritunl.com/u/zach)\
**Post date:** [May 21, 2025, 3:21pm UTC](https://forum.pritunl.com/t/use-same-host-tunnel-after-server-restart/3328/8 "2025-05-21T15:21:25Z")

</div>

No the plugin files stored in that directory are loaded when the service restarts. The plugin functions are called at different times for each handler. When each function is called is documented in the example in the [**plugins documentation**](https://docs.pritunl.com/docs/plugins). The `server_start` function is called every time a server starts and `server_stop` is called every time a server stops.
