#!/bin/bash # this script is to be run on server after the server script! # tested on Oracle cloud with Ubuntu 20.04 (IPv6 was not configured there, traffic goes through IPv4) # to connect on Linux: # sudo openvpn --config ovpnc01.ovpn SERVER='vpn_domain_name_goes_here.com' PORT=443 PROTOCOL='tcp' USR=$(logname) RSA_DIR="/home/${USR}/easy-rsa" CLIENT_DIR="/home/${USR}/client-configs" KEY_DIR="${CLIENT_DIR}/keys" FILES_DIR="${CLIENT_DIR}/files" user=$(whoami) if [ $user != root ]; then echo "You are using a non-privileged account" exit -1 fi if ! test -d $RSA_DIR; then echo 'Run the server script first!' exit -1 fi if [[ ! ${1+x} ]]; then echo 'Provide a client name as an argument to this script!' exit -1 else CLIENT=$1 fi if ! test -d $CLIENT_DIR; then mkdir $CLIENT_DIR mkdir $KEY_DIR mkdir $FILES_DIR cp /etc/openvpn/server/ta.key "${KEY_DIR}/" cp /etc/openvpn/server/ca.crt "${KEY_DIR}/" else echo 'Well, hello friend!' fi # create a request and get a signed certificate out of it cd $RSA_DIR ./easyrsa gen-req $CLIENT nopass ./easyrsa import-req "${RSA_DIR}/pki/reqs/${CLIENT}.req" $CLIENT ./easyrsa sign-req client $CLIENT cp "${RSA_DIR}/pki/private/${CLIENT}.key" "${KEY_DIR}/" cp "${RSA_DIR}/pki/issued/${CLIENT}.crt" "${KEY_DIR}/" # create a config file for the client cd $FILES_DIR CFG=$(cat <') \ ${KEY_DIR}/ca.crt \ <(echo -e '\n') \ ${KEY_DIR}/${CLIENT}.crt \ <(echo -e '\n') \ ${KEY_DIR}/${CLIENT}.key \ <(echo -e '\n') \ ${KEY_DIR}/ta.key \ <(echo -e '') \ > ${CLIENT}.ovpn chown -hR "${USR}:${USR}" "${CLIENT_DIR}" chmod -R 700 $CLIENT_DIR