Iptables rules for OpenVPN server running in a OpenVZ server

This forum is for all inquiries relating to the installation of OpenVPN from source and with binaries.
Forum rules
Please visit (and READ) the OpenVPN HowTo http://openvpn.net/howto prior to asking any questions in here!
Post Reply
zyggs
OpenVpn Newbie
Posts: 5
Joined: Sat Mar 19, 2016 3:12 pm

Iptables rules for OpenVPN server running in a OpenVZ server

Post by zyggs » Sat Mar 19, 2016 3:49 pm

I'm trying to install a OpenVPN server in a OpenVZ container.
I've successfully installed OpenVPN, but I still cannot connect to the server.
Here's my config:
CentOS 7

ifconfig

Code: Select all

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: venet0: <BROADCAST,POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN
    link/void
    inet 127.0.0.1/32 scope host venet0
    inet 192.168.42.219/32 brd 192.168.42.219 scope global venet0:0
    inet6 2a06:8ec0:0:30ae::1/64 scope global
       valid_lft forever preferred_lft forever
3: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 100
    link/none
    inet 10.8.0.1/24 brd 10.8.0.255 scope global tun0
/etc/sysctl.conf

Code: Select all

# Packet forwarding
net.ipv4.ip_forward = 1
net.inet.ip.fastforwarding = 1
/etc/openvpn/server.conf

Code: Select all

port 443
proto tcp
dev tun
sndbuf 0
rcvbuf 0
ca ca.crt
cert server.crt
key server.key
dh dh.pem
topology subnet
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 8.8.4.4"
keepalive 10 120
comp-lzo
persist-key
persist-tun
status openvpn-status.log
verb 3
crl-verify crl.pem
client.ovpn

Code: Select all

client
dev tun
proto tcp
sndbuf 0
rcvbuf 0
remote xxx.xxx.xxx.xxx 443
resolv-retry infinite
nobind
persist-key
persist-tun
remote-cert-tls server
comp-lzo
verb 3
<ca>

From reading other posts on this forum I inserted this rules on iptables:

iptables

Code: Select all

# OpenVPN (depending on the port you run OpenVPN)
iptables -A INPUT -i venet0 -m state --state NEW -p tcp --dport 443 -j ACCEPT

# Allow TUN interface connections to OpenVPN server
iptables -A INPUT -i tun+ -j ACCEPT

# Allow TUN interface connections to be forwarded through other interfaces
iptables -A FORWARD -i tun+ -j ACCEPT
iptables -A FORWARD -i tun+ -o venet0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i venet0 -o tun+ -m state --state RELATED,ESTABLISHED -j ACCEPT

# NAT the VPN client traffic to the internet
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o venet0 -j MASQUERADE

iptables -A OUTPUT -o tun+ -j ACCEPT
As I'm a total noob in Iptables I don't know if this rules are correct...
So I ask is anything wrong with my config? Thank you very much

User avatar
Traffic
OpenVPN Protagonist
Posts: 4081
Joined: Sat Aug 09, 2014 11:24 am

Re: Iptables rules for OpenVPN server running in a OpenVZ se

Post by Traffic » Sat Mar 19, 2016 4:37 pm

zyggs wrote:

Code: Select all

# NAT the VPN client traffic to the internet
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o venet0 -j MASQUERADE
Typically, OpenVZ does not support MASQUERADE so use SNAT instead:

Code: Select all

iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to-source server.real.external.IP

zyggs
OpenVpn Newbie
Posts: 5
Joined: Sat Mar 19, 2016 3:12 pm

Re: Iptables rules for OpenVPN server running in a OpenVZ se

Post by zyggs » Sat Mar 19, 2016 6:37 pm

Thank you for the reply.
I removed the bad iptables rule and added the one you suggest:

Code: Select all

iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to-source server.real.external.IP
And know I'm Im getting "Connection refused" when trying to connect.

Here's my iptables output:
iptables -L -n -v

Code: Select all

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 ACCEPT     tcp  --  venet0 *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:443
    0     0 ACCEPT     all  --  tun+   *       0.0.0.0/0            0.0.0.0/0

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 ACCEPT     all  --  tun+   *       0.0.0.0/0            0.0.0.0/0
    0     0 ACCEPT     all  --  tun+   venet0  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     all  --  venet0 tun+    0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 ACCEPT     all  --  *      tun+    0.0.0.0/0            0.0.0.0/0

Updated rules added to iptables, replacing MASQUERADE by SNAT as suggested by Traffic:

Code: Select all

# OpenVPN (depending on the port you run OpenVPN)
iptables -A INPUT -i venet0 -m state --state NEW -p tcp --dport 443 -j ACCEPT

# Allow TUN interface connections to OpenVPN server
iptables -A INPUT -i tun+ -j ACCEPT

# Allow TUN interface connections to be forwarded through other interfaces
iptables -A FORWARD -i tun+ -j ACCEPT
iptables -A FORWARD -i tun+ -o venet0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i venet0 -o tun+ -m state --state RELATED,ESTABLISHED -j ACCEPT

# NAT the VPN client traffic to the internet
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to-source server.real.external.IP

iptables -A OUTPUT -o tun+ -j ACCEPT

User avatar
Traffic
OpenVPN Protagonist
Posts: 4081
Joined: Sat Aug 09, 2014 11:24 am

Re: Iptables rules for OpenVPN server running in a OpenVZ se

Post by Traffic » Sat Mar 19, 2016 8:26 pm

You have posted this:
zyggs wrote:client.ovpn
proto tcp
remote xxx.xxx.xxx.xxx 443
zyggs wrote:CentOS 7

ifconfig
2: venet0: <BROADCAST,POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN
link/void
inet 127.0.0.1/32 scope host venet0
inet 192.168.42.219/32 brd 192.168.42.219 scope global venet0:0
Does your server have a public IP address ? Is it behind a firewall ?

zyggs
OpenVpn Newbie
Posts: 5
Joined: Sat Mar 19, 2016 3:12 pm

Re: Iptables rules for OpenVPN server running in a OpenVZ se

Post by zyggs » Sat Mar 19, 2016 9:09 pm

Yes! I'm likely behind a Firewall/NAT because my ifconfig shows a private IP address.
The IP address I omitted in my client config (remote xxx.xxx.xxx.xxx 443) is my public IP address (The IP I ssh into).

This is one of those cheap low cost servers. I just bough it so I can learn a bit about servers and networking.

User avatar
Traffic
OpenVPN Protagonist
Posts: 4081
Joined: Sat Aug 09, 2014 11:24 am

Re: Iptables rules for OpenVPN server running in a OpenVZ se

Post by Traffic » Sat Mar 19, 2016 9:58 pm

You will probably need to forward the proto/port for your VPN at the firewall ..

zyggs
OpenVpn Newbie
Posts: 5
Joined: Sat Mar 19, 2016 3:12 pm

Re: Iptables rules for OpenVPN server running in a OpenVZ se

Post by zyggs » Sat Mar 19, 2016 11:40 pm

I checked with nmap, and door 443 is closed. But door 80 is open.
So I changed OpenVPN port to listen on port 80.
I also change the iptables rule to accept tcp connections on port 80.

But when I try to connect to the VPN i get "TLS Error: TLS handshake failed"

Is it possible that the servers provider firewall is dropping packets sent from my openVPN client?

User avatar
Traffic
OpenVPN Protagonist
Posts: 4081
Joined: Sat Aug 09, 2014 11:24 am

Re: Iptables rules for OpenVPN server running in a OpenVZ se

Post by Traffic » Sat Mar 19, 2016 11:47 pm

I suggest you contact your providers support channel and ask if and how your server can accept incoming packets.

zyggs
OpenVpn Newbie
Posts: 5
Joined: Sat Mar 19, 2016 3:12 pm

Re: Iptables rules for OpenVPN server running in a OpenVZ se

Post by zyggs » Sat Mar 19, 2016 11:52 pm

That's what I'm going to do.
I believe that the problem it's on their side.

@Traffic Thank you for taking the time to try to help me!

t0ta1
OpenVpn Newbie
Posts: 1
Joined: Fri Mar 08, 2019 9:05 am

Re: Iptables rules for OpenVPN server running in a OpenVZ server

Post by t0ta1 » Fri Mar 08, 2019 5:06 pm

I am running OpenVPN on a 1and1 (1und1 / IONOS) VPS.
Installing and connecting via the VPN did work immediately - but getting internet traffic was the pitfall.
That line made my day - thx guys!

Code: Select all

# NAT the VPN client traffic to the internet
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to-source server.real.external.IP

Post Reply