# Setup VPN for WAN out only

**URL:** <https://dietpi.com/forum/t/setup-vpn-for-wan-out-only/25062>\
**Category:** Troubleshooting\
**Created:** [23 March 2026 13:30 UTC](https://dietpi.com/forum/t/setup-vpn-for-wan-out-only/25062 "2026-03-23T13:30:50Z")\
**Posts on this page:** 19\
**Page:** 2

<div class="post-metadata">

**Author:** ![Jappe](https://dietpi.com/forum/user_avatar/dietpi.com/jappe/32/1788_2.png) [@Jappe](https://dietpi.com/forum/u/Jappe)\
**Post date:** [28 March 2026 17:45 UTC](https://dietpi.com/forum/t/setup-vpn-for-wan-out-only/25062/21 "2026-03-28T17:45:06Z")

</div>

> [@miguelpruivo](#):
>
> What should I add and where exactly?

Bring the VPN down, with `dietpi-vpn` (to clear the unwanted `iptables` rules).

Check then your rules, we want `eth0` as default gateway and the kill-switch for transmission should also be still there , but I’m not sure if `dietpi-vpn` will also erase it when the interface goes down.  
Just check if the rule is still there, if not add it again.

> [@Jappe](#):
>
> ```auto
> sudo iptables -A OUTPUT -m owner --gid-owner debian-transmission ! -o tun0 -j REJECT
> sudo netfilter-persistent save
> 
> ```

* * *

Then you can bring up the tunnel with openvpn

```auto
sudo openvpn --config /path/to/vpn_config.opvn

```

I had also a look into transmission and you can directly specify an IP address where it should bind to. So no more hassle with iptable rules 😅

In `/etc/transmission-daemon/settings.json` is an option `bind-address-ipv4` where you can just put in the IP of the tunnel interface. (Check via `ip a`). I didn’t find a way to modify this via the web UI tho.

```auto
"bind-address-ipv4": "<VPN_IP_HERE>"

```

I hope this s\*\*t is working now, it drives me mad too 😅

---

<div class="post-metadata">

**Author:** ![miguelpruivo](https://dietpi.com/forum/user_avatar/dietpi.com/miguelpruivo/32/8837_2.png) [@miguelpruivo](https://dietpi.com/forum/u/miguelpruivo)\
**Post date:** [28 March 2026 22:40 UTC](https://dietpi.com/forum/t/setup-vpn-for-wan-out-only/25062/22 "2026-03-28T22:40:40Z")

</div>

I can’t get to the part of “trying” because it looks like the VPN as soon as it goes up, it will tunnel all traffic through it despite having the #redirect-gateway def1 commented in my openvpn config.

So first, I need to make sure the VPN isn’t tunneling anything until I explicitly tell it to tunnel (I suppose).

But I’m seeing you are getting a lot of work in helping me out with this, I may “give up” and search later on when I have some time for this. It has been quite a struggle, 1 week around this with no success.

---

<div class="post-metadata">

**Author:** ![Jappe](https://dietpi.com/forum/user_avatar/dietpi.com/jappe/32/1788_2.png) [@Jappe](https://dietpi.com/forum/u/Jappe)\
**Post date:** [29 March 2026 17:29 UTC](https://dietpi.com/forum/t/setup-vpn-for-wan-out-only/25062/23 "2026-03-29T17:29:21Z")

</div>

Hmm, can you share your actual config file again? Or ask your VPN provider if they send some push options when the connections is established, which probably sets the tun0 as default route again.

You can prevent such behaviour with`route-nopull` option. See [https://unix.stackexchange.com/questions/16500/how-to-refuse-routes-that-are-pushed-by-openvpn-server](https://unix.stackexchange.com/questions/16500/how-to-refuse-routes-that-are-pushed-by-openvpn-server)

---

<div class="post-metadata">

**Author:** ![miguelpruivo](https://dietpi.com/forum/user_avatar/dietpi.com/miguelpruivo/32/8837_2.png) [@miguelpruivo](https://dietpi.com/forum/u/miguelpruivo)\
**Post date:** [29 March 2026 19:58 UTC](https://dietpi.com/forum/t/setup-vpn-for-wan-out-only/25062/24 "2026-03-29T19:58:05Z")

</div>

```auto
client
remote REDACTED
dev tun
proto udp
auth-user-pass

resolv-retry infinite
#redirect-gateway def1
route 10.0.2.0 255.255.255.0 net_gateway
route 10.0.3.0 255.255.255.0 net_gateway
route 192.168.2.0 255.255.255.0 net_gateway
persist-key
persist-tun
nobind

data-ciphers AES-256-GCM:AES-128-GCM:AES-256-CBC
data-ciphers-fallback AES-256-CBC

auth SHA256
ping 5
explicit-exit-notify 2
script-security 2
remote-cert-tls server
route-delay 5
verb 4

```

@Jappe his is the client out of the box (I mean, I added the routes for my VLANs to not be routed through the VPN, but other than that, the config is as shown here).

---

<div class="post-metadata">

**Author:** ![Jappe](https://dietpi.com/forum/user_avatar/dietpi.com/jappe/32/1788_2.png) [@Jappe](https://dietpi.com/forum/u/Jappe)\
**Post date:** [30 March 2026 21:51 UTC](https://dietpi.com/forum/t/setup-vpn-for-wan-out-only/25062/25 "2026-03-30T21:51:36Z")

</div>

You can try to add the `route-nopull` option.  
Maybe @trendy has another idea?!

---

<div class="post-metadata">

**Author:** ![trendy](https://dietpi.com/forum/user_avatar/dietpi.com/trendy/32/61_2.png) [@trendy](https://dietpi.com/forum/u/trendy)\
**Post date:** [31 March 2026 09:46 UTC](https://dietpi.com/forum/t/setup-vpn-for-wan-out-only/25062/26 "2026-03-31T09:46:10Z")

</div>

Maybe it would be easier to setup a pbr rule to tell what will go through the wan first, then the rest will go through the VPN. I have done it in a similar way.

Add the default and some static routes in a custom table:

```auto
ip ro add default via 172.16.4.1 dev eth0 table 100

```

Then include rules for the traffic that will go through wan:

```auto
ip rule add not iif lo sport <transmission-port> to default lookup 100 prio 1000

```

So everything except traffic from transmission will go via wan.

You may use other descriptors, like source ip (in case it is predictable) or uid.

---

<div class="post-metadata">

**Author:** ![miguelpruivo](https://dietpi.com/forum/user_avatar/dietpi.com/miguelpruivo/32/8837_2.png) [@miguelpruivo](https://dietpi.com/forum/u/miguelpruivo)\
**Post date:** [31 March 2026 10:32 UTC](https://dietpi.com/forum/t/setup-vpn-for-wan-out-only/25062/27 "2026-03-31T10:32:32Z")

</div>

@trendy could you please guide me or where to put those? Sorry, at this point I’m afraid of doing it without being 100% sure because one time I locked myself from the SSH and my server is headless and sometimes I don’t even have physical access to it.

Also, with those rules, should I use the dietpi-vpn or no? And should I use it with the killswitch on? Should I use the no-pull or redirect-gateway def1 in the openvpn config?

Thank you all.

edit: I was trying to solve it myself and upon posting those 2 commands and your description on chatgpt it told me that the second command is saying that the traffic from transmission port is the one going through the WAN and not the opposite, but you said the opposite so now I am even more confused. I trust more on you than ChatGPT but just to make sure there is no mistakes.

I was trying to put this in the static-up rules from dietpi-vpn.

---

<div class="post-metadata">

**Author:** ![trendy](https://dietpi.com/forum/user_avatar/dietpi.com/trendy/32/61_2.png) [@trendy](https://dietpi.com/forum/u/trendy)\
**Post date:** [31 March 2026 23:07 UTC](https://dietpi.com/forum/t/setup-vpn-for-wan-out-only/25062/28 "2026-03-31T23:07:31Z")

</div>

These are simple commands for the prompt. They are lost after a reboot. So you could do a `shutdown -r +2` and it will reboot in 2 mins if you loose the connectivity.

---

<div class="post-metadata">

**Author:** ![miguelpruivo](https://dietpi.com/forum/user_avatar/dietpi.com/miguelpruivo/32/8837_2.png) [@miguelpruivo](https://dietpi.com/forum/u/miguelpruivo)\
**Post date:** [1 April 2026 10:34 UTC](https://dietpi.com/forum/t/setup-vpn-for-wan-out-only/25062/29 "2026-04-01T10:34:09Z")

</div>

Just ran those 2 commands directly (while dietpi-vpn was running with killswitch on) and the result was that I lost WAN on server but local network was still working.

I think you may have misunderstood the question: you are indeed forwarding transmission traffic to a specific (gateway IP) but I want to achieve is that I forward only transmission traffic to the tunnel created by dietpi-vpn and leave everything else just using the default wan. Problem is some kind of conflicts when dietpi-vpn starts that seems to mess with the tables.

---

<div class="post-metadata">

**Author:** ![trendy](https://dietpi.com/forum/user_avatar/dietpi.com/trendy/32/61_2.png) [@trendy](https://dietpi.com/forum/u/trendy)\
**Post date:** [2 April 2026 14:40 UTC](https://dietpi.com/forum/t/setup-vpn-for-wan-out-only/25062/30 "2026-04-02T14:40:40Z")

</div>

> [@miguelpruivo](#):
>
> you are indeed forwarding transmission traffic to a specific (gateway IP) but I want to achieve is that I forward only transmission traffic to the tunnel created by dietpi-vpn and leave everything else just using the default wan.

Yes, I understood that correctly.

> [@miguelpruivo](#):
>
> Problem is some kind of conflicts when dietpi-vpn starts that seems to mess with the tables.

Add this and try again:

`iptables -I OUTPUT -o eth0 -j ACCEPT`

Or disable the killswitch, you are not using it anyway.

---

<div class="post-metadata">

**Author:** ![miguelpruivo](https://dietpi.com/forum/user_avatar/dietpi.com/miguelpruivo/32/8837_2.png) [@miguelpruivo](https://dietpi.com/forum/u/miguelpruivo)\
**Post date:** [2 April 2026 23:56 UTC](https://dietpi.com/forum/t/setup-vpn-for-wan-out-only/25062/31 "2026-04-02T23:56:01Z")

</div>

> [@trendy](#):
>
> `ip rule add not iif lo sport <transmission-port> to default lookup 100 prio 1000`

I think it worked. The thing is… how do I know which ports transmission use for outbound communications? I know that I may use a specific one for incoming (if I open the port which I don’t) so I’m limited to the outbound peers which I connect to and have random ports I believe.

Setting the WebUI port from transmission doesn’t make sense I believe.

Example, this is what happens when I have transmission running:

```auto
tcp ESTAB 0 2525 10.16.4.131:56175 89.149.241.1:25136 users:(("transmission-da",pid=9842,fd=22))                                  
tcp ESTAB 0 0 10.16.4.131:45364 172.67.72.98:443 users:(("transmission-da",pid=9842,fd=52))                                  
tcp ESTAB 0 9 10.16.4.131:41119 85.242.144.182:65432 users:(("transmission-da",pid=9842,fd=56))                                  
tcp ESTAB 0 0 10.16.4.131:45338 172.67.72.98:443 users:(("transmission-da",pid=9842,fd=48))                                  
tcp ESTAB 0 3162 10.16.4.131:35407 94.185.80.230:58932 users:(("transmission-da",pid=9842,fd=37))                                  
tcp ESTAB 0 9 10.16.4.131:33253 109.50.173.14:56516 users:(("transmission-da",pid=9842,fd=41))                                  
tcp ESTAB 0 0 10.16.4.131:45300 172.67.72.98:443 users:(("transmission-da",pid=9842,fd=39))                                  
tcp ESTAB 0 0 10.16.4.131:45268 172.67.72.98:443 users:(("transmission-da",pid=9842,fd=32))                                  
tcp ESTAB 0 43 10.16.4.131:40839 85.241.129.90:1370 users:(("transmission-da",pid=9842,fd=47))                                  
tcp ESTAB 0 35 10.16.4.131:36277 79.127.131.215:40727 users:(("transmission-da",pid=9842,fd=30))                                  
tcp ESTAB 0 26 10.16.4.131:55647 158.173.152.51:48056 users:(("transmission-da",pid=9842,fd=44))                                  
tcp ESTAB 0 0 10.16.4.131:45290 172.67.72.98:443 users:(("transmission-da",pid=9842,fd=36))                                  
tcp ESTAB 0 0 10.16.4.131:45280 172.67.72.98:443 users:(("transmission-da",pid=9842,fd=33))                                  
tcp ESTAB 0 26 10.16.4.131:42065 188.250.132.179:51490 users:(("transmission-da",pid=9842,fd=34))                                  
tcp ESTAB 0 0 10.16.4.131:45348 172.67.72.98:443 users:(("transmission-da",pid=9842,fd=51))                                  
tcp ESTAB 0 9 10.16.4.131:42041 146.70.59.48:49313 users:(("transmission-da",pid=9842,fd=43))                                  
tcp ESTAB 0 0 10.16.4.131:38655 5.79.77.65:42771 users:(("transmission-da",pid=9842,fd=50))                                  
tcp ESTAB 0 0 10.16.4.131:44441 158.173.152.51:48056 users:(("transmission-da",pid=9842,fd=35))                                  
tcp ESTAB 0 146 10.16.4.131:33137 85.243.14.91:40202 users:(("transmission-da",pid=9842,fd=29))                                  
tcp ESTAB 0 485 10.16.4.131:48877 86.106.84.150:47846 users:(("transmission-da",pid=9842,fd=27))                                  
tcp ESTAB 0 9 10.16.4.131:44885 169.150.223.205:64613 users:(("transmission-da",pid=9842,fd=45))                                  
tcp ESTAB 0 0 10.16.4.131:50633 169.150.223.215:64062 users:(("transmission-da",pid=9842,fd=38))                                  
tcp ESTAB 0 0 10.16.4.131:45246 172.67.72.98:443 users:(("transmission-da",pid=9842,fd=21))        

```

Right now you see the source IP as the VPN IP because I have it enabled for everything (not transmission only) but the point is that source ports are a lot.

---

<div class="post-metadata">

**Author:** ![trendy](https://dietpi.com/forum/user_avatar/dietpi.com/trendy/32/61_2.png) [@trendy](https://dietpi.com/forum/u/trendy)\
**Post date:** [4 April 2026 21:43 UTC](https://dietpi.com/forum/t/setup-vpn-for-wan-out-only/25062/32 "2026-04-04T21:43:09Z")

</div>

There are the file transfer communications, which are sourcing from the `peer-port` in the configuration, remote control, which are sourcing from the rpc-port, and the rest (talking to trackers or updating blocklist ) which use random ports.

---

<div class="post-metadata">

**Author:** ![miguelpruivo](https://dietpi.com/forum/user_avatar/dietpi.com/miguelpruivo/32/8837_2.png) [@miguelpruivo](https://dietpi.com/forum/u/miguelpruivo)\
**Post date:** [5 April 2026 13:19 UTC](https://dietpi.com/forum/t/setup-vpn-for-wan-out-only/25062/33 "2026-04-05T13:19:43Z")

</div>

Unless I’m missing something, I believe that’s where the problem lies. How can I ensure that all torrent related traffic goes through the VPN if they use random ports? Maybe I misunderstood your post, but should I be concerned with the “random ports”, the RPC port, Peer Port or all? This may be beyond my knowledge.

Edit:

I think I finally managed to get it working, by doing the first part similar to what you suggest @trendy but then the second part I’m basically doing different, to sum it up:

```auto
sudo ip route add default via 172.16.4.1 dev eth0 table 100 // My gateway
sudo ip rule add to 172.16.4.0/24 lookup 100 prio 500 // All traffic through it
sudo ip rule add not uidrange 109-109 lookup 100 prio 1000 // But transmission UID don't

```

Where the 109 is the Transmission UID. From what I can tell it’s (finally) working and it uses all communications from that user through the VPN whereas the remaining stays on my default WAN/LAN.

---

<div class="post-metadata">

**Author:** ![trendy](https://dietpi.com/forum/user_avatar/dietpi.com/trendy/32/61_2.png) [@trendy](https://dietpi.com/forum/u/trendy)\
**Post date:** [6 April 2026 09:40 UTC](https://dietpi.com/forum/t/setup-vpn-for-wan-out-only/25062/34 "2026-04-06T09:40:40Z")

</div>

Very good! I believe it was suggested also in the earlier posts to use the uid to mark the transmission traffic.

Apart from that, you may keep the RPC via the normal gateway, so you can control the transmission locally if you wish.

---

<div class="post-metadata">

**Author:** ![miguelpruivo](https://dietpi.com/forum/user_avatar/dietpi.com/miguelpruivo/32/8837_2.png) [@miguelpruivo](https://dietpi.com/forum/u/miguelpruivo)\
**Post date:** [16 April 2026 16:15 UTC](https://dietpi.com/forum/t/setup-vpn-for-wan-out-only/25062/35 "2026-04-16T16:15:48Z")

</div>

Bumping this because as I have it, everything works perfectly but I noticed a major flaw: whenever the VPN goes down, Transmission is still allowed to fallback to WAN traffic. How can I prevent this? Thanks!

---

<div class="post-metadata">

**Author:** ![trendy](https://dietpi.com/forum/user_avatar/dietpi.com/trendy/32/61_2.png) [@trendy](https://dietpi.com/forum/u/trendy)\
**Post date:** [17 April 2026 08:47 UTC](https://dietpi.com/forum/t/setup-vpn-for-wan-out-only/25062/36 "2026-04-17T08:47:10Z")

</div>

@Jappe suggested a rule for iptables. Have you applied it?

> [@Setup VPN for WAN out only](https://dietpi.com/forum/t/setup-vpn-for-wan-out-only/25062/15):
>
> Ok if it’s only for tranmission we can use a simpler approach. First remove the stuff we added earlier # Remove MARK rules sudo iptables -t mangle -F OUTPUT # Remove policy routing rules (only the novpn table) sudo ip rule del fwmark 1 table novpn 2\>/dev/null sudo ip route flush table novpn 2\>/dev/null # Remove the novpn entry from rt\_tables sudo ip route del default via 192.168.2.1 dev eth0 table novpn sudo sed -i '/200 novpn/d' /etc/iproute2/rt\_tables Also remove the script from the ovpn …

---

<div class="post-metadata">

**Author:** ![miguelpruivo](https://dietpi.com/forum/user_avatar/dietpi.com/miguelpruivo/32/8837_2.png) [@miguelpruivo](https://dietpi.com/forum/u/miguelpruivo)\
**Post date:** [17 April 2026 10:34 UTC](https://dietpi.com/forum/t/setup-vpn-for-wan-out-only/25062/37 "2026-04-17T10:34:39Z")

</div>

@trendy all working now, not with that approach but basically doing this that I will leave here just in case someone needs it (or even myself later down the road if somehow I need it again). So.

1. I disabled the transmission service from starting automatically on boot (with the dietpi-services).

2. I setup the dietpi-vpn UP script with the following:

```auto
iptables -I INPUT 1 -s 192.168.2.0/24 -j ACCEPT // To allow my LAN traffic
sudo iptables -A OUTPUT -m owner --uid-owner 109 -d 172.16.4.0/24 -j ACCEPT
sudo iptables -A OUTPUT -m owner --uid-owner 109 -o eth0 -j REJECT
sudo ip route add default via 172.16.4.1 dev eth0 table 100
sudo ip rule add to 172.16.4.0/24 lookup 100 prio 500
sudo ip rule add not uidrange 109-109 lookup 100 prio 1000
service transmission-daemon start

```

Whereas 172.16.4.1 is my WAN gateway.

To sum it up what it does is first moving ALL the traffic through the WAN (instead of the tunnel) but leave the transmission (UID 109) aside. Then, starting transmission service only after all of this is set.

1. In the DOWN of the VPN, I basically stop the transmission service and that’s it.

Not sure if this approach has any downside that I’m not thinking of at the moment, but as far as I can tell, it seems to work just I want it:

1. All traffic uses WAN but transmission uses VPN (already verified in the torrent trackers what IP is being used) as well as it stops working if I stop the VPN.

To make it perfect, I’d only like to have as a complement rule something that would block UID 109 from EVER accessing WAN even if for some reason the dietpi-vpn fails and doesn’t trigger the DOWN script (assuming a bug or something). That would be perfect, but haven’t added it yet. But as it is, I think it’s clean and simple without messing up with a lot of tables, marking packets etc. but again, I’m no network expert in this regard.

Let me know if you come up with any idea @trendy @Jappe

---

<div class="post-metadata">

**Author:** ![trendy](https://dietpi.com/forum/user_avatar/dietpi.com/trendy/32/61_2.png) [@trendy](https://dietpi.com/forum/u/trendy)\
**Post date:** [19 April 2026 17:11 UTC](https://dietpi.com/forum/t/setup-vpn-for-wan-out-only/25062/38 "2026-04-19T17:11:38Z")

</div>

You have that already. You can put it in `rc.local` instead of vpn up script.

`iptables -A OUTPUT -m owner --uid-owner 109 -o eth0 -j REJECT`

---

<div class="post-metadata">

**Author:** ![system](https://dietpi.com/forum/uploads/default/original/1X/7e1021de2213860bce185915ce2c1786e56490ec.png) [@system](https://dietpi.com/forum/u/system)\
**Post date:** [18 July 2026 17:12 UTC](https://dietpi.com/forum/t/setup-vpn-for-wan-out-only/25062/39 "2026-07-18T17:12:30Z")

</div>

Closed on 18 July 2026 17:12 UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

[Previous page](https://dietpi.com/forum/t/setup-vpn-for-wan-out-only/25062.md?page=1)
