How to Set Up an OpenVPN Server on Ubuntu (Step-by-Step)

Have you ever connected to the Wi-Fi at a coffee shop and felt a little uneasy, wondering who might be snooping on your data? Or been frustrated when a streaming service blocks you because you’re in the wrong country? It’s time to take back control of your internet connection.

Building your own Virtual Private Network (VPN) is the ultimate act of digital empowerment. It creates a secure, encrypted tunnel from your device to a server you control, giving you enhanced privacy on public networks, secure access to your home files from anywhere, and the freedom to bypass annoying geographic content restrictions.

In this guide, we’ll walk you through the entire process of setting up your own rock-solid VPN server using OpenVPN, the battle-tested, highly secure, and completely open-source solution. Let’s build your personal digital fortress.

1. Planning Your Setup: Prerequisites and Key Concepts

A great build starts with a solid plan. Before we touch the command line, let’s make sure you have everything you need and understand the core ideas.

What You’ll Need: Server and Software

  • A Cloud Server (VPS): Think of this as your own private computer in a secure data center. A Virtual Private Server is the perfect choice for a VPN. For OpenVPN, you don’t need a beastly machine; a basic plan with 1 CPU and 1GB of RAM is more than enough to get started.

  • A Modern Linux OS: This guide is built for Ubuntu 22.04, one of the most popular and stable choices available. The commands will be very similar for other modern Linux systems.

  • A non-root user with sudo privileges: For security, we never want to work directly as the all-powerful “root” user. We’ll use a standard user account with elevated permissions.

  • A basic firewall: Think of this as a digital bouncer for your server. We’ll use UFW (Uncomplicated Firewall) to make sure only the right traffic gets in.

Prerequisites and Key Concepts
Prerequisites and Key Concepts

Understanding the Core Components

  • What is a Public Key Infrastructure (PKI)? This sounds complicated, but the concept is simple. Imagine a system of digital passports. The Certificate Authority (CA) is the trusted passport office. It issues a server certificate (a passport for your VPN server) and client certificates (passports for your phone, laptop, etc.). When your phone connects, it shows its passport to the server, and the server shows its passport back. Because both were issued by the same trusted “passport office” (your CA), they establish a secure, encrypted connection. We will build our own private passport office!

  • Server vs. Client: The OpenVPN server is the “home base” you are about to configure on your VPS. The OpenVPN clients are your personal devices—your laptop, phone, and tablet—that will connect to this home base.

2. Step-by-Step Server Configuration

Alright, planning is complete. Time to bring your VPN server to life.

Step 1: Initial Server Setup

First, let’s install OpenVPN and the tool we’ll use to create our digital passports, Easy-RSA.

# Update your server's package list
sudo apt update

# Install OpenVPN and Easy-RSA
sudo apt install openvpn easy-rsa

Easy-RSA is a fantastic tool that simplifies the process of creating and managing all the cryptographic certificates needed for our PKI.

Step 2: Building the Public Key Infrastructure (PKI)

Now, we’ll create our “passport office.”

  1. Set up the Easy-RSA Directory:

    # Create a new directory for Easy-RSA
    mkdir ~/easy-rsa
    
    # Link the Easy-RSA scripts into our new directory
    ln -s /usr/share/easy-rsa/* ~/easy-rsa/
  2. Configure the Certificate Authority: Navigate into the new directory and create a configuration file called vars.

    cd ~/easy-rsa
    nano vars

    Paste the following into the file, customizing the values to your liking. This sets the default information for all the “passports” we issue.

    set_var EASYRSA_REQ_COUNTRY    "US"
    set_var EASYRSA_REQ_PROVINCE   "California"
    set_var EASYRSA_REQ_CITY       "San Francisco"
    set_var EASYRSA_REQ_ORG        "My Private VPN"
    set_var EASYRSA_REQ_EMAIL      "me@example.com"
    set_var EASYRSA_REQ_OU         "IT"
    set_var EASYRSA_ALGO           "ec"
    set_var EASYRSA_DIGEST         "sha512"
  3. Build the Certificate Authority (CA):

    # Initialize the PKI
    ./easyrsa init-pki
    
    # Build the CA, you'll be asked for a passphrase. Choose a strong one!
    ./easyrsa build-ca
Step-by-Step Server Configuration
Step-by-Step Server Configuration

🚨 CRITICAL: The command above creates pki/ca.crt (your public certificate) and pki/private/ca.key (your private key). The ca.key file is the heart of your security. Protect it at all costs. Anyone who gets this key can sign their own trusted certificates and potentially access your VPN.

Step 3: Generating Server and Client Credentials

With our CA ready, we can now issue passports for our server and our first client device.

  1. Generate Server Certificate & Key:

    # The 'nopass' option means the server can start without you typing a password
    ./easyrsa gen-req server nopass
    ./easyrsa sign-req server server
  2. Generate Diffie-Hellman Key: This is a clever mathematical trick used to securely exchange keys at the start of the connection.

    ./easyrsa gen-dh
  3. Generate HMAC Key: This acts like a digital wax seal, adding another layer of verification to ensure traffic isn’t tampered with.

    openvpn --genkey --secret pki/ta.key
  4. Generate Client Certificate & Key: Let’s create credentials for our first device, which we’ll call “client1”.

    ./easyrsa gen-req client1 nopass
    ./easyrsa sign-req client client1

Step 4: Configuring the OpenVPN Service

Now we’ll write the rulebook for our OpenVPN server.

  1. Copy all the generated keys and certificates to the OpenVPN directory:

    sudo cp pki/{ca.crt,dh.pem,ta.key} /etc/openvpn/
    sudo cp pki/issued/server.crt /etc/openvpn/
    sudo cp pki/private/server.key /etc/openvpn/
  2. Create the main configuration file:

    sudo nano /etc/openvpn/server.conf

    Paste the following configuration inside. I’ve added comments to explain what each critical line does.

    port 1194                  # The port OpenVPN listens on
    proto udp                  # Use UDP protocol (faster for VPNs)
    dev tun                    # Use a TUN tunnel interface
    
    ca ca.crt                  # Our Certificate Authority file
    cert server.crt            # Our server's public certificate
    key server.key             # Our server's private key
    dh dh.pem                  # Diffie-Hellman parameters
    
    topology subnet
    server 10.8.0.0 255.255.255.0 # The internal IP address range for clients
    
    # This line tells clients to route ALL their internet traffic through the VPN
    push "redirect-gateway def1 bypass-dhcp"
    
    # Provide clients with DNS servers to prevent DNS leaks
    push "dhcp-option DNS 208.67.222.222" # OpenDNS
    push "dhcp-option DNS 1.1.1.1"       # Cloudflare DNS
    
    keepalive 10 120
    tls-auth ta.key 0          # The HMAC key for integrity
    cipher AES-256-GCM
    auth SHA256
    user nobody
    group nogroup
    persist-key
    persist-tun
    status /var/log/openvpn/openvpn-status.log
    verb 3                     # Verbosity level for logs
    explicit-exit-notify 1

Step 5: Adjusting Server Networking and Firewall

The final server-side step is to tell Linux it’s okay to forward internet traffic and to configure our firewall.

  1. Enable IP Forwarding:

    sudo nano /etc/sysctl.conf

    Find the line #net.ipv4.ip_forward=1 and uncomment it (remove the #). Save the file and apply the change:

    sudo sysctl -p
  2. Configure the Firewall (UFW):

    # Allow OpenVPN traffic on its port
    sudo ufw allow 1194/udp
    
    # Allow SSH so you don't lock yourself out!
    sudo ufw allow ssh
    
    # Set up Network Address Translation (NAT)
    # This makes all VPN client traffic appear to come from the server's IP
    sudo nano /etc/ufw/before.rules

    Add the following block of text at the very top of the file:

    # START OPENVPN RULES
    # NAT table rules
    *nat
    :POSTROUTING ACCEPT [0:0]
    # Allow traffic from OpenVPN client to eth0 (the public internet)
    -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
    COMMIT
    # END OPENVPN RULES

    Now, tell UFW to allow forwarded packets and restart it:

    sudo nano /etc/default/ufw

    Change DEFAULT_FORWARD_POLICY=”DROP” to DEFAULT_FORWARD_POLICY=”ACCEPT”. Save, then enable and start UFW.

    sudo ufw enable
  3. Start the OpenVPN Service:

    sudo systemctl start openvpn@server
    sudo systemctl enable openvpn@server

3. Configuring and Connecting Your Devices

Your server is ready! Now let’s prepare the “passport” for your personal device.

Creating a Unified Client Configuration Profile

The cleanest way to connect is with a single .ovpn file that contains everything.

  1. Create a base configuration file for your client:

    nano ~/client1.ovpn

    Paste this template inside, replacing your_server_ip with your server’s actual public IP address.

    client
    dev tun
    proto udp
    remote your_server_ip 1194
    resolv-retry infinite
    nobind
    persist-key
    persist-tun
    remote-cert-tls server
    cipher AES-256-GCM
    auth SHA256
    verb 3
    <ca>
    # Your ca.crt contents will go here
    </ca>
    <cert>
    # Your client1.crt contents will go here
    </cert>
    <key>
    # Your client1.key contents will go here
    </key>
    <tls-auth>
    # Your ta.key contents will go here
    </tls-auth>
    key-direction 1
  2. Now, manually copy and paste the contents of your certificate and key files into the corresponding sections of client1.ovpn:

    • ~/easy-rsa/pki/ca.crt -> inside the <ca> block

    • ~/easy-rsa/pki/issued/client1.crt -> inside the <cert> block

    • ~/easy-rsa/pki/private/client1.key -> inside the <key> block

    • ~/easy-rsa/pki/ta.key -> inside the <tls-auth> block

Connecting from Any Device

  1. Securely transfer the completed client1.ovpn file from your server to your local computer. Using a tool like scp or FileZilla is a great way to do this. Do not email it!

  2. Download the official OpenVPN Connect client for your device:

    • Windows

    • macOS

    • Android

    • iOS

  3. Open the app, choose to import a profile, and select your client1.ovpn file. Click connect, and you’re in!

4. Security, Maintenance, and Troubleshooting

Your VPN is a living thing; it needs a little care to stay secure and healthy.

Ongoing Security and Maintenance

  • Revoking Client Certificates: If you lose your phone or a device is compromised, you must revoke its “passport” On the server, run ./easyrsa revoke client 1 and then regenerate the CRL. This is an advanced topic, but crucial for security.

  • Keeping the Server Updated: Regularly run sudo apt update && sudo apt upgrade on your server to apply the latest security patches to both Linux and OpenVPN.

Ongoing Security and Maintenance
Ongoing Security and Maintenance

Common Issues and How to Fix Them

  • Can’t Connect:

    1. Check your server’s firewall. Is port 1194/udp allowed?

    2. Check if the OpenVPN service is running (sudo systemctl status openvpn@server).

    3. Check the client logs in the OpenVPN Connect app for error messages.

  • Connected but No Internet:

    1. Double-check that IP forwarding is enabled (cat /proc/sys/net/ipv4/ip_forward should return 1).

    2. Verify your NAT rules in /etc/ufw/before.rules are correct.

    3. Make sure you push DNS servers in your server.conf.

5. Your Private Tunnel to the World

Congratulations! You have successfully built and secured your own private VPN server. You now hold the keys to a more secure, private, and unrestricted internet experience. You’ve unlocked the ability to browse safely on public Wi-Fi, access your home network from across the globe, and watch your favorite shows no matter where you are.

From here, you can explore advanced topics like setting up a “kill switch” on your client devices or configuring split-tunneling to only route specific traffic through the VPN. Your journey to digital freedom has just begun.

Sign up for

In order not to miss any news or promotions from Vinahost

    Related Posts
    Comments
    Subscribe
    Notify of
    guest
    0 Góp ý
    Oldest
    Newest Most Voted
    Inline Feedbacks
    View all comments
    Total visit: views