How To Connect via OpenVPN on Ubuntu

 Here's a comprehensive guide on how to connect via OpenVPN on Ubuntu:


### Introduction


OpenVPN is a powerful and widely-used Virtual Private Network (VPN) solution that allows secure communication over insecure networks. Connecting to an OpenVPN server on Ubuntu involves several steps, from installing the necessary software to configuring the connection. Let's go through the process step-by-step.


### Step 1: Install OpenVPN


First, ensure that OpenVPN is installed on your Ubuntu system:


```bash

sudo apt update

sudo apt install openvpn easy-rsa

```


### Step 2: Set Up the Certificate Authority


To securely connect to an OpenVPN server, you'll need to set up your own Certificate Authority (CA):


```bash

sudo mkdir /etc/openvpn/easy-rsa

cd /etc/openvpn/easy-rsa

./easyrsa init-pki

./easyrsa build-ca

```


Follow the prompts to create your CA certificate and private key.


### Step 3: Generate Server Certificates and Keys


Generate the server certificate and key:


```bash

./easyrsa gen-req server nopass

./easyrsa sign-req server server

```


Generate Diffie-Hellman parameters:


```bash

./easyrsa gen-dh

```


### Step 4: Configure the OpenVPN Server


Create a configuration file for the OpenVPN server:


```bash

sudo nano /etc/openvpn/server.conf

```


Add the following content:


```

port 1194

proto udp

dev tun

ca ca.crt

cert server.crt

key server.key

dh dh2048.pem

server 10.8.0.0 255.255.255.0

ifconfig-pool-persist ipp.txt

push "redirect-gateway def1 bypass-dhcp"

push "dhcp-option DNS 208.67.222.222"

keepalive 10 120

cipher AES-256-CBC

comp-lzo

user nobody

group nogroup

persist-key

persist-tun

status openvpn-status.log

verb 3

```


### Step 5: Start the OpenVPN Service


Start the OpenVPN service:


```bash

sudo systemctl start openvpn@server

```


Enable it to start on boot:


```bash

sudo systemctl enable openvpn@server

```


### Step 6: Configure Firewall


Allow UDP traffic on port 1194:


```bash

sudo ufw allow 1194/udp

```


### Step 7: Generate Client Certificates and Keys


Generate client certificates and keys:


```bash

./easyrsa gen-req client nopass

./easyrsa sign-req client client

```


### Step 8: Create a Client Configuration File


Create a configuration file for the client:


```bash

nano client.ovpn

```


Add the following content:


```

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-CBC

auth SHA256

comp-lzo

verb 3

<ca>

-----BEGIN CERTIFICATE-----

(Your CA certificate here)

-----END CERTIFICATE-----

</ca>

<cert>

-----BEGIN CERTIFICATE-----

(Your client certificate here)

-----END CERTIFICATE-----

</cert>

<key>

-----BEGIN PRIVATE KEY-----

(Your client private key here)

-----END PRIVATE KEY-----

</key>

```


Replace `YOUR_SERVER_IP` with your server's IP address.


### Step 9: Connect via OpenVPN


There are several ways to connect to the VPN:


1. Using the command line:

   ```bash

   sudo openvpn --config client.ovpn

   ```


2. Using the Network Manager GUI:

   - Go to Settings > Network

   - Click the "+" button to add a new connection

   - Choose "OpenVPN" as the connection type

   - Import the `.ovpn` file you created earlier


3. Using a third-party OpenVPN client app on mobile devices


### Step 10: Verify the Connection


Once connected, you can verify your IP address to ensure you're now using the VPN server's IP:


```bash

curl ifconfig.me

```


This should return an IP address different from your regular internet connection.


### Conclusion


Connecting via OpenVPN on Ubuntu involves setting up both the server and client configurations. The process requires careful attention to security settings and certificate management. Always keep your certificates and keys secure and never share them publicly.


Remember to adjust firewall rules and port forwarding if necessary, depending on your network setup. Also, consider implementing additional security measures like two-factor authentication for enhanced protection.


By following these steps, you should be able to establish a secure OpenVPN connection on your Ubuntu system.

Comments

Popular posts from this blog

bad character U+002D '-' in my helm template

Fix: I come across an error:T ypeError: Cannot join tz-naive with tz-aware DatetimeIndex

GitLab pipeline stopped working with invalid yaml error