Install OpenVPN


OpenVPN enables remote client computers to access the server computer's directories and files.

Install VPN server

Switch to root:

$ sudo -s

Install openvpn and easy-rsa:

$ apt-get install openvpn easy-rsa

Set up public key infrastructure:

$ mkdir /etc/openvpn/easy-rsa/
$ cp -r /usr/share/easy-rsa/* /etc/openvpn/easy-rsa/
$ gedit /etc/openvpn/easy-rsa/vars

Define in vars:

export KEY_CITY="HongKong"
export KEY_ORG="K C Tang Consultants Ltd"
export KEY_EMAIL=""
export KEY_OU=kctclVPN
export KEY_NAME=kctclVPN
# next line added to avoid error when building the certificate and key 
export KEY_ALTNAMES=kctclVPN

Generate master Certificate Authority (CA) certificate and key:

$ cd /etc/openvpn/easy-rsa/
$ source vars
$ ./clean-all
$ ./build-ca

Generate a certificate and private key for the server:

$ ./build-key-server kctclVPN

Accept defaults for most parameters.

Answer “y” to:

  • "Sign the certificate? [y/n]"
  • "1 out of 1 certificate requests certified, commit? [y/n]"

Generate Diffie Hellman parameters:

$ ./build-dh

Copy certificates and keys generated in subdirectory keys/ to /etc/openvpn/:

$ cd keys/
$ cp kctclVPN.crt kctclVPN.key ca.crt dh2048.pem /etc/openvpn/

Generate a certificate and private key for the client user <client>:

$ cd /etc/openvpn/easy-rsa/
$ source vars
$ ./build-key <client>

Copy or move client's certificate and key to a Samba directory:

$ cp /etc/openvpn/ca.crt /<Samba directory>/
$ mv /etc/openvpn/easy-rsa/keys/<client>.crt /<Samba directory>/
$ mv /etc/openvpn/easy-rsa/keys/<client>.key /<Samba directory>/

Change the owners of the files:

$ cd /<Samba directory>
$ chown nobody:nogroup ca.crt <client>.crt <client>.key
$ chmod 644 ca.crt <client>.crt <client>.key

​E-mail the files to the client computer.

Remove the files:

$ rm ca.crt <client>.crt <client>.key

Config server.conf:

$ cd /
$ cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/
$ gzip -d /etc/openvpn/server.conf.gz
$ gedit /etc/openvpn/server.conf

Define to point to the correct file location, or ignore </path to file/> if in the same directory as server.conf:

ca </path to file/>ca.crt
cert </path to file/>kctclVPN.crt
key </path to file/>kctclVPN.key
dh </path to file/>dh2048.pem

Config sysctl.conf:

$ gedit /etc/sysctl.conf

Uncomment the following line to enable IP forwarding:


Reload sysctl.conf:

$ sysctl -p /etc/sysctl.conf

Start the server:

$ systemctl start openvpn
$ service openvpn start

Check if OpenVPN created a tun0 interface:

$ ifconfig tun0

Check syslog if tun0 does not appear:

$ grep -i vpn /var/log/syslog

Exit from root:

$ exit

Set the internet router to re-direct OpenVPN connections to server port 1194.

Install on Windows client computer

Download and install the latest OpenVPN Windows Installer.

Go to Microsoft Management Console: Start > Computer > Manage > Services and Applications > Services.

Start OpenVPN service.

Set it's startup type to automatic.

Save ca.crt <client>.crt <client>.key under C:\Program Files\OpenVPN\config\

Create C:\Program Files\OpenVPN\config\<client>.txt file and define it to contain:

dev tun
proto udp
remote 1194
resolv-retry infinite
ca ca.crt
cert <\\path\\><client>.crt
key <\\path\\><client>.key
ns-cert-type server
verb 3

Specify path in Windows format if ca.crt <client>.crt <client>.key are saved in a folder different from <client>.txt.

Change filename from <client>.txt to <client>.ovpn.

Run on Windows client as a service

To start OpenVPN automatically as a service every time after rebooting:

  • Go to Microsoft Management Console: Start > Computer > Manage > Services and Applications > Services.
  • Right-click OpenVPN service > Properties > Start.
  • Change Startup type to Automatic.
  • Click OK.

However, running as a service has a bug that the connection may not be re-connected after the computer wakes up after sleep or hibernation.

If there is the case, connecting with the GUI will be the better choice.

Run on Windows client using GUI

Start OpenVPN every time after rebooting:

  • Right-click OpenVPN GUI icon on Desktop.
  • Click Properties > Compatibility > Run this program as an administrator > OK.

Connect after starting or loss of connection after sleep or hibernation:

    • Click OpenVPN GUI icon on Desktop.
    • Click the icon on the system tray to connect or right-click the icon and click Connect.

    Map drive for quick access

    Define a drive to be listed in the file manager directory to represent the server:
    • Open file manager and enter \\ to access the vpn server.
    • Right-click the desired folder.
    • Click Map network drive.
    • Choose a drive name to represent the folder.
    • Click Finish.