Public | Automated Build

Last pushed: 10 days ago
Short Description
VPN server with IPsec/L2TP and Cisco IPsec
Full Description

IPsec VPN Server on Docker

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

Based on Debian Jessie with Libreswan (IPsec VPN software) and xl2tpd (L2TP daemon).

» See also: IPsec VPN Server on Ubuntu, Debian and CentOS

Read this in other languages: English, 简体中文.

Table of Contents

Install Docker

First, install and run Docker on your Linux server.


Get the trusted build from the Docker Hub registry:

docker pull hwdsl2/ipsec-vpn-server

Alternatively, you may build from source code on GitHub.

How to use this image

Environment variables

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


This will create a single user account for VPN login. 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.

Note: In your env file, DO NOT put "" or '' around values, or add space around =. DO NOT use these characters within values: \ " '.

All the variables to this image are optional, which means you don't have to type in any environment variable, and you can have an IPsec VPN server out of the box! Read the sections below for details.

Start the IPsec VPN server

Important: First, load the IPsec NETKEY kernel module on the Docker host:

sudo modprobe af_key

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 \
    -p 500:500/udp \
    -p 4500:4500/udp \
    -v /lib/modules:/lib/modules:ro \
    -d --privileged \

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

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

docker cp ipsec-vpn-server:/opt/src/vpn-gen.env ./

Check server status

To check the status of your IPsec VPN server, you can pass ipsec status to your container like this:

docker exec -it ipsec-vpn-server ipsec status

Or display current established VPN connections:

docker exec -it ipsec-vpn-server ipsec whack --trafficstatus

Next steps

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

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

If you get an error when trying to connect, see Troubleshooting.

Enjoy your very own VPN!

Important notes

Read this in other languages: English, 简体中文.

For Windows users, this one-time registry change is required if the VPN server and/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 simultaneously from behind the same NAT (e.g. home router), you must use only IPsec/XAuth mode. Also, your server must run the latest version of this Docker image.

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

Before editing any VPN config files, you must first start a Bash session in the running container.

If you wish to add, edit or remove VPN user accounts, see Manage VPN Users. Important: After editing the VPN config files, you must also comment out the relevant sections in /opt/src/, to avoid losing your changes on container restart.

Clients are set to use Google Public DNS when the VPN connection is active. If another DNS provider is preferred, replace and in /opt/src/ with the new servers. Then restart the Docker container.

Update Docker image

To update your Docker image and container:

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 (refer to "Retrieve VPN login details" above). Then remove the Docker container with docker rm -f ipsec-vpn-server. Finally, re-create it using instructions from the "How to use this image" section.

Advanced usage

Build from source code

Advanced users can download and compile the source code from GitHub:

git clone
cd docker-ipsec-vpn-server
docker build -t hwdsl2/ipsec-vpn-server .

Or use this if not modifying the source code:

docker build -t hwdsl2/ipsec-vpn-server

Bash shell inside container

To start a Bash session in the running container:

docker exec -it ipsec-vpn-server env TERM=xterm bash -l

(Optional) Install the nano editor:

apt-get update && apt-get -y install nano

When finished, exit the container and restart if needed:

docker restart ipsec-vpn-server

Technical details

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

The default IPsec configuration supports:

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

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

  • 4500/udp and 500/udp for IPsec

See also


Copyright (C) 2016-2017 Lin Song
Based on the work of Thomas Sarlandie (Copyright 2012)

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!

Docker Pull Command
Source Repository

Comments (14)
8 hours ago

Hello,I have a questions,when I connect the vpn at home,my network segment is 1(ipaddr is,I cant ssh and ping the company's server successful ,the server ipaddr is in the company.But when I use another router,the ipaddr is home),i can ping and ssh the company's server successful. Could you please help me fix it?

2 months ago

Thanks. Setup is a piece of cake and works beautifully with Stock Android VPN Settings

4 months ago

你好,我按照步骤操作确实实现了IPsec PSK模式下的登陆,在Android,IOS上都实现了,但是Linux上配置感觉有点不大对,我用vpnc配置好参数后无法连接(环境:Ubuntu server 16.04),它一直询问我IPSec ID。

7 months ago

@erv2 For Windows users, the extra steps you mentioned are required before connecting. Please refer to the "Important notes" section [1] of the README.


7 months ago

it looks like windows requires a bit of extra setting to set up connection with this solution:

  1. you have to add the following reg to allow connecting to vpn server behind nat:
    For Windows Vista, 7, 8, 10, and 2008 Server:
    RegValue: AssumeUDPEncapsulationContextOnSendRule
    Type: DWORD
    Data Value: 2 the security setting of the VPN connection, you have to allow "CHAP" protocol.

is there a way to make windows experience more seamless?

7 months ago

Hi I am running into issue trying to connect windows 10 with the VPN hosting on EC2.

soon after i managed to connect to the server, the server sent a termination signal to my windows vpn client. here's the logs:

xl2tpd[1]: Call established with 70.30.79.xx, Local: 15051, Remote: 6140, Serial: 1
xl2tpd[1]: result_code_avp: result code endianness fix for buggy Apple client. network=768, le=3
xl2tpd[1]: control_finish: Connection closed to 70.30.79.xx, serial 1 ()
xl2tpd[1]: Terminating pppd: sending TERM signal to pid 715
xl2tpd[1]: result_code_avp: result code endianness fix for buggy Apple client. network=256, le=1
xl2tpd[1]: control_finish: Connection closed to 70.30.79.xx, port 56763 (), Local: 19248, Remote: 2

i have no problem connecting to it using my iphone.

7 months ago

@antioch You might have encountered a Docker/lxc bug [1]. Other than that, please make sure that you carefully follow all instructions in the README above.

I suggest that you remove and recreate the Docker container, and try again.

First, load the IPsec NETKEY kernel module on the Docker host:

sudo modprobe af_key

Then remove and recreate the Docker container:

docker rm -f ipsec-vpn-server
docker pull hwdsl2/ipsec-vpn-server
docker run ... (see full command in README above)


7 months ago

I installed this docker to the default Ubuntu/Docker image on Azure and encountered some issues.

Firstly, after starting the container and checking the logs I saw:
"modprobe: ERROR: ../libkmod/libkmod.c:557 kmod_search_moddep() could not open moddep file '/lib/modules/4.4.0-28-generic/modules.dep.bin'"
but it seems like the server is running when I check the status.

Secondly, I'm unable to connect to the server, my client gets a timeout. Perhaps the VPN server is actually not running/listening?

8 months ago

@h00283522 On Linode you need to use a distribution-provided kernel via GRUB instead of a Linode-provided kernel, where some modules may be missing. Please refer to [1] for detailed steps.


8 months ago

Great image!

I've got one error when I typed modprobe af_key, I get back "modprobe: ERROR: ../libkmod/libkmod.c:586 kmod_search_moddep() could not open moddep file '/lib/modules/4.7.0-x86_64-linode72/modules.dep.bin'
modprobe: FATAL: Module af_key not found in directory /lib/modules/4.7.0-x86_64-linode72"

Any ideas? Thanks!