# VPN out + OpenVPN in

**URL:** <https://dietpi.com/forum/t/vpn-out-openvpn-in/5147>\
**Category:** Troubleshooting\
**Created:** [20 March 2021 21:00 UTC](https://dietpi.com/forum/t/vpn-out-openvpn-in/5147 "2021-03-20T21:00:36Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![cotarelo](https://dietpi.com/forum/letter_avatar_proxy/v4/letter/c/bc8723/32.png) [@cotarelo](https://dietpi.com/forum/u/cotarelo)\
**Post date:** [20 March 2021 21:00 UTC](https://dietpi.com/forum/t/vpn-out-openvpn-in/5147/1 "2021-03-20T21:00:36Z")

</div>

Hello,

I had the OpenVPN working successfully and I was able to connect remotely to my home network from abroad 🕶 The interface is tun0

Now I am looking to anonymize part of my traffic (the deluge one) so I installed a package that installs a VPN client. The interface is tun1

The problem is that when the client is installed and working, I am not able to reach my home network with OpenVPN, I guess it has something to do with routes or iptables. here is my configuration

```nohighlight
root@DietPi:/home/dietpi# route -v
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.117.78.1 128.0.0.0 UG 0 0 0 tun1
default 192.168.31.1 0.0.0.0 UG 0 0 0 eth0
10.8.0.0 10.8.0.2 255.255.255.0 UG 0 0 0 tun0
10.8.0.2 0.0.0.0 255.255.255.255 UH 0 0 0 tun0
10.117.78.0 0.0.0.0 255.255.254.0 U 0 0 0 tun1
128.0.0.0 10.117.78.1 128.0.0.0 UG 0 0 0 tun1
192.168.31.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
194.59.249.243 192.168.31.1 255.255.255.255 UGH 0 0 0 eth0

```

```nohighlight
Chain OUTPUT (policy DROP)
target prot opt source destination         
DROP tcp -- anywhere anywhere tcp dpt:53
DROP udp -- anywhere anywhere udp dpt:53
ACCEPT all -- anywhere 192.168.0.0/16      
ACCEPT all -- anywhere 10.0.0.0/8          
ACCEPT all -- anywhere 172.16.0.0/12       
ACCEPT all -- anywhere 104.20.26.217       
ACCEPT all -- anywhere 172.67.17.175       
ACCEPT all -- anywhere assets.windscribe.com 
ACCEPT all -- anywhere 172.67.203.127      
ACCEPT all -- anywhere 104.21.93.29        
ACCEPT all -- anywhere 104.21.53.216       
ACCEPT all -- anywhere 172.67.219.39       
ACCEPT all -- anywhere 104.21.65.74        
ACCEPT all -- anywhere 172.67.189.40       
ACCEPT all -- anywhere anywhere            
ACCEPT all -- anywhere localhost           
ACCEPT all -- anywhere 194.59.249.243

```

Can anyone advise what am I missing? My home network sits at 192.168.31.X and the network that OpenVPN creates is 10.8.0.X , and the one that the VPN client creates is 10.117.X.X

Thank you

---

<div class="post-metadata">

**Author:** ![MichaIng](https://dietpi.com/forum/user_avatar/dietpi.com/michaing/32/7_2.png) [@MichaIng](https://dietpi.com/forum/u/MichaIng)\
**Post date:** [20 March 2021 21:35 UTC](https://dietpi.com/forum/t/vpn-out-openvpn-in/5147/2 "2021-03-20T21:35:28Z")

</div>

Indeed the initial connection from VPN clients to your server through eth0 is “answered” through the upstream VPN tunnel (hence never reaches the client) because of the routes. When a VPN client connection is up and you concurrently want to allow remote connections to that host, you need to setup a routing table for those, either based on an application user or for initially incoming connections on a certain port or in general.

iptables can be used to mark packets based on conditions and ip rules can be used to apply a different routing table for packets based on the mark.

But I need to dig out the details.

---

<div class="post-metadata">

**Author:** ![Joulinar](https://dietpi.com/forum/user_avatar/dietpi.com/joulinar/32/57_2.png) [@Joulinar](https://dietpi.com/forum/u/Joulinar)\
**Post date:** [20 March 2021 22:17 UTC](https://dietpi.com/forum/t/vpn-out-openvpn-in/5147/3 "2021-03-20T22:17:44Z")

</div>

usually trendy has some experience with iptables

---

<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:** [21 March 2021 04:35 UTC](https://dietpi.com/forum/t/vpn-out-openvpn-in/5147/4 "2021-03-21T04:35:34Z")

</div>

You can omit the iptables part, if the rule is something simple.  
[There was a similar topic here.](https://dietpi.com/forum/t/use-pi-as-a-vpn-gateway-for-lan-clients-but-not-for-itself/4978/3)  
In such cases I prefer to leave the default gateway to the ISP and manually add the services or hosts which need to be routed over the tunnel.

---

<div class="post-metadata">

**Author:** ![MichaIng](https://dietpi.com/forum/user_avatar/dietpi.com/michaing/32/7_2.png) [@MichaIng](https://dietpi.com/forum/u/MichaIng)\
**Post date:** [21 March 2021 09:53 UTC](https://dietpi.com/forum/t/vpn-out-openvpn-in/5147/5 "2021-03-21T09:53:33Z")

</div>

Nice, iif for incoming interface and oif for outgoing interface, right?  
In this case I think it’s not that easy because the initial incoming VPN connection request is coming though the regular WAN interface and outbound connections from the local VPN server shall still use the upstream VPN tunnel. So it’s indeed about inbound connections vs unbound connections. Not sure if ip rules has some syntax for this?

---

<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:** [21 March 2021 14:21 UTC](https://dietpi.com/forum/t/vpn-out-openvpn-in/5147/6 "2021-03-21T14:21:59Z")

</div>

> [@](#):
>
> Nice, iif for incoming interface and oif for outgoing interface, right?

Yes!

> [@](#):
>
> In this case I think it’s not that easy because the initial incoming VPN connection request is coming though the regular WAN interface and outbound connections from the local VPN server shall still use the upstream VPN tunnel. So it’s indeed about inbound connections vs unbound connections. Not sure if ip rules has some syntax for this?

It is easy, you can make a rule to bind the source port of the vpn server to the routing table that uses the wan uplink. Instead of iif, use the sport selector.

---

<div class="post-metadata">

**Author:** ![MichaIng](https://dietpi.com/forum/user_avatar/dietpi.com/michaing/32/7_2.png) [@MichaIng](https://dietpi.com/forum/u/MichaIng)\
**Post date:** [21 March 2021 16:24 UTC](https://dietpi.com/forum/t/vpn-out-openvpn-in/5147/7 "2021-03-21T16:24:37Z")

</div>

Ah nice. So all packets from that port will be leaving on eth0, but that is fine since that port is for establishing the VPN connection only anyway, not for communication though the tunnel. That method should work pretty will for e.g. allowing remote connections to a BitTorrent server while it is connected to a VPN. Much easier than connection marks, many thanks for sharing 🙂.

---

<div class="post-metadata">

**Author:** ![cotarelo](https://dietpi.com/forum/letter_avatar_proxy/v4/letter/c/bc8723/32.png) [@cotarelo](https://dietpi.com/forum/u/cotarelo)\
**Post date:** [22 March 2021 00:34 UTC](https://dietpi.com/forum/t/vpn-out-openvpn-in/5147/8 "2021-03-22T00:34:16Z")

</div>

> [@trendy](#):
>
> It is easy, you can make a rule to bind the source port of the vpn server to the routing table that uses the wan uplink. Instead of iif, use the sport selector.

Thanks  
Could you put an example of the command and where I can make it permanent?

---

<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:** [22 March 2021 10:06 UTC](https://dietpi.com/forum/t/vpn-out-openvpn-in/5147/9 "2021-03-22T10:06:42Z")

</div>

An example would be:

```nohighlight
ip route add to default via 10.10.10.1 table 100
ip rule add sport 1234 to default lookup 100 prio 16010

```

Change the ip to the one of the isp router and the port to the one you are using for the vpn server.  
You can add it in rc.local to be permanent.

---

<div class="post-metadata">

**Author:** ![cotarelo](https://dietpi.com/forum/letter_avatar_proxy/v4/letter/c/bc8723/32.png) [@cotarelo](https://dietpi.com/forum/u/cotarelo)\
**Post date:** [22 March 2021 18:31 UTC](https://dietpi.com/forum/t/vpn-out-openvpn-in/5147/10 "2021-03-22T18:31:35Z")

</div>

I have added

```nohighlight
ip route add to default via 192.168.31.1 table 100
ip rule add sport 1194 to default lookup 100 prio 16010
ip rule add sport 443 to default lookup 100 prio 16010
ip rule add sport 943 to default lookup 100 prio 16010

```

Since the OpenVPN uses 443 and 943 TCP and 1194 UDP by default.

But I still have no luck connecting from outside to the internal VPN, it times out

---

<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:** [22 March 2021 20:33 UTC](https://dietpi.com/forum/t/vpn-out-openvpn-in/5147/11 "2021-03-22T20:33:10Z")

</div>

First of all OpenVPN is udp/1194, the rest belong to other protocols. But which one are you using?  
Also if you are adding multiple ports you should also change the sequence number, as now the first two are overwritten by the third.

---

<div class="post-metadata">

**Author:** ![cotarelo](https://dietpi.com/forum/letter_avatar_proxy/v4/letter/c/bc8723/32.png) [@cotarelo](https://dietpi.com/forum/u/cotarelo)\
**Post date:** [23 March 2021 07:24 UTC](https://dietpi.com/forum/t/vpn-out-openvpn-in/5147/12 "2021-03-23T07:24:14Z")

</div>

Ok thank you. I tried at the beginning only with 1194 but ot didn’t connect so I added the other two which I thought they were also needed for Openvpn.

I am using the default installer from dietpi for Openvpn slño if 1194 UDP is enough i can leave the first one only.  
Still it doesn’t get through

---

<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:** [23 March 2021 10:49 UTC](https://dietpi.com/forum/t/vpn-out-openvpn-in/5147/13 "2021-03-23T10:49:52Z")

</div>

You can verify the port with ss -tunlp | grep vpn  
Other than that post the following: ip -4 addr; ip -4 ro list table all; ip -4 ru

---

<div class="post-metadata">

**Author:** ![cotarelo](https://dietpi.com/forum/letter_avatar_proxy/v4/letter/c/bc8723/32.png) [@cotarelo](https://dietpi.com/forum/u/cotarelo)\
**Post date:** [23 March 2021 19:49 UTC](https://dietpi.com/forum/t/vpn-out-openvpn-in/5147/14 "2021-03-23T19:49:39Z")

</div>

The fist command has no return, the second gives a lot of information

```nohighlight
dietpi@DietPi:~$ ss -tunlp | grep vpn
dietpi@DietPi:~$ ip -4 addr; ip -4 ro list table all; ip -4 ru
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    inet 192.168.31.254/24 brd 192.168.31.255 scope global eth0
       valid_lft forever preferred_lft forever
5: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 100
    inet 10.8.0.1 peer 10.8.0.2/32 scope global tun0
       valid_lft forever preferred_lft forever
6: tun1: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 100
    inet 10.121.34.39/23 brd 10.121.35.255 scope global tun1
       valid_lft forever preferred_lft forever
default via 192.168.31.1 dev eth0 table 100 
0.0.0.0/1 via 10.121.34.1 dev tun1 
default via 192.168.31.1 dev eth0 onlink 
10.8.0.0/24 via 10.8.0.2 dev tun0 
10.8.0.2 dev tun0 proto kernel scope link src 10.8.0.1 
10.121.34.0/23 dev tun1 proto kernel scope link src 10.121.34.39 
37.120.136.243 via 192.168.31.1 dev eth0 
128.0.0.0/1 via 10.121.34.1 dev tun1 
192.168.31.0/24 dev eth0 proto kernel scope link src 192.168.31.254 
local 10.8.0.1 dev tun0 table local proto kernel scope host src 10.8.0.1 
broadcast 10.121.34.0 dev tun1 table local proto kernel scope link src 10.121.34.39 
local 10.121.34.39 dev tun1 table local proto kernel scope host src 10.121.34.39 
broadcast 10.121.35.255 dev tun1 table local proto kernel scope link src 10.121.34.39 
broadcast 127.0.0.0 dev lo table local proto kernel scope link src 127.0.0.1 
local 127.0.0.0/8 dev lo table local proto kernel scope host src 127.0.0.1 
local 127.0.0.1 dev lo table local proto kernel scope host src 127.0.0.1 
broadcast 127.255.255.255 dev lo table local proto kernel scope link src 127.0.0.1 
broadcast 192.168.31.0 dev eth0 table local proto kernel scope link src 192.168.31.254 
local 192.168.31.254 dev eth0 table local proto kernel scope host src 192.168.31.254 
broadcast 192.168.31.255 dev eth0 table local proto kernel scope link src 192.168.31.254 
0:	from all lookup local 
16010:	from all sport 1194 lookup 100 
32766:	from all lookup main 
32767:	from all lookup default

```

Not related, but I also have realised that thr no-ip ddns script is updating to the wrong ip even when I mark eth0 as the interface for updating.

---

<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:** [24 March 2021 08:13 UTC](https://dietpi.com/forum/t/vpn-out-openvpn-in/5147/15 "2021-03-24T08:13:20Z")

</div>

What about this?  
grep port /etc/openvpn/\*

---

<div class="post-metadata">

**Author:** ![cotarelo](https://dietpi.com/forum/letter_avatar_proxy/v4/letter/c/bc8723/32.png) [@cotarelo](https://dietpi.com/forum/u/cotarelo)\
**Post date:** [24 March 2021 11:08 UTC](https://dietpi.com/forum/t/vpn-out-openvpn-in/5147/16 "2021-03-24T11:08:33Z")

</div>

```nohighlight
grep port /etc/openvpn/*
grep: /etc/openvpn/client: Is a directory
grep: /etc/openvpn/easy-rsa: Is a directory
grep: /etc/openvpn/server: Is a directory
/etc/openvpn/server.conf:port 1194

```

Also

```nohighlight
netstat -putan | grep 1194
udp 0 0 0.0.0.0:1194 0.0.0.0:* 1240/openvpn

```

[/code]

---

<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:** [24 March 2021 13:57 UTC](https://dietpi.com/forum/t/vpn-out-openvpn-in/5147/17 "2021-03-24T13:57:44Z")

</div>

netstat is not included in dietpi and is deprecated, ss is the successor. Anyway, we have established the openvpn server is running on udp/1194  
Install tcpdump if you don’t have it already and run: sudo tcpdump -i eth0 -evn udp port 1194  
Then try to connect from the client, let it capture a few packets, stop with Ctrl-c and paste here the output.

---

<div class="post-metadata">

**Author:** ![MichaIng](https://dietpi.com/forum/user_avatar/dietpi.com/michaing/32/7_2.png) [@MichaIng](https://dietpi.com/forum/u/MichaIng)\
**Post date:** [24 March 2021 22:06 UTC](https://dietpi.com/forum/t/vpn-out-openvpn-in/5147/18 "2021-03-24T22:06:54Z")

</div>

Probably it’s required to add “dev eth0” to the ip route, to force it using the right device as well.

---

<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:** [25 March 2021 09:35 UTC](https://dietpi.com/forum/t/vpn-out-openvpn-in/5147/19 "2021-03-25T09:35:13Z")

</div>

It doesn’t hurt to add it, however the dietpi shouldn’t have any issues finding the 192.168.31.1 as it is directly connected to the eth0.

---

<div class="post-metadata">

**Author:** ![cotarelo](https://dietpi.com/forum/letter_avatar_proxy/v4/letter/c/bc8723/32.png) [@cotarelo](https://dietpi.com/forum/u/cotarelo)\
**Post date:** [27 March 2021 07:15 UTC](https://dietpi.com/forum/t/vpn-out-openvpn-in/5147/20 "2021-03-27T07:15:58Z")

</div>

Where should I add the dev flag? What other things I can do?

[Next page](https://dietpi.com/forum/t/vpn-out-openvpn-in/5147.md?page=2)
