Creating a tunnel between two subnets using OpenVPN Access Server
- Prepare your cloud
- Create a network and subnets
- Create a VM gateway
- Start the VPN server
- Configure network traffic permissions
- Get the administrator password
- Create an OpenVPN user for the tunnel
- Set up the second subnet's gateway to access the OpenVPN server
- Test the tunnel
- How to delete the resources you created
When hosting computing resources in a public cloud, one often needs to ensure secure connections between two different subnets, such as an office network and a test farm in Yandex Cloud. The best way to handle this is using a VPN to:
- Connect geographically remote networks.
- Connect freelancers to the office network.
- Set up an encrypted connection over an open Wi-Fi network.
This tutorial describes how to create a VPN tunnel using the OpenVPN technology.
OpenVPN Access Server is compatible with the OpenVPN open-source version
In this example, we are going to create a tunnel that connects two different subnets into a single network. It will be working between two VPN gateways, one of them being OpenVPN Access Server and the other, a VM instance with the OpenVPN client. To test the VPN tunnel, configure gateways on both sides of it. In our example, one subnet is hosted in Yandex Cloud, while the other may reside both in Yandex Cloud and in an external network.
To create a tunnel between two different subnets:
- Prepare your cloud.
- Create a network and subnets.
- Create VMs you want to link.
- Create a VM gateway.
- Start the VPN server.
- Configure network traffic permissions.
- Get the administrator password.
- Create an OpenVPN user for the tunnel.
- Set up the second subnet's gateway to access the OpenVPN server.
- Test the tunnel.
If you no longer need the VPN server, delete the created VMs.
Prepare your cloud
Sign up for Yandex Cloud and create a billing account:
- Go to the management console
and log in to Yandex Cloud or create an account if you do not have one yet. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account, create one.
If you have an active billing account, you can go to the cloud page
Learn more about clouds and folders.
Required paid resources
The cost of infrastructure support for OpenVPN includes:
- Fee for the disks and continuously running VMs (see Yandex Compute Cloud pricing).
- Fee for using a dynamic or static external IP address (see Yandex Virtual Private Cloud pricing).
- Fee for the OpenVPN Access Server license (when using more than two connections).
Create a network and subnets
To connect cloud resources to the internet, make sure you have a network and subnets.
Create a network
- In the management console
, select the folder where you want to create a cloud network. - In the list of services, select Virtual Private Cloud.
- Click Create network.
- Enter a name for the network, e.g.,
ovpn-network
. - Disable the Create subnets option.
- Click Create network.
Create subnets
- Select the
ovpn-network
network. - Click Add subnet.
- Enter a name for the subnet, e.g.,
ovpn-left
. - Select an availability zone from the drop-down list.
- Enter the subnet CIDR:
10.128.0.0/24
. - Click Create subnet.
- Repeat steps 2 to 6 for the second subnet named
ovpn-right
with the10.253.11.0/24
CIDR.
Create VMs you want to link
-
On the folder page in the management console
, click Create resource and selectVirtual machine instance
. -
Under Boot disk image in the Product search field, select an image for the VM.
-
Under Location, select the availability zone the
ovpn-left
subnet resides in. -
Under Network settings:
-
In the Subnet field, select the network named
ovpn-network
and the subnet namedovpn-left
. -
In the Public IP field, select
No address
. -
Expand the Additional section:
- In the Internal IPv4 address field, select
Manual
. - In the input field that appears, enter
10.128.0.4
.
- In the Internal IPv4 address field, select
-
-
Under Access, select SSH key and specify the access credentials for the VM:
- In the Login field, enter the username:
yc-user
. -
In the SSH key field, select the SSH key saved in your organization user profile.
If there are no saved SSH keys in your profile, or you want to add a new key:
- Click Add key.
- Enter a name for the SSH key.
- Upload or paste the contents of the public key file. You need to create a key pair for the SSH connection to a VM yourself.
- Click Add.
The SSH key will be added to your organization user profile.
If users cannot add SSH keys to their profiles in the organization, the added public SSH key will only be saved to the user profile of the VM being created.
- In the Login field, enter the username:
-
Under General information, specify the VM name:
ao-openvpn-test
. -
Click Create VM.
-
Repeat steps 1 to 7 to create the second VM named
vm-ovpn-host
with internal address10.253.11.110
, hosted in theovpn-right
subnet.
Create a VM gateway
-
On the folder page in the management console
, click Create resource and selectVirtual machine instance
. -
Under Boot disk image in the Product search field, select an image for the VM.
-
Under Location, select the availability zone the
ovpn-right
subnet resides in. -
Under Network settings:
-
In the Subnet field, select the network named
ovpn-network
and the subnet namedovpn-right
. -
In the Public IP field, select
Auto
orList
.Either use static public IP addresses from the list or convert the VM IP address to static. Dynamic IP addresses may change after the VM reboots and the connections will no longer work.
-
Expand the Additional section; in the Internal IPv4 address field, select
Manual
. -
In the input field that appears, enter
10.253.11.19
.
-
-
Under Access, select SSH key and specify the VM access data:
- In the Login field, enter the username:
yc-user
. -
In the SSH key field, select the SSH key saved in your organization user profile.
If there are no saved SSH keys in your profile, or you want to add a new key:
- Click Add key.
- Enter a name for the SSH key.
- Upload or paste the contents of the public key file. You need to create a key pair for the SSH connection to a VM yourself.
- Click Add.
The SSH key will be added to your organization user profile.
If users cannot add SSH keys to their profiles in the organization, the added public SSH key will only be saved to the user profile of the VM being created.
- In the Login field, enter the username:
-
Under General information, specify the VM name:
vm-ovpn-gw
. -
Click Create VM.
Start the VPN server
Create a VM to be the gateway for VPN connections:
-
On the folder page in the management console
, click Create resource and selectVirtual machine instance
. -
Under Boot disk image, enter
OpenVPN Access Server
in the Product search field and select the OpenVPN Access Server image. -
Under Location, select the availability zone the
ovpn-left
subnet resides in. -
Under Disks and file storages, enter
10 GB
as your boot disk size. -
Under Computing resources, navigate to the
Custom
tab and specify the required platform, number of vCPUs, and amount of RAM:- Platform:
Intel Ice Lake
. - vCPU:
2
. - Guaranteed vCPU performance:
100%
. - RAM:
2 GB
.
- Platform:
-
Under Network settings:
-
In the Subnet field, select the network named
ovpn-network
and the subnet namedovpn-left
. -
In the Public IP field, select
Auto
orList
.Either use static public IP addresses from the list or convert the VM IP address to static. Dynamic IP addresses may change after the VM reboots and the connections will no longer work.
-
In the Security groups field, select a security group. If you leave this field empty, the default security group will be assigned.
-
Expand the Additional section; in the Internal IPv4 address field, select
Manual
. -
In the input field that appears, enter
10.128.0.3
.
-
-
Under Access, select SSH key and specify the VM access data:
- In the Login field, enter the username:
yc-user
. -
In the SSH key field, select the SSH key saved in your organization user profile.
If there are no saved SSH keys in your profile, or you want to add a new key:
- Click Add key.
- Enter a name for the SSH key.
- Upload or paste the contents of the public key file. You need to create a key pair for the SSH connection to a VM yourself.
- Click Add.
The SSH key will be added to your organization user profile.
If users cannot add SSH keys to their profiles in the organization, the added public SSH key will only be saved to the user profile of the VM being created.
- In the Login field, enter the username:
-
Under General information, specify the VM name:
vpn-server
. -
Click Create VM.
-
A window will open informing you of the pricing type, which is BYOL (Bring Your Own License).
-
Click Create.
Configure network traffic permissions
Security groups act as a virtual firewall for incoming and outgoing traffic. See more about the default security group here.
-
To enable OpenVPN Access Server to work, add the following rules to the default security group:
Traffic
directionDescription Port range Protocol Source CIDR blocks Incoming VPN Server
443
TCP
CIDR
0.0.0.0/0
Incoming VPN Server
1194
UDP
CIDR
0.0.0.0/0
Incoming Admin Web UI,
Client Web UI
943
TCP
CIDR
0.0.0.0/0
A VPN server can redirect traffic from the
HTTPS
port. If required, leave the onlyTCP 443
port open. See also the settings in the Configuration → Network Settings tab of the server admin panel. -
If you have configured a security group of your own, make sure it allows traffic between the VPN server and the required resources. For example, they share the same security group and there is a Self rule for the whole group.
Get the administrator password
The openvpn user with administrator privileges was created on the OpenVPN
server in advance. The password is generated automatically when you create a VM.
Get the password in the serial port output or the serial console. The password will display in the following string:
To log in, please use the `openvpn` account with the <password> password.
Where <password>
is the openvpn
user password.
Log in to the admin panel using the openvpn
username and the obtained password.
If you do not get the password after launching the VPN server for the first time, you need to re-create the VM running OpenVPN Access Server. The password will not display when reboot.
Create an OpenVPN user for the tunnel
OpenVPN Access Server provides two web interfaces:
- Client Web UI at
https://<VM_public_IP_address>:943/
. This interface is used by regular users to download client applications and configuration profiles. - Admin Web UI at
https://<VM_public_IP_address>:943/admin/
. This interface is used to configure the server.
Note
By default, the server has a self-signed certificate installed. If you need to replace this certificate, follow the steps described here
Once you deploy OpenVPN Access Server on the Yandex Cloud VM that will be working as a gateway, you will have specific IP addresses and accounts as follows (the addresses below are provided for indicative purposes, yours may be different):
- Internal IP of the
vpn-server
gateway:10.128.0.3
. - Public IP address of the
vpn-server
VM:<VM_public_IP_address>
- Admin Web UI:
https://<VM_public_IP_address>:943/admin
- Account for accessing the Admin UI:
openvpn/<admin password>
- Client Web UI:
https://<VM_public_IP_address>:943
On the server side, create an OpenVPN user the second subnet's gateway will use to access the OpenVPN server to enable the tunnel. To create a user, log in to the Admin Web UI admin panel:
- Open
https://<VM_public_IP_address>:943/admin
in your browser. - Enter the
openvpn
username and password (see this section on how to get the admin password). - Click Agree. This will open the home screen of the OpenVPN admin panel.
- Go to the User management tab and select User permissions.
- In the user list, enter the name of the new user in the New Username field, e.g.,
as-gw-user
. - Click the pencil icon in the More Settings column and set the new user's password in the Local Password field.
- In the Access Control field, select User Routing and specify the current local subnet where OpenVPN Access Server is deployed, e.g.,
10.128.0.0/24
. - In the VPN Gateway field, select Yes and specify another local subnet to connect to via the tunnel, e.g.,
10.253.11.0/24
. - Click Save settings.
- Click Update running server.
- Log in to the user panel under the new
as-gw-user
account, save the connection profile in a file namedas-gw-user.conf
, and move this file to the VM that will act as a gateway for the OpenVPN tunnel in the other subnet.
Set up the second subnet's gateway to access the OpenVPN server
Run the following commands in the vm-ovpn-gw
console:
sudo apt update
sudo apt install openvpn
cp as-gw-user.conf /etc/openvpn/client/
echo -e "as-gw-user\n<password>" > /etc/openvpn/client/param.txt
As a result, a file named param.txt
should appear in the /etc/openvpn/client/
folder. Copy to the same folder the previously created as-gw-user.conf
file of the OpenVPN user you created to establish the tunnel:
ls -lh /etc/openvpn/client/
Result:
total 16K
-rw-rw-r-- 1 root root 9.7K Nov 10 14:37 as-gw-user.conf
-rw-r--r-- 1 root root 24 Nov 10 14:31 param.txt
In the auth-user-pass
string of the /etc/openvpn/as-gw-user.conf
file, specify the param.txt
file name:
dev tun
dev-type tun
remote-version-min 1.2
reneg-seq 604800
auth-user-pass param.txt
verb 3
push-peer-info
Run the following commands:
sudo systemctl enable openvpn-client@as-gw-user
sudo systemctl start openvpn-client@as-gw-user
sudo systemctl status openvpn-client@as-gw-user
The result should look like this:
● openvpn-client@as-gw-user.service - OpenVPN tunnel for as/gw/user
Loaded: loaded (/lib/systemd/system/openvpn-client@.service; enabled; vendor preset:
enabled)
Active: active (running) since Fri 2022-11-11 20:12:49 UTC; 1h 6min ago
Docs: man:openvpn(8)
https://community.openvpn.net/openvpn/wiki/Openvpn24ManPage
https://community.openvpn.net/openvpn/wiki/HOWTO
Main PID: 2626 (openvpn)
Status: "Initialization Sequence Completed"
Tasks: 1 (limit: 2237)
Memory: 2.0M
CPU: 157ms
CGroup: /system.slice/system-openvpn\x2dclient.slice/openvpn-client@as-gw-user.service
└─2626 /usr/sbin/openvpn --suppress-timestamps --nobind --config as-gw-user.conf
To enable packet transfers from other hosts, run these commands:
vm-ovpn-gw:~$ sudo bash -c "echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf"
vm-ovpn-gw:~$ sudo sysctl -p
Check that there is a route to 10.253.11.0/24
at the vpn-server
gateway:
vpn-server:~$ sudo ip route
Result:
default via 10.128.0.1 dev eth0 proto dhcp src 10.128.0.3 metric 100
10.128.0.0/24 dev eth0 proto kernel scope link src 10.128.0.3
10.128.0.1 dev eth0 proto dhcp scope link src 10.128.0.3 metric 100
10.253.11.0/24 dev as0t2 proto static
172.27.224.0/22 dev as0t0 proto kernel scope link src 172.27.224.1
172.27.228.0/22 dev as0t1 proto kernel scope link src 172.27.228.1
172.27.232.0/22 dev as0t2 proto kernel scope link src 172.27.232.1
172.27.236.0/22 dev as0t3 proto kernel scope link src 172.27.236.1
Check the route to 10.128.0.0/24
at the vm-ovpn-gw
VM:
sudo ip route
Result:
default via 10.253.11.1 dev ens18 proto dhcp src 10.253.11.19 metric 100
10.128.0.0/24 via 172.27.232.1 dev tun0 metric 101
10.253.11.0/24 dev ens18 proto kernel scope link src 10.253.11.19 metric 100
10.253.11.1 dev ens18 proto dhcp scope link src 10.253.11.19 metric 100
172.27.224.0/20 via 172.27.232.1 dev tun0 metric 101
172.27.232.0/22 dev tun0 proto kernel scope link src 172.27.232.5
178.154.226.72 via 10.253.11.1 dev ens18
Test the tunnel
To test the tunnel, you will need the test VMs we mentioned above. These must reside in both subnets and be different from the tunnel gateways.
For these two VMs to exchange data, they both must see the static routes to the other subnet. ao-openvpn-test
to 10.253.11.0/24
, and vm-ovpn-host
to 10.128.0.0/24
.
Run the following command on vm-ovpn-host
:
sudo ip route add 10.128.0.0./24 via 10.253.11.19
On the test VM in Yandex Cloud, adding a static route within the VM will not help. In Yandex Cloud, static routes for VMs should be specified in a different way.
In Yandex Cloud, the ao-openvpn-as
VMs (OpenVPN server) and ao-openvpn-test
VMs reside in the same default
subnet. In the settings of this subnet, add a static route with the following parameters:
Name: office-net
Prefix: 10.253.11.0/24
Next hop: 10.128.0.3
To apply this static route to the ao-openvpn-test
VM, shut it down and start it again.
Now use the ping
command to test the tunnel from the vm-ovpn-host
VM to the other test VM:
ping 10.128.0.4
Result:
PING 10.128.0.4 (10.128.0.4) 56(84) bytes of data.
64 bytes from 10.128.0.4: icmp_seq=1 ttl=61 time=7.45 ms
64 bytes from 10.128.0.4: icmp_seq=2 ttl=61 time=5.61 ms
64 bytes from 10.128.0.4: icmp_seq=3 ttl=61 time=5.65 ms
^C
--- 10.128.0.4 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 5.613/6.235/7.446/0.855 ms
Do the same from the other end of the tunnel, from the ao-openvpn-test
test VM:
ping 10.253.11.110
Result:
PING 10.253.11.110 (10.253.11.110) 56(84) bytes of data.
64 bytes from 10.253.11.110: icmp_seq=1 ttl=61 time=6.23 ms
64 bytes from 10.253.11.110: icmp_seq=2 ttl=61 time=5.90 ms
64 bytes from 10.253.11.110: icmp_seq=3 ttl=61 time=6.09 ms
64 bytes from 10.253.11.110: icmp_seq=4 ttl=61 time=5.69 ms
^C
--- 10.253.11.110 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 5.688/5.976/6.229/0.203 ms
How to delete the resources you created
To free up folder resources, delete the vpn-server
VM and the test VM.
If you reserved a public static IP address, delete it.