How To Set Up and Configure an OpenVPN Server on CentOS

A VPN allows you to connect to remote VPN servers, making your connection encrypted and secure and surf the web anonymously by keeping your traffic data private.

There are many commercial VPN providers you can choose from, but you can never be truly sure that the provider is not logging your activity. The safest option is to set up your own VPN server.

In this tutorial we explain how to install OpenVPN on your ServerHub Bare Metal Server or VPS.

OpenVPN is an open source VPN application that lets you create and join a private network securely over the internet

Prerequisites

To complete this tutorial, you will need:

  • Sudo Access to an CentOS server to host your OpenVPN instance.
  • The server should have a firewall configured.
  • Separate dedicated machine to serve as your CA (certificate authority)

Let’s Begin

Configuring easy-rsa

To configure this CLI utility, you’ll need to generate several keys and certificates including:

1.       Certificate Authority (CA)

2.       Server Key and Certificate

3.       Diffie-Hellman key

4.       Client Key and Certificate

Here is what you need to do:

Step 1: Copy the easy-rsa script generation to “/etc/OpenVPN/”.

cp -r /usr/share/easy-rsa/ /etc/openvpn/

Then click on the easy-rsa directory and make changes to the vars file.

cd /etc/openvpn/easy-rsa/2.*/

vim vars

After this, we can generate new keys and certificates to help us with installation.

source ./vars

Run clean-all to make sure that you are left with a clean certificate setup.

./clean-all

Now it’s time to generate a certificate authority (ca). Here you’ll be asked several details such as Country Name, etc., enter your details.

This command will create a ca.key and ca.crt in the /etc/OpenVPN/easy-rsa/2.0/keys/ directory.

./build-ca

Step 2: Generating a Server Key and Certificate

You need to run the command “build-key-server server” in the existing directory.

./build-key-server server

Step 3: Building a Diffie-Hellman Key Exchange

Execute this build-dh command:

./build-dh

It might take some time to generate these files. The waiting time depends on the KEY_SIZE you have set on the file vars.

Step 4: Generating Client Key and Certificate

./build-key client

Step 5: Move or copy the `keys/` directory to `/etc/opennvpn`.

cd /etc/openvpn/easy-rsa/2.0/

cp -r keys/ /etc/openvpn/

Configure OpenVPN

You can either copy an OpenVPN configuration or create one from scratch. You can copy it from /usr/share/doc/openvpn-2.3.6/sample/sample-config-files.

Here is how you can create one:

cd /etc/openvpn/

vim server.conf

Paste this configurations

#change with your port

port 1337



#You can use udp or tcp

proto udp



# “dev tun” will create a routed IP tunnel.

dev tun



#Certificate Configuration



#ca certificate

ca /etc/openvpn/keys/ca.crt



#Server Certificate

cert /etc/openvpn/keys/server.crt



#Server Key and keep this is secret

key /etc/openvpn/keys/server.key



#See the size a dh key in /etc/openvpn/keys/

dh /etc/openvpn/keys/dh1024.pem



#Internal IP will get when already connect

server 192.168.200.0 255.255.255.0



#this line will redirect all traffic through our OpenVPN

push “redirect-gateway def1”



#Provide DNS servers to the client, you can use goolge DNS

push “dhcp-option DNS 8.8.8.8”

push “dhcp-option DNS 8.8.4.4”



#Enable multiple client to connect with same key

duplicate-cn



keepalive 20 60

comp-lzo

persist-key

persist-tun

daemon



#enable log

log-append /var/log/myvpn/openvpn.log



#Log Level

verb 3

Save it.

Now you need to create a new folder for the log file.

mkdir -p /var/log/myvpn/

touch /var/log/myvpn/openvpn.log

How to Disable Selinux and Firewalld

Step 1: disabling firewalld

systemctl mask firewalld

systemctl stop firewalld

Step 2: Disabling SELinux

vim /etc/sysconfig/selinux

Ensure you make SELINUX as disabled.

SELINUX=disabled

Now reboot your server to incorporate the changes.

Configure Routing and Iptables

Step 1: you need to enable iptables

systemctl enable iptables

systemctl start iptables

iptables –F

Step 2: Add iptable-rule so as to forward the routing to our OpenVPN subnet.

iptables -t nat -A POSTROUTING -s 192.168.200.024 -o eth0 -j MASQUERADE

iptables-save > /etc/sysconfig/iptablesvpn

Step 3: Now enable port forwarding

vim /etc/sysctl.conf

Then add this to the end of the line:

net.ipv4.ip_forward = 1.

Step 4: Restart your network server

systemctl start openvpn@server

serverhub

Recent Posts

Comprehensive Guide to Oracle, Microsoft SQL Server, PostgreSQL, and MongoDB: Download, Install, and Configure

Introduction to Oracle Database Oracle Database is a widely adopted relational database management system (RDBMS)…

3 weeks ago

Comprehensive Guide to MySQL: Download, Install, and Configure on Ubuntu and macOS

Introduction to MySQL MySQL is one of the most widely used open-source relational database management…

2 months ago

Deep Learning Applications: Factors to Consider in Choosing the Best Server

Deep learning is a type of artificial intelligence that teaches computers to learn from large…

3 months ago

Comprehensive Guide to OpenSSH: Secure Connectivity for Remote Login

OpenSSH or Open Secure Shell is a powerful suite of tools that provides secure remote…

5 months ago

Mastering VMware ESXi: An Ultimate Guide to Virtualization

Virtualization has become a cornerstone of modern IT infrastructure in today's digital era. Among the…

5 months ago

Setting a First Git Server: A ServerHub Comprehensive Guide

Welcome to ServerHub’s comprehensive guide on installing, setting up, and configuring your first Git server!…

6 months ago

This website uses cookies.