How to Set Up OpenVPN on EC2: A Step-by-Step Guide
In today's digital world, securing your connection is more important than ever. Whether you're accessing internal resources, setting up a private network, or simply ensuring privacy, OpenVPN provides a robust and trusted solution. In this tutorial, I'll walk you through setting up OpenVPN on an AWS EC2 instance. Follow the steps below to configure your VPN, and by the end, you'll have a secure connection ready for use!
Prerequisites
Before we dive into the installation process, ensure that you have:
An AWS account
An EC2 instance running Ubuntu (or other Linux distributions)
Access to your instance via SSH
Step 1: Launch an EC2 Instance
First, log in to your AWS Management Console and launch a new EC2 instance:
Choose AMI: Select an Ubuntu Server AMI (Amazon Machine Image).
Instance Type: Choose a suitable instance type (e.g., t2.micro for testing purposes).
Configure Security Group: Ensure you allow inbound traffic for OpenVPN (UDP port 1194) and SSH (port 22).
Launch the instance and connect to it using SSH.
ssh -i <your-key.pem> ubuntu@<your-ec2-instance-public-ip>
Once you’re connected, you're ready to set up OpenVPN.
Step 2: Install OpenVPN on EC2
Next, we’ll use a script to install and configure OpenVPN. The script automates the entire installation process and makes it very easy.
Download the OpenVPN installation script:
sudo curl -O https://raw.githubusercontent.com/angristan/openvpn-install/master/openvpn-install.sh
Make the script executable:
chmod +x openvpn-install.sh
Run the script to start the OpenVPN installation:
sudo ./openvpn-install.sh
The script will ask you a series of questions:
IP Address: The public IP address of your EC2 instance should be automatically detected.
Protocol: Choose UDP (default).
Port: Default OpenVPN port is 1194, which you can keep or change.
DNS Provider: Select a DNS provider (default options like Google or Cloudflare work fine).
Client Name: Enter a name for the VPN client (e.g.,
myclient
).
Once the installation is complete, the script will generate configuration files, including a .ovpn
file that contains the VPN client credentials.
Step 3: Download the OpenVPN Client Configuration
Now that the OpenVPN server is set up, you need to download the .ovpn
client configuration file from your EC2 instance to your local machine. This file will allow you to connect to the VPN using an OpenVPN client.
Exit the EC2 instance:
exit
Download the
.ovpn
file from your EC2 instance:scp -i <your-key.pem> ubuntu@<your-ec2-instance-public-ip>:~/myclient.ovpn .
You now have the client configuration file on your local machine.
Step 4: Install the OpenVPN Client
To connect to your new OpenVPN server, you'll need an OpenVPN client on your local machine. You can download the appropriate client for your operating system from the links below:
Windows: Download OpenVPN Client for Windows
Linux: Most Linux distributions come with OpenVPN pre-installed. If not, you can install it via the package manager:
sudo apt-get install openvpn
Step 5: Upload the Certificate and Connect to the VPN
Once you've installed the OpenVPN client, you'll need to upload the .ovpn
file to the client and connect to the VPN.
Windows/Mac: Open the OpenVPN client and import the
.ovpn
file.Linux: Use the command line to connect:
sudo openvpn --config myclient.ovpn
Once connected, your internet traffic will be routed through the OpenVPN server, ensuring a secure and encrypted connection.
Step 6: Verify Your Connection
To verify that your VPN is working, you can check your public IP address before and after connecting to the VPN. Go to whatismyip.com and see if your IP address changes after connecting to the VPN.
Conclusion
Congratulations! You've successfully set up OpenVPN on an AWS EC2 instance and connected to it using an OpenVPN client. This setup provides a secure, encrypted tunnel for all your online activities, ensuring privacy and protection from potential cyber threats.
If you have any questions or need further assistance, feel free to drop a comment below. Happy surfing!