Published on September 15, 2024By DeveloperBreeze

How to Use OpenVPN on Ubuntu

Introduction

OpenVPN is a robust, open-source VPN (Virtual Private Network) solution that provides secure access to private networks over the internet. It is one of the most popular and flexible VPN services available. This tutorial will guide you through installing and configuring OpenVPN on Ubuntu, allowing you to establish a secure connection to a remote network.

---

Table of Contents

  1. Prerequisites
  • Ubuntu version and user permissions
  • Required packages and dependencies
  1. Installing OpenVPN on Ubuntu
  • Using the Ubuntu package manager
  • Verifying the installation
  1. Setting Up OpenVPN Client
  • Acquiring the OpenVPN configuration file (.ovpn)
  • Configuring the OpenVPN client on Ubuntu
  1. Starting and Stopping the OpenVPN Service
  • Connecting to the VPN
  • Stopping the VPN connection
  1. Verifying the OpenVPN Connection
  • Checking connection status
  • Confirming IP address and route changes
  1. Automatic OpenVPN Start on Boot
  • Configuring OpenVPN to start on system boot
  1. Troubleshooting Common OpenVPN Issues
  • Diagnosing connectivity issues
  • Resolving DNS and routing problems

---

1. Prerequisites

Before getting started with OpenVPN, make sure your system meets the following requirements:

  • Ubuntu Version: This tutorial is based on Ubuntu 20.04 LTS or later, but it can be adapted to other versions of Ubuntu.
  • User Permissions: You need sudo or root privileges to install and configure OpenVPN.
  • OpenVPN Configuration File: You need a valid `.ovpn` configuration file from your VPN provider or system administrator. This file contains all the settings needed to connect to the VPN.

---

2. Installing OpenVPN on Ubuntu

OpenVPN is included in the default Ubuntu package repositories, making installation straightforward.

Step 1: Update Package List

Before installing any new software, it’s a good idea to update your package list to ensure all packages are up to date.

sudo apt update

Step 2: Install OpenVPN

To install OpenVPN, use the following command:

sudo apt install openvpn

The `openvpn` package contains both the OpenVPN server and client software, but this tutorial focuses on setting up the client.

Step 3: Verify Installation

Once the installation is complete, you can verify the installation by checking the version of OpenVPN:

openvpn --version

You should see version information and details about the configuration of the OpenVPN installation.

---

3. Setting Up OpenVPN Client

To set up the OpenVPN client, you need a `.ovpn` configuration file provided by your VPN provider or network administrator. This file contains all the necessary details to establish a VPN connection.

Step 1: Copy the OpenVPN Configuration File

Place the `.ovpn` configuration file in the `/etc/openvpn/` directory. You may need to rename the file for consistency:

sudo cp /path/to/your-vpn-config.ovpn /etc/openvpn/client.conf

Replace `/path/to/your-vpn-config.ovpn` with the actual path to your configuration file.

Step 2: Check the Configuration File

You can open the `.ovpn` file in a text editor (like `nano`) to ensure it contains all the necessary details, such as:

  • Remote server address
  • Authentication type (password, certificate)
  • Encryption protocols
sudo nano /etc/openvpn/client.conf

If any details need to be added or modified, you can edit the file here.

---

4. Starting and Stopping the OpenVPN Service

Now that your configuration file is in place, you can start and stop the VPN connection.

Step 1: Start OpenVPN

To start the OpenVPN connection, use the following command:

sudo openvpn --config /etc/openvpn/client.conf

You may be prompted for your VPN username and password if the configuration requires authentication.

Step 2: Stop OpenVPN

To stop the OpenVPN connection, simply press `Ctrl + C` in the terminal where OpenVPN is running.

Alternatively, if you’re running OpenVPN as a service:

sudo systemctl stop openvpn@client

---

5. Verifying the OpenVPN Connection

Once connected, you should verify that the OpenVPN connection is working properly.

Step 1: Check the Connection Status

Open a terminal and check the status of the OpenVPN service:

sudo systemctl status openvpn@client

This will display whether OpenVPN is active and connected to the VPN server.

Step 2: Confirm IP Address

You can verify that your IP address has changed by running the following command:

curl ifconfig.me

This will show your public IP address. If the VPN connection is successful, it should display the IP address of the VPN server, not your local ISP-provided IP address.

Step 3: Check Routing Table

To verify that your traffic is being routed through the VPN, check the routing table with:

ip route

You should see routes that direct traffic through the VPN interface (often labeled as `tun0`).

---

6. Automatic OpenVPN Start on Boot

To configure OpenVPN to start automatically at boot, you can enable the OpenVPN service.

Step 1: Enable OpenVPN Service

Run the following command to enable OpenVPN at boot:

sudo systemctl enable openvpn@client

This ensures that the VPN connection is initiated every time your system boots up.

Step 2: Start the Service

To start the service without rebooting:

sudo systemctl start openvpn@client

---

7. Troubleshooting Common OpenVPN Issues

Here are a few common problems and solutions when using OpenVPN.

Issue 1: OpenVPN Fails to Connect

  • Check the logs: Run `journalctl -xe` or `sudo systemctl status openvpn@client` to check for specific errors in the OpenVPN logs.
  • Firewall/Port Issues: Ensure that the VPN port (usually `1194` for UDP) is not blocked by your firewall or ISP.

Issue 2: DNS Issues After Connecting to VPN

If you experience DNS resolution problems after connecting to OpenVPN:

  • Edit `/etc/resolv.conf`: Add the DNS server provided by your VPN provider.
  sudo nano /etc/resolv.conf

Add the following line, replacing the IP with your VPN's DNS:

  nameserver 8.8.8.8

Issue 3: Internet Not Working After VPN Connects

  • Check Default Gateway: Ensure the VPN server is configured as the default gateway. Use `ip route` to confirm routing.
  • Split Tunneling: If your configuration is set up for split tunneling, check that only specific traffic is routed through the VPN.

---

Conclusion

Using OpenVPN on Ubuntu provides a secure and flexible way to access remote networks. By following this tutorial, you’ve learned how to install OpenVPN, configure a client, and establish a secure VPN connection. Additionally, we’ve covered automatic startup and troubleshooting common issues, ensuring that your OpenVPN connection remains stable and reliable.

If you want further security and automation, you can explore using NetworkManager for easy VPN management or integrating OpenVPN with firewall rules for enhanced protection.

Comments

Please log in to leave a comment.