Project Icon

docker-ipsec-vpn-server

Docker镜像快速部署IPsec VPN服务器

此Docker镜像可快速部署IPsec VPN服务器,支持IPsec/L2TP、Cisco IPsec和IKEv2协议。基于Alpine或Debian系统,使用Libreswan和xl2tpd。通过环境变量可自定义VPN设置,支持多用户和自定义DNS。提供IKEv2客户端管理脚本,便于配置使用。适合在不安全网络中加密流量,操作简便。

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,

项目侧边栏1项目侧边栏2
推荐项目
Project Cover

豆包MarsCode

豆包 MarsCode 是一款革命性的编程助手,通过AI技术提供代码补全、单测生成、代码解释和智能问答等功能,支持100+编程语言,与主流编辑器无缝集成,显著提升开发效率和代码质量。

Project Cover

AI写歌

Suno AI是一个革命性的AI音乐创作平台,能在短短30秒内帮助用户创作出一首完整的歌曲。无论是寻找创作灵感还是需要快速制作音乐,Suno AI都是音乐爱好者和专业人士的理想选择。

Project Cover

有言AI

有言平台提供一站式AIGC视频创作解决方案,通过智能技术简化视频制作流程。无论是企业宣传还是个人分享,有言都能帮助用户快速、轻松地制作出专业级别的视频内容。

Project Cover

Kimi

Kimi AI助手提供多语言对话支持,能够阅读和理解用户上传的文件内容,解析网页信息,并结合搜索结果为用户提供详尽的答案。无论是日常咨询还是专业问题,Kimi都能以友好、专业的方式提供帮助。

Project Cover

阿里绘蛙

绘蛙是阿里巴巴集团推出的革命性AI电商营销平台。利用尖端人工智能技术,为商家提供一键生成商品图和营销文案的服务,显著提升内容创作效率和营销效果。适用于淘宝、天猫等电商平台,让商品第一时间被种草。

Project Cover

吐司

探索Tensor.Art平台的独特AI模型,免费访问各种图像生成与AI训练工具,从Stable Diffusion等基础模型开始,轻松实现创新图像生成。体验前沿的AI技术,推动个人和企业的创新发展。

Project Cover

SubCat字幕猫

SubCat字幕猫APP是一款创新的视频播放器,它将改变您观看视频的方式!SubCat结合了先进的人工智能技术,为您提供即时视频字幕翻译,无论是本地视频还是网络流媒体,让您轻松享受各种语言的内容。

Project Cover

美间AI

美间AI创意设计平台,利用前沿AI技术,为设计师和营销人员提供一站式设计解决方案。从智能海报到3D效果图,再到文案生成,美间让创意设计更简单、更高效。

Project Cover

AIWritePaper论文写作

AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。

投诉举报邮箱: service@vectorlightyear.com
@2024 懂AI·鲁ICP备2024100362号-6·鲁公网安备37021002001498号