fbpx How to Create a VPN with OpenVPN on Linux: Complete Guide | Certificação Linux

How to Create a VPN with OpenVPN

📅 04/28/2023 ⏱ 4 min ✍️ Uira Ribeiro
Linux
How to Create a VPN with OpenVPN
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

There are several reasons why you might want to create a VPN (Virtual Private Network) using OpenVPN on Linux. Some common reasons include:

  • Security: OpenVPN is one of the most secure options for creating a VPN, as it uses strong encryption to protect communications between devices connected to the VPN. This is especially important if you are using a public Wi-Fi network or accessing the Internet in locations that may be subject to monitoring.
  • Privacy: A VPN can help protect your privacy by hiding your location and preventing third parties from tracking your online activities. By using OpenVPN, you can create a VPN that protects your privacy without needing to trust third-party services.
  • Remote access: With OpenVPN, you can securely access resources on a private network, such as shared files, servers, and other devices, from anywhere in the world, as long as you have an Internet connection.
  • Control: With a self-hosted OpenVPN VPN, you have full control over your VPN's configuration and security policies. This is especially important if you are dealing with confidential or critical information, such as business or customer information.
  • Cost: Setting up an OpenVPN VPN on a Linux server is generally more cost-effective than subscribing to third-party VPN services, especially if you are creating a VPN for commercial or large-scale use.

Overall, OpenVPN is a great option for anyone who needs a secure, reliable, and customizable VPN on Linux.

OpenVPN Installation

To install OpenVPN on a Linux distribution, you can follow these steps:

Open a terminal on your Linux and update the package cache with the following command:

sudo apt update

Install OpenVPN using the following command:

sudo apt install openvpn

If your Linux is from the RedHat family, such as CentOS and Fedora, you should install the EPEL repository:

Visit the official EPEL website (https://fedoraproject.org/wiki/EPEL) and find the EPEL version that corresponds to your operating system version.

You can install the epel repository using one of the two commands:

sudo yum install epel-release
sudo dnf install epel-release

After OpenVPN is installed, you can verify that the installation was successful by running the following command:

openvpn --version

If everything is configured correctly, you will see the installed OpenVPN version.
Now that OpenVPN is installed, you can start configuring your VPN connection.

OpenVPN configuration can vary according to your specific needs, but it generally involves creating a configuration file and connecting to the VPN using that file.

Configure OpenVPN Server

Create the key to share between the server and the client:

cd /etc/openvpn/server
$ openvpn --genkey --secret openvpn.keyopenvpn --genkey --secret openvpn.key

Once the key is created, edit the server file and write the following configuration:

vi server.conf
port 1194 # porta
proto udp # protocolo
dev tun   # cria interface
ifconfig 10.10.10.1 10.10.10.2 # ip da interface e do cliente
secret openvpn.key # arquivo da chave

Now you can start the openvpn service:

sudo systemctl start openvpn-server@server

Note that the "@server" must have the same name as the configuration file in /etc/openvpn/server/server.conf

Verify that openvpn created the tun0 interface:

ip address show
3: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 100
 link/none 
    inet 10.10.10.1/24 scope global tun0
       valid_lft forever preferred_lft forever
    inet6 fe80::c843:a32b:c811:11b0/64 scope link stable-privacy 
       valid_lft forever preferred_lft forever

Configure OpenVPN Client

Copy the openvpn.key key to the client, and place it in the /etc/openvpn/client directory.

Edit the client.conf file

vi client.conf
remote 192.168.10.147 # endereço do servidor openvpn
dev tun # cria a interface
ifconfig 10.10.10.2 10.10.10.1 # designa o ip do cliente como .2 e o servidor .1
secret openvpn.key # arquivo da chave
port 1194 # porta
proto udp # protocolo

Connecting to the OpenVPN Server

To connect to the server using OpenVPN, start the client service:

sudo systemctl start openvpn-client@client

Remember that the "@client" must be the name of the configuration file in /etc/openvpn/client/client.conf

Once the service is running, you can connect to the server using the VPN address, for example using SSH:

ssh [email protected]

This way, you can close port 22 on your server, and keep only UDP port 1194 open.

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp