Skip to content

Instantly share code, notes, and snippets.

@proffapt
Last active May 1, 2024 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save proffapt/15cacf6c0abdd5509e5c1b7d2c7a49ce to your computer and use it in GitHub Desktop.
Save proffapt/15cacf6c0abdd5509e5c1b7d2c7a49ce to your computer and use it in GitHub Desktop.
Elaborated step by step guide to setup OpenVPN on Azure via student discount

Setup OpenVPN Server on Azure


Step 1: Get Microsoft Azure

  • Avail the Github Student Developer Pack.
  • Now navigate to the benefits page and apply a filter for cloud or just click here. Follow the steps to sign up for Azure, and you will receive $100 credits.

Note Although, we could have done it directly using Institute ID on Microsoft Azure. But the afore-mentioned method exposes you to various other possibilities which you might have not even thought of. We chose Microsoft Azure here, if you want you can also choose DigitalOcean or any other cloud platform of your preference.

Step 2: Create an EC2 instance

  • Goto Azure portal

  • Click on the hamburger menu > Create a resource > Compute > Ubuntu Server 22.04 LTS. Fill in the necessary details in the Basics section.
    image image image

    • Create a new Resource Group & give your virtual machine a name.
      image

    • Now about region & disk size.
      First select the cheapest size and then select the region from the available options. A standard B1s size is going to be good enough and will last around 11 months using free credits. Now choose the closest region where the said size is available, which in our case will be South-East Asia. A bigger (aka more costly) size would probably be available in Indian regions.
      image
      image
      image
      image

    • Now choose an Authentication method according to your preference.

      • Using ssh keys is more secure but hard to follow.

        Using SSH keys
        • Select SSH public key as the Authentication Method and fill in the required fields.
          image

        • Choose HTTPS(443) in Select inbound ports.
          image

        • Leave the rest of the settings as default in other sections and click Review+Create.

        • Now you will be prompted to Generate a new key pair, select Download private key and create Resource. This step will download a .pem file onto your local machine.
          image
          image

      • Using password is easy to follow but less secure.

        Using password
        • Select Password as the Authentication Method and fill in the required fields.
          image

        • Choose HTTPS(443) in Select inbound ports.
          image

        • Leave the rest of the settings as default in other sections and click Review+Create.

  • Now, wait for the VM to be deployed. Once the VM is deployed

    • Click Goto Resource.
      image

    • Click Configure for the DNS option under Networking.
      image

    • Type in any DNS name like your username in the DNS name label field and press Save.
      image

Step 3: SSH into the Remote Server

Warning

For this step you will need to switch to a network other than that of campus as PORT 22(default port for SSH) IS BLOCKED ON CAMPUS NETWORK.

SSH steps are drastically different for a Windows client & a Linux/MacOS (*nix) client.
Click on the following links to read about the steps for the client of your interest:

SSHing via a Windows machine

  • To make sure your PC has SSH client and server both installed, run the following command on Command Prompt as Administrator.

    Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
    Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
  • Now ssh into the remote server

    Using SSH keys
    ssh -i C:\path\to\privatekey user@host_address

    C:\path\to\privatekey1 • user2 • host_address3

    Using Password
    ssh user@host_address

    user2 • host_address3

SSHing via a Linux or MacOS machine

  • You can ssh via the following methods

    Using SSH keys
    ssh -i path/to/privatekey user@host_address

    path/to/privatekey1 • user2 • host_address3

    Note

    The afore-mentioned method has a long command to type in, the recommended method will not be an alias but utilize something which SSH itself provides us.
    We can add the config details to the ssh config file (~/.ssh/config). The format is given below, replace the content inside <_> with your values and save it into the config file.
    Then you can ssh directly by using ssh MyAzure.

     Host <MyAzure>
        HostName <Host Address>
        User <Username>
        IdentityFile </path/to/privatekey>
        IdentitiesOnly yes

    Note make sure you have set the correct permissions on the private key or else you will get an error during ssh. You can fix this by changing the permissions using the following command:

    chmod 400 path/to/privatekey

    path/to/privatekey1

    Using Password
    ssh user@host_address

    user2 • host_address3

Step 4: Setup OpenVPN Access Server

After we have ssh'ed into the machine, we have to set up the OpenVPN Access Server.

  • Before that, it's a good practice to update and upgrade your system via
    sudo apt update
    sudo apt upgrade
  • Execute the following command
    wget https://git.io/vpn -O openvpn-install.sh && sudo bash openvpn-install.sh
    It will download and execute a script that automates OpenVPN server configuration.
  • Keep in mind to update the following options during the setup process & leave the rest in their default state:
    • IP address: Your Public IP for the azure machine.
    • UDP or TCP: Enter 2 for TCP as UDP ports are blocked on campus network.
    • PORT: 443
    • DNS RESOLVER: Enter 4 for OpenDNS.
    • CLIENT: One configuration for one client/device. Name it like pc, mobile, etc.
  • The .ovpn file will be stored inside /root directory, copy it into your user's home directory using the following command
    sudo cp /root/client_name.ovpn ~/

    client_name4

Note Run the same script to generate new clients (you will need a unique client for each device that’s going to be connected to the VPN), i.e., one .ovpn file one connection.

Configuration for Gaming

Use the TCP_NODELAY option if you are planning to use this VPN for gaming. Execute the following command on the remote VPN server

sudo echo "tcp-nodelay" | sudo tee -a /etc/openvpn/server.conf

Now restart the OpenVPN service using

sudo systemctl restart openvpn.service && sudo systemctl restart openvpn-server@server.service

Step 5: Download ovpn files

Now we have to transfer the .ovpn files generated on the remote server to our local machine. The steps to achieve this are different for *nix (Linux or MacOS) & Windows, refer to the following links to read about the steps for your platform of interest:

Windows

  • Download WinSCP a GUI implementation for scp (secure copy) on windows. Open it.

  • Click on New Session
    image

  • Now refer to the procedure mentioned below based on your authentication method.

    Using SSH keys
    • Download PuTTY for converting the .pem file to .ppk via PuTTYgen. Open it.

      • From the Start menu, choose All Programs > PuTTY > PuTTYgen.

      • Choose Load.
        image

      • By default, PuTTYgen displays only files with the extension .ppk. To locate your .pem file, choose the option to display files of all types.
        image

      • Select your .pem file for the key pair that you specified when you launched your instance and choose Open. PuTTYgen displays a notice that the .pem file was successfully imported. Choose OK.

        image

      • To save the key in the format that PuTTY can use, choose Save private key.
        image

      • PuTTYgen displays a warning about saving the key without a passphrase. Choose Yes.
        image

    • Now head back to the WinSCP window & enter the following login configuration

      • File Protocol: SCP.
      • Host Name: Your remote machine's Public IP address.
      • Port: 22 (Default).
      • Username: username which you set for the remote machine.
        image
    • Navigate to Advanced > Authentication

    • Browse and select your .ppk file.
      image

    • Press OK then Login & finally YES.

    Using Password
    • Enter the following login configuration

      • File Protocol: SCP.
      • Host Name: Your remote machine's Public IP address.
      • Port: 22 (Default).
      • Username: Username which you set for the remote machine.
      • Password: Password which you set for the remote machine
        image
    • Press Login then YES.

  • Select and download all the .ovpn files you created which will be shown on the interface.
    image

  • Now shut down the WinSCP session

Linux & MacOS

Using SSH keys

Run the following command, the key will be downloaded in the Downloads directory.

scp -i path/to/privatekey user@host_address:client_name.ovpn ~/Downloads/

path/to/privatekey1 • user2 • host_address3 • client_name4

You can skip the -i (identity file) parameter if you have added the ssh config earlier.

Using Password

Run the following command, the key will be downloaded in the Downloads directory after you enter the correct password set by you earlier.

scp user@host_address:client_name.ovpn ~/Downloads/

user2 • host_address3 • client_name4

To start/stop/check status of the OpenVPN server using systemctl:

sudo systemctl start/stop/status openvpn@server.service

Android: Follow either of the aforementioned methods and then transfer the downloaded .ovpn file to your Android device via Telegram/Bluetooth/Mail or whatever to your android device.

Step 6: Connecting to the VPN on client devices

  • Android: Download Open VPN Connect app from Play Store. Open the app and after going through the first screen, go to Files tab, there import the .ovpn file, and connect.

  • Linux: In most of the distros, you can go to the network manager and import the .ovpn file. If not then install OpenVPN with sudo apt install openvpn and connect using sudo openvpn --config /path/to/config.ovpn.

  • MacOS: You can either download the GUI tool tunnelblick for importing the .ovpn file or download the CLI tool for openvpn via MacPorts or HomeBrew using sudo ports install openvpn and brew install openvpn respectively; then execute sudo openvpn --config /path/to/config.ovpn.

  • Windows: Download the official OpenVPN Connect client for Windows, import the .ovpn file, and toggle it ON to finally connect - video guide.

Step 7: Budget Control

Warning This is a very important step, to ensure the long-term usability of your credits

  • Use only one instance.
  • Bandwidth is free up to $100 credits, so it's better not to waste resources on the VPN.

Note If in any case, you have to stop an instance forcibly, do it; to be on the safer side.


Authors

Created with lots of </> and ♥

Footnotes

  1. It is the path to the .pem file which you downloaded just before deploying the VM. 2 3 4

  2. Name of the user given while creating the virtual machine. 2 3 4 5 6

  3. Public IP address of the machine. 2 3 4 5 6

  4. Name of the client you specified in the script. 2 3

@lukeymyuan
Copy link

Nice work mate, can you also add networking inbound port rules?

@nespar7
Copy link

nespar7 commented Mar 30, 2024

Hey there, the command sudo systemctl restart openvpn@server.service in step 4 fails because the filename in the /etc/systemd/system/multi-user.target.wants directory is changed to openvpn-server@server.service.

So this should be changed to sudo systemctl restart openvpn-server@server.service. Thanks for the tutorial

@CxdeBits
Copy link

Will this work decently to access blocked content in China?

@proffapt
Copy link
Author

Will this work decently to access blocked content in China?
@CxdeBits

Yes, the method is independent of country and restriction level - if the server is reachable and the Chinese restrictions doesn't apply on that server - then this will work.

@proffapt
Copy link
Author

Hey there, the command sudo systemctl restart openvpn@server.service in step 4 fails because the filename in the /etc/systemd/system/multi-user.target.wants directory is changed to openvpn-server@server.service.

So this should be changed to sudo systemctl restart openvpn-server@server.service. Thanks for the tutorial

Hi @nespar7,

I have updated the gist, thanks for the info!

@proffapt
Copy link
Author

Nice work mate, can you also add networking inbound port rules?

Hi @lukeymyuan,

As far as I remember the default configuration works just fine, please correct me if things have changed.

@proffapt
Copy link
Author

Also the article is also available and updated at: https://proffapt.hashnode.dev/openvpn-server-on-azure

@CxdeBits
Copy link

CxdeBits commented Apr 26, 2024

@proffapt I followed all steps exactly as described, however when connecting through Android OpenVPN connect app I get back:
Server poll timeout, trying next remote entry... Any tips? Only difference is that I selected UDP except for TCP. Is this really the issue?

@000795435
Copy link

@proffapt I followed all steps exactly as described, however when connecting through Android OpenVPN connect app I get back: Server poll timeout, trying next remote entry... Any tips? Only difference is that I selected UDP except for TCP. Is this really the issue?

I have figured it out, that is because the firewall blocks the inbound requests. Here is the solution:

Screenshot 2024-05-01 095324

On the Azure Virtual Machine page, go to "Network settings" under "Networking", you'll see the panel called "Network Security Group" which is for configuring the firewall. I assume you've set port 443 and protocol UDP for the VPN, so click the Inbound port rule which has port "443" and change to protocol from "TCP" to "UDP" and restart the server. If it doesn't work, then you should run the openVPN script again for selecting another available port number (like 1194), and don't forget register the new port number with its protocol type on the "Network Security Group" panel by clicking "Create port rule" then "Inbound port rule".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment