DEV Community

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

Posted on

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

Top comments (0)