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

AI Agent image

How to Build an AI Agent with Semantic Kernel (and More!)

Join Developer Advocate Luce Carter for a hands-on tutorial on building an AI-powered dinner recommendation agent. Discover how to integrate Microsoft Semantic Kernel, MongoDB Atlas, C#, and OpenAI for ingredient checks and smart restaurant suggestions.

Watch the video 📺

Top comments (0)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

đź‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay