Convert Figma logo to code with AI

hwdsl2 logodocker-ipsec-vpn-server

Docker image to run an IPsec VPN server, with IPsec/L2TP, Cisco IPsec and IKEv2

6,362
1,378
6,362
0

Top Related Projects

πŸ”’ OpenVPN server in a Docker container complete with an EasyRSA PKI CA

28,868

Set up a personal VPN in the cloud

23,178

Streisand sets up a new server running your choice of WireGuard, OpenConnect, OpenSSH, OpenVPN, Shadowsocks, sslh, Stunnel, or a Tor bridge. It also generates custom instructions for all of these services. At the end of the run you are given an HTML file with instructions that can be shared with friends, family members, and fellow activists.

OpenVPN road warrior installer for Ubuntu, Debian, AlmaLinux, Rocky Linux, CentOS and Fedora

Quick Overview

The hwdsl2/docker-ipsec-vpn-server is a Docker image that sets up an IPsec VPN server using strongSwan and xl2tpd. It provides a secure and easy-to-deploy VPN solution that supports both IPsec/L2TP and Cisco IPsec protocols, making it compatible with various devices and operating systems.

Pros

  • Easy setup and deployment using Docker
  • Supports multiple VPN protocols (IPsec/L2TP and Cisco IPsec)
  • Regularly updated with security patches and improvements
  • Detailed documentation and troubleshooting guides

Cons

  • Limited customization options compared to manual VPN server setup
  • Potential performance overhead due to running in a Docker container
  • Requires basic understanding of Docker and networking concepts
  • May not be suitable for large-scale enterprise deployments

Getting Started

To set up the IPsec VPN server using Docker, follow these steps:

  1. Pull the Docker image:
docker pull hwdsl2/ipsec-vpn-server
  1. Create and run the Docker container:
docker run \
    --name ipsec-vpn-server \
    --env-file ./vpn.env \
    --restart=always \
    -p 500:500/udp \
    -p 4500:4500/udp \
    -v /lib/modules:/lib/modules:ro \
    -d --privileged \
    hwdsl2/ipsec-vpn-server
  1. Create a vpn.env file with your VPN credentials:
VPN_IPSEC_PSK=your_ipsec_pre_shared_key
VPN_USER=your_vpn_username
VPN_PASSWORD=your_vpn_password
  1. After the container starts, you can retrieve the VPN client configuration details:
docker logs ipsec-vpn-server

For more detailed instructions and advanced configuration options, refer to the project's GitHub repository.

Competitor Comparisons

πŸ”’ OpenVPN server in a Docker container complete with an EasyRSA PKI CA

Pros of docker-openvpn

  • Uses OpenVPN protocol, which is generally faster and more flexible than IPsec
  • Supports more advanced features like split tunneling and custom DNS settings
  • Has a larger community and more extensive documentation

Cons of docker-openvpn

  • Requires more complex client-side configuration
  • May be blocked more easily by firewalls in some countries
  • Setup process can be more involved for beginners

Code Comparison

docker-ipsec-vpn-server:

docker run \
    --name ipsec-vpn-server \
    --env-file ./vpn.env \
    --restart=always \
    -p 500:500/udp \
    -p 4500:4500/udp \
    -v /lib/modules:/lib/modules:ro \
    -d --privileged \
    hwdsl2/ipsec-vpn-server

docker-openvpn:

docker run -v $OVPN_DATA:/etc/openvpn --rm kylemanna/openvpn ovpn_genconfig -u udp://VPN.SERVERNAME.COM
docker run -v $OVPN_DATA:/etc/openvpn --rm -it kylemanna/openvpn ovpn_initpki
docker run -v $OVPN_DATA:/etc/openvpn -d -p 1194:1194/udp --cap-add=NET_ADMIN kylemanna/openvpn

The docker-ipsec-vpn-server setup is simpler, requiring fewer commands and less configuration. However, docker-openvpn offers more flexibility and customization options at the cost of a more complex setup process.

28,868

Set up a personal VPN in the cloud

Pros of algo

  • Supports multiple VPN protocols (WireGuard, IPsec/IKEv2)
  • Automated setup process for various cloud providers
  • Enhanced security features and regular updates

Cons of algo

  • More complex setup compared to docker-ipsec-vpn-server
  • Requires more system resources
  • Less flexibility for customization

Code Comparison

algo:

- name: Install WireGuard
  apt:
    name:
      - wireguard
      - wireguard-tools
    state: present

docker-ipsec-vpn-server:

FROM alpine:3.14

ENV SWAN_VER 4.1
ENV LIBRESWAN_URL https://github.com/libreswan/libreswan/archive/v${SWAN_VER}.tar.gz

RUN apk add --no-cache libcap-ng libcurl libevent linux-pam nss nspr

The algo project uses Ansible playbooks for setup, while docker-ipsec-vpn-server uses a Dockerfile for containerization. algo's code snippet shows the installation of WireGuard, while docker-ipsec-vpn-server's code focuses on setting up the base image and dependencies for IPsec.

Both projects aim to simplify VPN setup, but algo offers a broader range of protocols and cloud provider support. docker-ipsec-vpn-server, on the other hand, provides a more straightforward Docker-based solution focused solely on IPsec. The choice between the two depends on specific requirements, such as protocol preferences, deployment environment, and desired level of customization.

23,178

Streisand sets up a new server running your choice of WireGuard, OpenConnect, OpenSSH, OpenVPN, Shadowsocks, sslh, Stunnel, or a Tor bridge. It also generates custom instructions for all of these services. At the end of the run you are given an HTML file with instructions that can be shared with friends, family members, and fellow activists.

Pros of Streisand

  • Supports multiple VPN protocols (OpenVPN, WireGuard, OpenConnect, etc.)
  • Automates the setup of various privacy-enhancing services
  • Provides a user-friendly interface for managing and distributing credentials

Cons of Streisand

  • More complex setup process compared to docker-ipsec-vpn-server
  • Requires more server resources due to multiple services
  • Less frequently updated than docker-ipsec-vpn-server

Code Comparison

Streisand (Ansible playbook excerpt):

- name: Install OpenVPN
  apt:
    name: openvpn
    state: present

- name: Configure OpenVPN server
  template:
    src: openvpn-server.conf.j2
    dest: /etc/openvpn/server.conf

docker-ipsec-vpn-server (Docker Compose excerpt):

version: '3'
services:
  ipsec-vpn-server:
    image: hwdsl2/ipsec-vpn-server
    ports:
      - "500:500/udp"
      - "4500:4500/udp"
    environment:
      - VPN_IPSEC_PSK=your_psk
      - VPN_USER=your_username
      - VPN_PASSWORD=your_password

The code comparison shows that Streisand uses Ansible for configuration management, while docker-ipsec-vpn-server utilizes Docker Compose for easy deployment. Streisand's approach allows for more granular control over the installation process, while docker-ipsec-vpn-server offers a simpler, containerized solution.

OpenVPN road warrior installer for Ubuntu, Debian, AlmaLinux, Rocky Linux, CentOS and Fedora

Pros of openvpn-install

  • Simpler setup process with a single script installation
  • Supports a wider range of Linux distributions
  • Easier to manage and add/remove clients

Cons of openvpn-install

  • Less secure than IPsec by default (though can be configured for higher security)
  • May require additional firewall configuration
  • Potentially slower performance compared to IPsec

Code Comparison

openvpn-install:

#!/bin/bash
# OpenVPN installer for Debian, Ubuntu, and CentOS

# Detect Debian users running the script with "sh" instead of bash
if readlink /proc/$$/exe | grep -q "dash"; then
    echo 'This installer needs to be run with "bash", not "sh".'
    exit
fi

docker-ipsec-vpn-server:

FROM alpine:3.14

ENV SWAN_VER 4.6
ENV GLIBC_VER 2.33-r0

RUN apk add --no-cache libcap-ng libcrypto1.1 libcurl libldns libssl1.1 \
    musl nspr nss nss-tools xmlsec-nss

The openvpn-install script is a bash script that performs checks and installations directly on the host system. In contrast, docker-ipsec-vpn-server uses a Dockerfile to create a containerized environment for the VPN server, which provides better isolation and easier deployment across different systems.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

English | À¸­æ–‡

IPsec VPN Server on Docker

Build Status GitHub Stars Docker Stars Docker Pulls

Docker image to run an IPsec VPN server, with IPsec/L2TP, Cisco IPsec and IKEv2.

Based on Alpine 3.20 or Debian 12 with Libreswan (IPsec VPN software) and xl2tpd (L2TP daemon).

An IPsec VPN encrypts your network traffic, so that nobody between you and the VPN server can eavesdrop on your data as it travels via the Internet. This is especially useful when using unsecured networks, e.g. at coffee shops, airports or hotel rooms.

Β» :book: Book: Build Your Own VPN Server: A Step by Step Guide

Quick start

Use this command to set up an IPsec VPN server on Docker:

docker run \
    --name ipsec-vpn-server \
    --restart=always \
    -v ikev2-vpn-data:/etc/ipsec.d \
    -v /lib/modules:/lib/modules:ro \
    -p 500:500/udp \
    -p 4500:4500/udp \
    -d --privileged \
    hwdsl2/ipsec-vpn-server

Your VPN login details will be randomly generated. See Retrieve VPN login details.

Alternatively, you may set up IPsec VPN without Docker. To learn more about how to use this image, read the sections below.

Features

  • Supports IKEv2 with strong and fast ciphers (e.g. AES-GCM)
  • Generates VPN profiles to auto-configure iOS, macOS and Android devices
  • Supports Windows, macOS, iOS, Android, Chrome OS and Linux as VPN clients
  • Includes a helper script to manage IKEv2 users and certificates

Install Docker

First, install Docker on your Linux server. You may also use Podman to run this image, after creating an alias for docker.

Advanced users can use this image on macOS with Docker for Mac. Before using IPsec/L2TP mode, you may need to restart the Docker container once with docker restart ipsec-vpn-server. This image does not support Docker for Windows.

Download

Get the trusted build from the Docker Hub registry:

docker pull hwdsl2/ipsec-vpn-server

Alternatively, you may download from Quay.io:

docker pull quay.io/hwdsl2/ipsec-vpn-server
docker image tag quay.io/hwdsl2/ipsec-vpn-server hwdsl2/ipsec-vpn-server

Supported platforms: linux/amd64, linux/arm64 and linux/arm/v7.

Advanced users can build from source code on GitHub.

Image comparison

Two pre-built images are available. The default Alpine-based image is only ~18 MB.

Alpine-basedDebian-based
Image namehwdsl2/ipsec-vpn-serverhwdsl2/ipsec-vpn-server:debian
Compressed size~ 18 MB~ 63 MB
Base imageAlpine Linux 3.20Debian Linux 12
Platformsamd64, arm64, arm/v7amd64, arm64, arm/v7
Libreswan version5.05.0
IPsec/L2TPΓ’ΒœΒ…Γ’ΒœΒ…
Cisco IPsecΓ’ΒœΒ…Γ’ΒœΒ…
IKEv2Γ’ΒœΒ…Γ’ΒœΒ…

Note: To use the Debian-based image, replace every hwdsl2/ipsec-vpn-server with hwdsl2/ipsec-vpn-server:debian in this README. These images are not currently compatible with Synology NAS systems.

I want to use the older Libreswan version 4.

It is generally recommended to use the latest Libreswan version 5, which is the default version in this project. However, if you want to use the older Libreswan version 4, you can build the Docker image from source code:

git clone https://github.com/hwdsl2/docker-ipsec-vpn-server
cd docker-ipsec-vpn-server
# Specify Libreswan version 4
sed -i 's/SWAN_VER 5\.0/SWAN_VER 4.15/' Dockerfile Dockerfile.debian
# To build Alpine-based image
docker build -t hwdsl2/ipsec-vpn-server .
# To build Debian-based image
docker build -f Dockerfile.debian -t hwdsl2/ipsec-vpn-server:debian .

How to use this image

Environment variables

Note: All the variables to this image are optional, which means you don't have to type in any variable, and you can have an IPsec VPN server out of the box! To do that, create an empty env file using touch vpn.env, and skip to the next section.

This Docker image uses the following variables, that can be declared in an env file (see example):

VPN_IPSEC_PSK=your_ipsec_pre_shared_key
VPN_USER=your_vpn_username
VPN_PASSWORD=your_vpn_password

This will create a user account for VPN login, which can be used by your multiple devices*. The IPsec PSK (pre-shared key) is specified by the VPN_IPSEC_PSK environment variable. The VPN username is defined in VPN_USER, and VPN password is specified by VPN_PASSWORD.

Additional VPN users are supported, and can be optionally declared in your env file like this. Usernames and passwords must be separated by spaces, and usernames cannot contain duplicates. All VPN users will share the same IPsec PSK.

VPN_ADDL_USERS=additional_username_1 additional_username_2
VPN_ADDL_PASSWORDS=additional_password_1 additional_password_2

Note: In your env file, DO NOT put "" or '' around values, or add space around =. DO NOT use these special characters within values: \ " '. A secure IPsec PSK should consist of at least 20 random characters.

Note: If you modify the env file after the Docker container is already created, you must remove and re-create the container for the changes to take effect. Refer to Update Docker image.

Additional environment variables

Advanced users can optionally specify a DNS name, client name and/or custom DNS servers.

Learn how to specify a DNS name, client name and/or custom DNS servers.

Advanced users can optionally specify a DNS name for the IKEv2 server address. The DNS name must be a fully qualified domain name (FQDN). Example:

VPN_DNS_NAME=vpn.example.com

You may specify a name for the first IKEv2 client. Use one word only, no special characters except - and _. The default is vpnclient if not specified.

VPN_CLIENT_NAME=your_client_name

By default, clients are set to use Google Public DNS when the VPN is active. You may specify custom DNS server(s) for all VPN modes. Example:

VPN_DNS_SRV1=1.1.1.1
VPN_DNS_SRV2=1.0.0.1

For more details and a list of some popular public DNS providers, see Use alternative DNS servers.

By default, no password is required when importing IKEv2 client configuration. You can choose to protect client config files using a random password.

VPN_PROTECT_CONFIG=yes

Note: The variables above have no effect for IKEv2 mode, if IKEv2 is already set up in the Docker container. In this case, you may remove IKEv2 and set it up again using custom options. Refer to Configure and use IKEv2 VPN.

Start the IPsec VPN server

Create a new Docker container from this image (replace ./vpn.env with your own env file):

docker run \
    --name ipsec-vpn-server \
    --env-file ./vpn.env \
    --restart=always \
    -v ikev2-vpn-data:/etc/ipsec.d \
    -v /lib/modules:/lib/modules:ro \
    -p 500:500/udp \
    -p 4500:4500/udp \
    -d --privileged \
    hwdsl2/ipsec-vpn-server

In this command, we use the -v option of docker run to create a new Docker volume named ikev2-vpn-data, and mount it into /etc/ipsec.d in the container. IKEv2 related data such as certificates and keys will persist in the volume, and later when you need to re-create the Docker container, just specify the same volume again.

It is recommended to enable IKEv2 when using this image. However, if you prefer not to enable IKEv2 and use only the IPsec/L2TP and IPsec/XAuth ("Cisco IPsec") modes to connect to the VPN, remove the first -v option from the docker run command above.

Note: Advanced users can also run without privileged mode.

Retrieve VPN login details

If you did not specify an env file in the docker run command above, VPN_USER will default to vpnuser and both VPN_IPSEC_PSK and VPN_PASSWORD will be randomly generated. To retrieve them, view the container logs:

docker logs ipsec-vpn-server

Search for these lines in the output:

Connect to your new VPN with these details:

Server IP: your_vpn_server_ip
IPsec PSK: your_ipsec_pre_shared_key
Username: your_vpn_username
Password: your_vpn_password

The output will also include details for IKEv2 mode, if enabled.

(Optional) Backup the generated VPN login details (if any) to the current directory:

docker cp ipsec-vpn-server:/etc/ipsec.d/vpn-gen.env ./

Next steps

Read this in other languages: English, À¸­æ–‡.

Get your computer or device to use the VPN. Please refer to:

Configure and use IKEv2 VPN (recommended)

Configure IPsec/L2TP VPN Clients

Configure IPsec/XAuth ("Cisco IPsec") VPN Clients

Read :book: VPN book to access extra content.

Enjoy your very own VPN! :sparkles::tada::rocket::sparkles:

Important notes

Windows users: For IPsec/L2TP mode, a one-time registry change is required if the VPN server or client is behind NAT (e.g. home router).

The same VPN account can be used by your multiple devices. However, due to an IPsec/L2TP limitation, if you wish to connect multiple devices from behind the same NAT (e.g. home router), you must use IKEv2 or IPsec/XAuth mode.

If you wish to add, edit or remove VPN user accounts, first update your env file, then you must remove and re-create the Docker container using instructions from the next section. Advanced users can bind mount the env file.

For servers with an external firewall (e.g. EC2/GCE), open UDP ports 500 and 4500 for the VPN. Aliyun users, see #433.

Clients are set to use Google Public DNS when the VPN is active. If another DNS provider is preferred, read this section.

Update Docker image

To update the Docker image and container, first download the latest version:

docker pull hwdsl2/ipsec-vpn-server

If the Docker image is already up to date, you should see:

Status: Image is up to date for hwdsl2/ipsec-vpn-server:latest

Otherwise, it will download the latest version. To update your Docker container, first write down all your VPN login details. Then remove the Docker container with docker rm -f ipsec-vpn-server. Finally, re-create it using instructions from How to use this image.

Configure and use IKEv2 VPN

IKEv2 mode has improvements over IPsec/L2TP and IPsec/XAuth ("Cisco IPsec"), and does not require an IPsec PSK, username or password. Read more here.

First, check container logs to view details for IKEv2:

docker logs ipsec-vpn-server

Note: If you cannot find IKEv2 details, IKEv2 may not be enabled in the container. Try updating the Docker image and container using instructions from the Update Docker image section.

During IKEv2 setup, an IKEv2 client (with default name vpnclient) is created, with its configuration exported to /etc/ipsec.d inside the container. To copy config file(s) to the Docker host:

# Check contents of /etc/ipsec.d in the container
docker exec -it ipsec-vpn-server ls -l /etc/ipsec.d
# Example: Copy a client config file from the container
# to the current directory on the Docker host
docker cp ipsec-vpn-server:/etc/ipsec.d/vpnclient.p12 ./

Next steps: Configure your devices to use the IKEv2 VPN.

Learn how to manage IKEv2 clients.

You can manage IKEv2 clients using the helper script. See examples below. To customize client options, run the script without arguments.

# Add a new client (using default options)
docker exec -it ipsec-vpn-server ikev2.sh --addclient [client name]
# Export configuration for an existing client
docker exec -it ipsec-vpn-server ikev2.sh --exportclient [client name]
# List existing clients
docker exec -it ipsec-vpn-server ikev2.sh --listclients
# Show usage
docker exec -it ipsec-vpn-server ikev2.sh -h

Note: If you encounter error "executable file not found", replace ikev2.sh above with /opt/src/ikev2.sh.

Learn how to change the IKEv2 server address.

In certain circumstances, you may need to change the IKEv2 server address. For example, to switch to use a DNS name, or after server IP changes. To change the IKEv2 server address, first open a bash shell inside the container, then follow these instructions. Note that the container logs will not show the new IKEv2 server address until you restart the Docker container.

Remove IKEv2 and set it up again using custom options.

In certain circumstances, you may need to remove IKEv2 and set it up again using custom options.

Warning: All IKEv2 configuration including certificates and keys will be permanently deleted. This cannot be undone!

Option 1: Remove IKEv2 and set it up again using the helper script.

Note that this will override variables you specified in the env file, such as VPN_DNS_NAME and VPN_CLIENT_NAME, and the container logs will no longer show up-to-date information for IKEv2.

# Remove IKEv2 and delete all IKEv2 configuration
docker exec -it ipsec-vpn-server ikev2.sh --removeikev2
# Set up IKEv2 again using custom options
docker exec -it ipsec-vpn-server ikev2.sh

Option 2: Remove ikev2-vpn-data and re-create the container.

  1. Write down all your VPN login details.
  2. Remove the Docker container: docker rm -f ipsec-vpn-server.
  3. Remove the ikev2-vpn-data volume: docker volume rm ikev2-vpn-data.
  4. Update your env file and add custom IKEv2 options such as VPN_DNS_NAME and VPN_CLIENT_NAME, then re-create the container. Refer to How to use this image.

Advanced usage

See Advanced usage.

Technical details

There are two services running: Libreswan (pluto) for the IPsec VPN, and xl2tpd for L2TP support.

The default IPsec configuration supports:

  • IPsec/L2TP with PSK
  • IKEv1 with PSK and XAuth ("Cisco IPsec")
  • IKEv2

The ports that are exposed for this container to work are:

  • 4500/udp and 500/udp for IPsec

License

Note: The software components inside the pre-built image (such as Libreswan and xl2tpd) are under the respective licenses chosen by their respective copyright holders. As for any pre-built image usage, it is the image user's responsibility to ensure that any use of this image complies with any relevant licenses for all software contained within.

Copyright (C) 2016-2024 Lin Song View my profile on LinkedIn
Based on the work of Thomas Sarlandie (Copyright 2012)

Creative Commons License
This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License
Attribution required: please include my name in any derivative and let me know how you have improved it!