In this article, we'll explain how to connect to your private OpenVPN server over SSH Tunnel to encapsulate the transmitted data for added security to your connection.
Requirements:
- Linux VPS or Dedicated Server with SSH access enabled.
- OpenVPN Server installed on your VPS/Dedicated server.
For more about OpenVPN server installation on CentOS / RHEL / Almalinux / RockyLinux, Check this Github repo or follow this article.
OpenVPN server must run on TCP protocol and port 443 is recommended. - Mac OS machine
- Terminal app
- OpenVPN Connect app installed on your Mac.
Ignore the requirement #2 if you already have the .ovpn profile ready.
Prepare the VPN profile
After you've installed OpenVPN server using the installation script mentioned above, the installation script will create the .ovpn profile on the server. Copy this profile to your PC (the profile must have ".ovpn" extension).
40.40.40.40 and OpenVPN Server is listening to TCP port 443)- Open the downloaded OpenVPN profile on your Mac with a text editor
- Replace the line
remote 40.40.40.40 443withremote 127.0.0.1 8585
(127.0.0.1is the localhost IP, and8585is the local port "you can use any available port number"). - Save the profile.
Import Profile to OpenVPN Connect App
- Open OpenVPN Connect App
- Import the edited .ovpn profile
- Proceed to the next step.
Open Terminal Window on Mac
- Open Terminal App.
- Run this command to initiate an SSH connection with port forwarding enabled
ssh -v -N -L 127.0.0.1:8585:<VPN_SERVER_IP>:<VPN_PORT> root@<SSH_SERVER_IP>replace
<VPN_SERVER_IP>with the VPN server IP, in out example it's 40.40.40.40
replace<VPN_PORT>with the VPN port, in out example it's 443
replace<SSH_SERVER_IP>with the IP address of an SSH-accessible server. This may be the same as the VPN server IP if SSH access is available, or any other server you can access via SSH.
replacerootwith the SSH user. - Enter the SSH password when prompted.
- Now you've an active SSH Connection with Port forwarding enabled on local port 8585.
Securely connect to VPN over SSH Tunnel
Now you've SSH Connection open on Terminal, leave it open all the time while you're using the VPN.
- Back to OpenVPN Connect App
- Select the VPN profile and Connect.
- You're now connected over the SSH tunnel!
