Skip to content

Instantly share code, notes, and snippets.

@dkgndianko
Last active October 22, 2020 12:22
Show Gist options
  • Save dkgndianko/6658097f3b3946779080798cb173f663 to your computer and use it in GitHub Desktop.
Save dkgndianko/6658097f3b3946779080798cb173f663 to your computer and use it in GitHub Desktop.
Connect to VPN with openvpn

situation

When trying to connect to a VPN via command. And you receive config files like .ovpn client .crt and .key or .p12.

Procedure

  1. First gather config files in a same directory. For this tuto, we assume that every config file is in the directory ~/Config/VPN/myVPN.
  2. Install openvpn package from official repositories.

on Ubuntu

sudo apt-get install openvpn

on Fedora

sudo dnf install openvpn

on MacOs

brew install openvpn

After instlling, make sure that the openvpn command is available in the PATH environment.

  1. Connection To connect to the VPN, go to the path where config files are stored and lauch the command.
cd ~/Config/VPN/myVPN
sudo openvpn --config downloaded-client-config.ovpn --pkcs12 vpn-client.domain.tld.p12

or in a signle command

sudo openvpn --config ~/Config/VPN/myVPN/downloaded-client-config.ovpn --pkcs12 ~/Config/VPN/myVPN/vpn-client.domain.tld.p12

If you see something like this

Thu Oct 22 12:10:46 2020 TUN/TAP device tun0 opened
Thu Oct 22 12:10:46 2020 TUN/TAP TX queue length set to 100
Thu Oct 22 12:10:46 2020 /sbin/ip link set dev tun0 up mtu 1500
Thu Oct 22 12:10:46 2020 /sbin/ip addr add dev tun0 10.***.***.***/27 broadcast 10.***.***.***
Thu Oct 22 12:10:46 2020 /sbin/ip route add ***.87.***.***/** via 192.***.***.***
Thu Oct 22 12:10:47 2020 /sbin/ip route add 0.0.0.0/1 via 10.***.***.***
Thu Oct 22 12:10:47 2020 /sbin/ip route add 128.0.0.0/1 via 10.***.***.***
Thu Oct 22 12:10:47 2020 WARNING: this configuration may cache passwords in memory -- use the auth-nocache option to prevent this
Thu Oct 22 12:10:47 2020 Initialization Sequence Completed

~/Config/VPN/myVPN is the path do the directory where config files are stored. You need to change it accordingly to your situation.

You can even create an alias for that

alias connectMyVPN="sudo openvpn --config ~/Config/VPN/myVPN/downloaded-client-config.ovpn --pkcs12 ~/Config/VPN/myVPN/vpn-client.domain.tld.p12"

You can use alternatives for authentication.

.p12 file

sudo openvpn --config downloaded-client-config.ovpn --pkcs12 vpn-client.domain.tld.p12

.cert and .key files

sudo openvpn --config downloaded-client-config.ovpn --cert vpn-client.domain.tld.cert --key vpn-client.domain.tld.key

username/password

sudo openvpn --config downloaded-client-config.ovpn --auth-user-pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment