App Support.

We're here to help.

Setting up an OpenVPN server with Ubuntu and Viscosity

This guide will walk you through the steps involved in setting up an OpenVPN server on an Ubuntu host that allows you to securely access your home/office network from a remote location and optionally send all of your network traffic through it so you can access the internet securely as well.

Before using this guide, we highly recommend you read through our Introduction to Running an OpenVPN Server Article.


For this guide, we assume:

  • You have already installed the latest version of Ubuntu (16.04 at time of writing)
  • You have root access to this installation
  • This installation of Ubuntu is a clean install
  • You have access to the server via Terminal or SSH
  • You are able to copy files on and off the server using SCP or a similar protocol
  • You already have a copy of Viscosity installed on your client device

If you need to download and install a copy of Ubuntu, information can be found at We won't be covering the details of setting up an Ubuntu instance, many guides can be found online. If you are running a different version of Ubuntu, it's very likely that many or even all of the steps outlined in this guide will still apply. If you are looking to setup an OpenVPN server on a different operating system, please check out our other guides.

If you don't have a copy of Viscosity already installed on your client, then please check out this setup guide for installing Viscosity (Mac | Windows).



For further information or help with Ubuntu, there are a range of community resources available at

Getting Started

First, update your server:

sudo apt-get update
sudo apt-get -y upgrade
sudo apt-get -y dist-upgrade

You may be informed that you need to restart after the package upgrades are complete. If so, go ahead and restart, then log back in and continue on.

To set up an OpenVPN server, we need to install OpenVPN. Run the following commands, waiting for each to complete, to add OpenVPNs repository to apt so OpenVPN is automatically installed and updated, and then to install OpenVPN:

sudo wget -O - | sudo apt-key add -
sudo apt-get update
sudo apt-get -y install openvpn

Generating Configurations, Certificates and Keys

The next step is to generate your configurations for the server and your clients as well as certificates to go with them. You can do this easily by following the Creating Certificates and Keys. You can either follow the section for Ubuntu and copy your client configurations off the server (we recommend this), or generate everything on your PC or Mac and copy the server folder that is generated onto your Ubuntu server.

If you use the default DNS Server (, you will need to setup a DNS server yourself, instructions are at the end of this article. We recommend instead using an existing DNS server, a publically available DNS server like Google's ( and is the easiest.

IP Forwarding

In order to forward our requests passing through the VPN, we want the OpenVPN server to act like a router. As such, we need to enable IP forwarding. In the terminal, we can enable IP forwarding on the Ubuntu server by entering:


However, every time we reboot the server, this command will be undone. To ensure that doesn't happen, we need to modify the sysctl.conf file using nano. Enter:

nano /etc/sysctl.conf

Scroll down to the section:

# Uncomment the next line to enable packet forwarding for IPv4

Delete the ‘#’ character at the start of the line so that it now becomes:


When you are done, press ctrl + x to exit nano. Save the changes when prompted.

To ensure that hosts on the home/office network can find the VPN server, we need to make the server respond to any ARP requests:

echo 1 > /proc/sys/net/ipv4/conf/ens33/proxy_arp

where ens33 is the network interface of the home/office network.

Firewall Rules

We will use the Uncomplicated Firewall (ufw) that is installed by default on Ubuntu to control how traffic is passed through the OpenVPN server. If your Ubuntu server is externally accessible (has a direct connection to the internet with its own IP address, without a router), then you should use ufw to allow the OpenVPN port we have set up above (1194). Otherwise, we will set ufw to allow all traffic and configure the settings for routing.

If your Ubuntu server DOES NOT need ufw to protect it (i.e. it is behind a router or has a different firewall set up), you need to set the default input rule to allow all traffic to pass through ufw by default. To do so, we need to modify the configuration file.

  1. Open this file in nano:
    nano /etc/default/ufw
  2. Scroll down to the section:
  3. Since our server will not be using this firewall for protection, change this from "DROP" to "ACCEPT"
  4. Scroll down to the section:
  5. Change the forward policy from "DROP" to "ACCEPT"
  6. Press ctrl + x to exit nano. Save the changes when prompted.

If your Ubuntu server DOES need ufw to protect it, you need to tell it which ports to allow as it has the default input and forward policies of DROP. If you are running the OpenVPN server from some remote location, you will need to SSH into it to change any settings. As such, we need to tell the firewall to permit SSH traffic.

  1. Entering the following into the terminal:
    ufw allow ssh
  2. The VPN traffic we will send to the OpenVPN server will be over UDP on port 1194, so enter into the terminal:
    ufw allow 1194/udp

If you have other services running on your Ubuntu server, then you need to make sure that you allow their traffic through ufw as well. Make sure to add any allow rules for any other ports your Ubuntu server is listening on (such as a Plex media server or maybe your own email server).

Now, regardless of your network setup, you need to set up the routing rules for ufw.

  1. Open the ufw configuration file in nano:
    nano /etc/ufw/before.rules
  2. Move the cursor below the section:
    # rules.before
    # Rules that should be run before the ufw command line added rules. Custom
    # rules should be added to one of these chains:
    # ufw-before-input
    # ufw-before-output
    # ufw-before-forward
  3. Paste in the following:
    # NAT table rules
    # Allow OpenVPN client to communicate with local home network
    -A POSTROUTING -s -d -o ens33 -j MASQUERADE
    # Allow traffic from OpenVPN client to ens32
    -A POSTROUTING -s -o ens32 -j MASQUERADE
    • The ens32 term assumes the connection from the OpenVPN server to the internet is through the adapter ens32
    • The ens33 term assumes this is the interface connected to the home LAN network. If your Ubuntu server only has a single interface (ens32), then you can comment out this line
    • We assume is the subnet for your home/office network
  4. Move the cursor below the section:
    # Don't delete these required lines, otherwise there will be errors
    :ufw-before-input - [0:0]
    :ufw-before-output - [0:0]
    :ufw-before-forward - [0:0]
    :ufw-not-local - [0:0]
    # End required lines
  5. Paste in the following:
    #Accept all traffic to and from VPN
    -A ufw-before-input -i tun+ -j ACCEPT
    -A ufw-before-output -i tun+ -j ACCEPT

    # Forward traffic to and from the VPN
    -A ufw-before-forward -s -j ACCEPT
    -A ufw-before-forward -d -j ACCEPT

Ufw is now ready to be activated.

  1. In the terminal, type:
    ufw enable
  2. If you are SSH’d into this Ubuntu instance, it will display the prompt:
    To which you should reply with y.

The following output will then be displayed (regardless of SSH or not)

Firewall is active and enabled on system startup

Starting the OpenVPN Server

At this point the basics are complete. Your server configuration and files should be on your server, either generated on the server, or copied on from where you generated them.

First, copy the server configuration files to the OpenVPN directory:

sudo cp /path/to/configs/server/* /etc/openvpn/

Now start your server, and enable it to start after a reboot as well.

sudo systemctl enable [email protected]
sudo systemctl start [email protected]

To check the server status, enter:

sudo systemctl status [email protected]

To which it should reply with:

Active: active (running) since Thu 2017-06-01 17:06:29 PDT; 4s ago

If the server is not listed as active (running), you should see a log output displaying what has gone wrong.

Your OpenVPN server is now up and running and ready for you to connect to it.

Router Setup

If your Ubuntu server is directly accessible, then you can skip this section. There is no router to configure.

However if your Ubuntu server is behind a router (such as on your home WiFi), then you will need to configure your router to permit VPN traffic. Please see our Introduction to Running an OpenVPN Server Article for more information.

Setting Up Viscosity

The final step is to setup Viscosity. Thanks to openvpn-generate, this is as easy as importing and connecting.


Copy your *.visz file you created with openvpn-generate to your Mac or Windows machine with Viscosity installed and double click the file. You should see a prompt that the config was imported successfully.

Connecting and Using Your VPN Connection

You are now ready to connect. Click on the Viscosity icon in the menu bar (Windows: system tray) and select 'Connect DemoConnection'. That's it, you should see a notification that you're now connected!

To check that the VPN is up and running, you can use the Viscosity details window. Click the Viscosity menu bar (Windows: system tray) icon and select 'Details...'. This will bring up the details window.

This window will show you the traffic passing through the VPN connection.

That's it, you've set up your very own OpenVPN server. Congratulations, you are now free to enjoy the benefits of operating your own OpenVPN server!

Transferring Files

The most simple way to transfer files between your server and a client with decent security is to use SCP. If you have SSH access on your server, SCP should also work. We recommend using a client like Cyberduck, Transmit or WinSCP. You will need to copy the files into the user directory you connect to and then transfer the files around using command line.

Alternatively if you have physical access to the server, you may wish to use a USB thumbdrive instead.

Basic DNS Server

If you are electing to run your own DNS server for clients, the following is an example setup for dnsmasq. Install dnsmasq with the following command:

sudo apt-get install -y dnsmasq

You first need to disable the dnsmasq instance provided by NetworkManager by default. This default instance is restricted and won't allow us to listen for DNS requests over the VPN.

  1. Open the NetworkManager configuration:
    nano /etc/NetworkManager/NetworkManager.conf
  2. Comment out the line enabling dnsmasq by adding a '#' character to the front of the line:
  3. Press ctrl + x to exit nano. Save the changes when prompted.

Next we need to modify some of the default configuration.

  1. Create a backup of the original dnsmasq configuration:
    cp /etc/dnsmasq.conf /etc/dnsmasq.conf.bak
  2. Set the DNS server to listen for requests from the Ubuntu server ( and from the VPN (
    echo -e "listen-address=,\nbind-interfaces" > /etc/dnsmasq.conf
  3. Prevent our DNS requests from flooding the root DNS servers with bad requests:
    echo -e "domain-needed\nbogus-priv" >> /etc/dnsmasq.conf
  4. Use the Google DNS servers to resolve requests (you are free to use your DNS resolution service of choice):
    echo -e "server=\nserver=" >> /etc/dnsmasq.conf
  5. Restart the dnsmasq service to activate our changes:
    service dnsmasq restart

To check that the DNS server is listening on the addresses we requested, type:

netstat -anup

You should see a list of addresses, including and

udp        0      0*                       54892/dnsmasq   
udp        0      0 *                       54892/dnsmasq   

Lastly, we need to ensure that dnsmasq is started after OpenVPN on system startup. By default, dnsmasq runs before OpenVPN, which prevents it from setting up the DNS server for the VPN as the VPN doesn't exist yet.

  1. Create a backup of the original file:
    cp /etc/rc.local /etc/rc.local.bak
  2. Type:
    echo -e "service dnsmasq restart\nexit 0" >> /etc/rc.local
  3. Make this script executable:
    chmod +x /etc/rc.local

We are now done setting up the DNS server.