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 (20.04 at time of writing)
  • You have root access to this installation
  • This installation of Ubuntu is a clean install
  • Know the name of your network interfaces (instructions just below)
  • 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).

If you're new to SSH or SCP, we have some instructions in our Introduction Guide.

To get the name of your Primary Network Interface (WAN Access), run the following on your Ubuntu Server:
ip route | grep default | awk '{print $NF}'

If you have a second network interface for accessing machines on your local network, you will need the name of this as well when setting up the firewall and routing.


Unfortunately we cannot provide any direct support for setting up your own OpenVPN server. We provide this guide as a courtesy to help you get started with, and make the most of, your copy of Viscosity. We've thoroughly tested the steps in this guide to ensure that, if you follow the instructions detailed below, you should be well on your way to enjoying the benefits of running your own OpenVPN server.

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

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 -
echo "deb focal main" | sudo tee /etc/apt/sources.list.d/openvpn-aptrepo.list
Note: If you are using an older Ubuntu distribution, substitute "focal" in the command above for "xenial" if Ubuntu 16.04, or "bionic" if Ubuntu 18.04.

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 Guide. 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.

Once you have generated your Configurations, you will need to transfer the server configuration to your server, or transfer your client configurations off your server. If you have local access, a USB drive is the easiest. If not, we have some help on how to transfer files with SCP in our Introduction Guide.

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:

echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward

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:

sudo 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 | sudo tee /proc/sys/net/ipv4/conf/ens33/proxy_arp

where ens33 is the network interface of the home/office network if you have two network interfaces, or your Primary Network Interface (WAN) otherwise (See Preparation section for more information).

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:
    sudo nano /etc/default/ufw
  2. Scroll down to the section:
    # Set the default input policy to ACCEPT, DROP, or REJECT. Please note that if
    # you change this you will most likely want to adjust your rules.
  3. Since our server will not be using this firewall for protection, change this from "DROP" to "ACCEPT"
  4. Scroll down to the section:
    # Set the default forward policy to ACCEPT, DROP or REJECT.  Please note that
    # if you change this you will most likely want to adjust your rules
  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:
    sudo 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:
    sudo 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).

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

  1. Open the ufw configuration file in nano:
    sudo 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

    # Uncomment the next POSTROUTING line and replace ens33 with the name
    # of your Local Network Interface if you have a second network interface
    # for accessing machines on your local network
    # Allow OpenVPN client to communicate with local home network
    #-A POSTROUTING -s -d -o ens32 -j MASQUERADE

    # Allow traffic from OpenVPN client to your WAN and/or LAN via ens33
    -A POSTROUTING -s -o ens33 -j MASQUERADE
    • Replace ens33 with the name of your Primary Network Interface if it is different (See Preparation section for more information)
  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
  6. Press ctrl + x to exit nano. Save the changes when prompted.

Ufw is now ready to be activated.

  1. In the terminal, type:
    sudo ufw enable
  2. If you are SSH’d into this Ubuntu instance, it will display the prompt:
    Command may disrupt existing ssh connections. Proceed with operation (y|n)?
    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 macOS menu bar or Windows system tray to open the Viscosity Menu, select the connection you imported, and Viscosity will connect.

To check that the VPN is up and running, you can open the Details window from the Viscosity Menu. This will allow you to view connection details, traffic and the OpenVPN log.


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!

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

Proto Recv-Q Send-Q Local Address         Foreign Address       State     PID/Program name
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.