How to Setup L2TP VPN Server on VPS | Step-by-Step Tutorial

Learn how to easily configure an L2TP VPN server on your VPS with our comprehensive guide. Follow simple steps for secure remote access.

Updated: 04 Dec, 23 by Lisa P 12 Min

List of content you will read in this article:

In today's digital age, safeguarding your online presence has never been more critical. The use of Virtual Private Networks (VPNs) has gained immense popularity due to their ability to provide enhanced privacy, security, and remote access capabilities. Setting up an L2TP (Layer Two Tunneling Protocol) VPN server on a Virtual Private Server (VPS) can empower individuals with a secure and private means of accessing the internet and sensitive data. In this guide, we will walk you through the process of configuring your very own L2TP VPN server on a VPS, catering to a wide range of users, from those looking to bolster their online security to system administrators and IT professionals seeking to offer VPN services.

Before continuing, Understanding VPS vs VPN: Which Suits You Best

L2TP, or Layer two Tunneling Protocol, is a widely used VPN (Virtual Private Network) protocol that facilitates secure and encrypted internet connections. At its core, L2TP establishes a tunnel between the user's device and the VPN server, creating a secure conduit for data transmission. Within this tunnel, L2TP encapsulates data packets, adding a layer of security that shields the original data from potential eavesdroppers. Learn How VPNs Work.

While L2TP itself doesn't provide encryption, it is commonly combined with IPsec (Internet Protocol Security) to fortify security. IPsec supplements L2TP by introducing encryption and authentication measures, ensuring that data remains confidential and tamper-resistant during transit.

L2TP's advantages include its strong security features and compatibility with various operating systems and devices, such as Windows, macOS, iOS, Android, and Linux. Its reliability and ability to maintain connections across different networks, like Wi-Fi and mobile data, make it a dependable choice. Moreover, L2TP's cross-platform compatibility allows users to securely connect from different devices to a centralized VPN server, reinforcing online privacy and security across their digital interactions. 

Find out which is better: OpenVPN or L2TP for your network needs.

Before embarking on the journey of setting up your L2TP VPN server on a Virtual Private Server (VPS), it's essential to ensure you have the following prerequisites in place:

  • Virtual Private Server (VPS): First and foremost, you'll need access to a VPS. Ensure that your chosen VPS provider offers the necessary resources and flexibility to host your VPN server. The VPS should run a compatible operating system for VPN setup.

Linux  VPS
Need Affordable VPS Hosting

Starting From $5.99/Monthly

🏅 Fast Delivery
🏅 25+ Data Centers

  • Compatible OS: Confirm that your VPS operates on a compatible operating system, typically Linux distro such as Ubuntu, Debian, CentOS, or others that support L2TP VPN configuration. You may also need root or administrative access to install and configure software.
  • SSH Access: Secure Shell (SSH) access to your VPS is essential for remotely connecting to the server's command line interface. SSH ensures secure communication with your VPS, allowing you to execute commands and manage configurations.
  • Basic Linux Knowledge: While you don't need to be a Linux expert, having basic familiarity with Linux commands and file structures is beneficial. This knowledge will enable you to navigate the VPS environment, edit configuration files, and troubleshoot common issues that may arise during setup.

Here are step-by-step instructions for installing and configuring the L2TP VPN server, along with essential terminal commands and configuration files:

  • Access Your VPS: Log in to your VPS using SSH. Open a terminal window and connect to your VPS by entering:

ssh username@VPS_IP_Address

  • Update and Upgrade: Ensure your server is up to date by running these commands:

sudo apt update sudo apt upgrade

Install L2TP VPN Server: Install the required packages for the L2TP VPN server:

sudo apt install xl2tpd strongswan

  • Configure L2TP: Edit the configuration file for L2TP:

sudo nano /etc/xl2tpd/xl2tpd.conf

Add the following lines to the file:

 [global] ipsec saref = yes [lns default] ip range = 10.10.10.2-10.10.10.255 local ip = 10.10.10.1 require chap = yes refuse pap = yes require authentication = yes ppp debug = yes pppoptfile = /etc/ppp/options.xl2tpd length bit = yes

  • Create PPP Options File:

sudo nano /etc/ppp/options.xl2tpd

Add the following lines:

refuse-pap refuse-chap refuse-mschap require-mschap-v2 require-mppe-128 noccp noauth persist maxfail 0 debug dump

  • Configure IPsec: Edit the IPsec configuration file:

sudo nano /etc/ipsec.conf

Add these lines:

config setup uniqueids=never conn %default ikelifetime=60m keylife=20m rekeymargin=3m keyingtries=1 authby=secret keyexchange=ike ike=aes256-sha1-modp1024,aes128-sha1-modp1024! esp=aes256-sha1-modp1024,aes128-sha1-modp1024! conn L2TP-PSK-noNAT authby=secret pfs=no auto=add rekey=no ikelifetime=8h keylife=1h type=transport left=%any leftprotoport=17/1701 right=%any rightdns=8.8.8.8,8.8.4.4 rightprotoport=17/%any

  • Add Pre-Shared Key: Create the pre-shared key file and add your chosen key:

sudo nano /etc/ipsec.secrets

Add your pre-shared key in the format: your_vps_ip : PSK "your_secret_key"

  • Restart Services: Restart the VPN services to apply the changes:

sudo service strongswan restart sudo service xl2tpd restart

  • Open Firewall Ports: Ensure UDP ports 500, 4500, and 1701 are open on your VPS firewall.
  • User Configuration: Create usernames and passwords for VPN users, and assign them local IP addresses in /etc/ppp/chap-secrets.
  • Test Connection: Finally, test the VPN connection from a client device using the provided username and password.

Below, we'll walk you through the setup process on various platforms, including Windows, macOS, iOS, and Android.

Windows:

  1. Open Network Settings: Navigate to "Settings" > "Network & Internet" > "VPN."
  2. Add a VPN Connection: Click on "Add a VPN connection."
  3. Configure VPN Settings:
  • VPN Provider: Select "Windows (built-in)."
  • Connection Name: Give your VPN connection a name.
  • Server Name or Address: Enter the IP address of your VPS.
  • VPN Type: Choose "L2TP/IPsec with pre-shared key."
  • Pre-shared Key: Enter the pre-shared key you set during server configuration.
  • Username and Password: Enter the credentials you created for VPN access.
  1. Save and Connect: Click "Save" to save the configuration. To connect, click on the VPN connection you created and click "Connect."

macOS:

  1. Open System Preferences: Go to "System Preferences" > "Network."
  2. Add a VPN Connection: Click the "+" button to add a new network connection.
  3. Configure VPN Settings:
  • Interface: Choose "VPN."
  • VPN Type: Select "L2TP over IPSec."
  • Service Name: Enter a name for your VPN connection.
  • Configuration: Choose "Default."
  • Server Address: Enter the IP address of your VPS.
  • Account Name: Enter your username.
  • Authentication Settings: Click "Authentication Settings" and enter your password and pre-shared key.
  1. Save Configuration: Click "Apply" to save the configuration.
  2. Connect: Click "Connect" to establish the VPN connection.

iOS (iPhone/iPad):

  1. Open Settings: Go to "Settings" > "General" > "VPN."
  2. Add VPN Configuration:
  • Tap "Add VPN Configuration."
  • Select "Type" and choose "L2TP."
  • Enter a description for your VPN.
  • Server: Enter the IP address of your VPS.
  • Account: Enter your username.
  • Password: Enter your password.
  • Secret: Enter the pre-shared key.
  1. Save Configuration: Tap "Done" to save the configuration.
  2. Connect: Toggle the VPN switch to connect to your VPN server.

Android:

  1. Open Settings: Go to "Settings" > "Network & Internet" > "VPN."
  2. Add a VPN Connection: Tap the "+" or "Add VPN" button.
  3. Configure VPN Settings:
  • Name: Give your VPN connection a name.
  • Type: Choose "L2TP/IPsec PSK."
  • Server Address: Enter the IP address of your VPS.
  • L2TP Secret: Enter the pre-shared key.
  • IPsec Pre-shared Key: Enter the pre-shared key.
  • Username and Password: Enter your VPN credentials.
  1. Save Configuration: Tap "Save" to save the VPS VPN configuration.
  2. Connect: Tap the newly created VPN profile to connect.

After completing the L2TP VPN client setup on a VPS, it's essential to test the connection to ensure it's functioning correctly. Here's how to do it and some troubleshooting tips for common issues:

  1. Client Device: Ensure that your client device (e.g., computer or smartphone) is connected to the internet.
  2. Connect to VPN: On your client device, open the VPN settings and select the VPN connection you created earlier. Click "Connect" or toggle the VPN switch.
  3. Authentication: Enter the username and password you created during the server setup when prompted. For enhanced security, consider using multi-factor authentication if available.
  4. Connect: Click "Connect" or equivalent on your client device. The device will attempt to establish a connection to the VPN server.
  5. Verification: Once connected, your client device should display a notification or indicator confirming the VPN connection. Additionally, you can check your external IP address; it should match the IP address of your VPS, indicating a successful connection.

Troubleshooting Tips:

  1. Authentication Failures: If you encounter authentication errors, double-check the username and password. Ensure they match the credentials you configured on the server.
  2. Incorrect Pre-Shared Key: If using L2TP/IPsec with a pre-shared key, verify that the key is correctly entered on both the server and client sides.
  3. Firewall Issues: If the connection fails, your VPS firewall may be blocking VPN traffic. Ensure that UDP ports 500, 4500, and 1701 are open.
  4. IP Address Conflicts: Check for IP address conflicts. Ensure that the local IP addresses assigned to clients do not overlap.
  5. Log Analysis: Review server logs for error messages or connection attempts. Logs can provide insights into the cause of connection issues.

To ensure top-notch security and performance for your L2TP VPN server on a VPS, follow these concise guidelines:

  1. Regular Updates: Keep server and client software up to date to safeguard against vulnerabilities. Promptly apply security patches and updates to maintain a secure environment.
  2. Strong Authentication: Use multi-factor authentication (MFA) and certificates for enhanced security. MFA adds extra layers of verification, while certificates bolster user authentication.
  3. Firewall Rules: Configure firewall rules on your VPS to filter incoming and outgoing traffic, allowing only essential data to reach your VPN server. Block unnecessary ports and services to minimize exposure to threats.
  4. Log Monitoring: Set up log monitoring and alerting to detect unusual activities, security breaches, or performance issues. Regularly analyze logs to promptly address any concerns.
  5. Robust Encryption: Prioritize strong encryption protocols for data transmission. Configure your VPN to utilize robust encryption methods, such as L2TP/IPsec with strong pre-shared keys or certificates, to ensure data security.

In conclusion, setting up an L2TP VPN server on a VPS can be a game-changer for your online security and privacy. By following the steps outlined in this guide, you've gained the knowledge and tools to create your own secure tunnel to the internet. With your own VPN server, you can browse the web with confidence, access geo-restricted content, and ensure your data remains private in an increasingly connected world.

 

Lisa P

Lisa P

Hello, everyone, my name is Lisa. I'm a passionate electrical engineering student with a keen interest in technology. I'm fascinated by the intersection of engineering principles and technological advancements, and I'm eager to contribute to the field by applying my knowledge and skills to solve real-world problems.