DEV Community

Cover image for How To Set Up an OpenVPN Server on Linux Server (Ubuntu)
Mohammad Reza
Mohammad Reza

Posted on

7 3

How To Set Up an OpenVPN Server on Linux Server (Ubuntu)

You can easily follow this steps to have your own VPN on your server.

Image description
1 . Install docker in your server

sudo apt install docker.io
Enter fullscreen mode Exit fullscreen mode

2 . Make a directory with this command

mkdir /opt/openvpn
cd /opt/openvpn
Enter fullscreen mode Exit fullscreen mode

3 . Pick a name for the $OVPN_DATA data volume container

OVPN_DATA="ovpn-data"
Enter fullscreen mode Exit fullscreen mode

4 . Initialize the $OVPN_DATA container that will hold the configuration files and certificates. (replace VPN.SERVERNAME.COM with your server IP)

docker volume create --name $OVPN_DATA
docker run -v $OVPN_DATA:/etc/openvpn --rm kylemanna/openvpn ovpn_genconfig -u udp://VPN.SERVERNAME.COM
docker run -v $OVPN_DATA:/etc/openvpn --rm -it kylemanna/openvpn ovpn_initpki
Enter fullscreen mode Exit fullscreen mode

5 . Start OpenVPN server process

docker run -v $OVPN_DATA:/etc/openvpn -d -p 1194:1194/udp --cap-add=NET_ADMIN kylemanna/openvpn
Enter fullscreen mode Exit fullscreen mode

6 . Take a rest, drink water, walk :))

Image description

7 . Generate a client certificate without a passphrase (replace CLIENTNAME with what you want!)

docker run -v $OVPN_DATA:/etc/openvpn --rm -it kylemanna/openvpn easyrsa build-client-full CLIENTNAME nopass
Enter fullscreen mode Exit fullscreen mode

8 . Retrieve the client configuration with embedded certificates

docker run -v $OVPN_DATA:/etc/openvpn --rm kylemanna/openvpn ovpn_getclient CLIENTNAME > CLIENTNAME.ovpn
Enter fullscreen mode Exit fullscreen mode

You can also use a trick for making ovpn
Make a bash file, name it script.sh and wirte these in it

OVPN_DATA="ovpn-data"
docker run -v $OVPN_DATA:/etc/openvpn --rm -it kylemanna/openvpn easyrsa build-client-full $CLIENTNAME nopass
docker run -v $OVPN_DATA:/etc/openvpn --rm kylemanna/openvpn ovpn_getclient $CLIENTNAME > $CLIENTNAME.ovpn
Enter fullscreen mode Exit fullscreen mode

Now if you want to make ovpn for 10 people you can run this command

for i in {1..10}; do export CLIENTNAME=user$i && sh script.sh ; done
Enter fullscreen mode Exit fullscreen mode

9 . Now you can find your .ovpn file in /opt/openvpn

Image description
10 . (Optional) Just copy files to your machine ,go to your local machine and run this command

scp root@<your-server-ip>:/opt/openvpn/*.ovpn /home/azibom
Enter fullscreen mode Exit fullscreen mode

If you have any questions, you can ask here:)
Share with your friends if you like

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay