1
0
mirror of synced 2024-11-24 22:06:07 +03:00
setup-ipsec-vpn/README.md

277 lines
14 KiB
Markdown
Raw Normal View History

2017-03-20 06:10:49 +03:00
# IPsec VPN Server Auto Setup Scripts
2016-04-17 10:20:02 +03:00
2022-01-31 04:35:57 +03:00
[![Build Status](https://img.shields.io/github/workflow/status/hwdsl2/setup-ipsec-vpn/vpn%20test.svg?cacheSeconds=3600)](https://github.com/hwdsl2/setup-ipsec-vpn/actions/workflows/main.yml) [![GitHub Stars](https://img.shields.io/github/stars/hwdsl2/setup-ipsec-vpn.svg?cacheSeconds=86400&logo=github)](https://github.com/hwdsl2/setup-ipsec-vpn/stargazers) [![Docker Stars](https://img.shields.io/docker/stars/hwdsl2/ipsec-vpn-server.svg?cacheSeconds=86400&logo=docker)](https://github.com/hwdsl2/docker-ipsec-vpn-server) [![Docker Pulls](https://img.shields.io/docker/pulls/hwdsl2/ipsec-vpn-server.svg?cacheSeconds=86400&logo=docker)](https://github.com/hwdsl2/docker-ipsec-vpn-server)
2015-08-19 00:14:42 +03:00
2022-01-16 08:43:15 +03:00
Set up your own IPsec VPN server in just a few minutes, with IPsec/L2TP, Cisco IPsec and IKEv2. All you need to do is provide your own VPN credentials, and let the scripts handle the rest.
2015-08-19 00:14:42 +03:00
2016-08-30 19:35:52 +03:00
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.
2021-06-05 01:27:21 +03:00
We will use [Libreswan](https://libreswan.org/) as the IPsec server, and [xl2tpd](https://github.com/xelerance/xl2tpd) as the L2TP provider.
2016-01-08 12:17:50 +03:00
2016-08-30 19:35:52 +03:00
*Read this in other languages: [English](README.md), [简体中文](README-zh.md).*
2016-05-29 22:38:31 +03:00
#### Table of Contents
2016-05-12 07:39:23 +03:00
2017-01-08 08:32:58 +03:00
- [Quick start](#quick-start)
2016-05-12 07:39:23 +03:00
- [Features](#features)
- [Requirements](#requirements)
- [Installation](#installation)
2016-09-26 09:08:02 +03:00
- [Next steps](#next-steps)
- [Important notes](#important-notes)
2016-06-29 22:35:28 +03:00
- [Upgrade Libreswan](#upgrade-libreswan)
2021-06-06 20:04:19 +03:00
- [Manage VPN users](#manage-vpn-users)
- [Advanced usage](#advanced-usage)
2016-06-25 04:42:57 +03:00
- [Uninstallation](#uninstallation)
2022-02-13 08:20:31 +03:00
- [Feedback & Questions](#feedback--questions)
2016-05-12 07:39:23 +03:00
- [License](#license)
2017-01-08 08:32:58 +03:00
## Quick start
2021-08-10 11:43:00 +03:00
First, prepare your Linux server\* with a fresh install of one of the following OS:
2022-01-27 08:17:56 +03:00
Ubuntu, Debian, CentOS/RHEL, Rocky Linux, AlmaLinux, Amazon Linux 2 or Alpine Linux
2017-01-08 08:32:58 +03:00
Use this one-liner to set up an IPsec VPN server:
2020-12-27 09:16:49 +03:00
```bash
2022-02-11 06:53:59 +03:00
wget https://git.io/vpnstart -qO vpn.sh && sudo sh vpn.sh
2020-12-27 09:16:49 +03:00
```
2017-01-08 08:32:58 +03:00
Your VPN login details will be randomly generated, and displayed on the screen when finished.
2021-07-21 09:56:11 +03:00
<details>
<summary>
2022-02-13 19:38:49 +03:00
Alternative one-liner using curl.
2022-02-13 08:20:31 +03:00
</summary>
```bash
curl -fsSL https://git.io/vpnstart -o vpn.sh && sudo sh vpn.sh
```
</details>
<details>
<summary>
2022-02-13 19:38:49 +03:00
See the VPN script in action (terminal recording).
2021-07-21 09:56:11 +03:00
</summary>
2021-08-10 11:43:00 +03:00
**Note:** This recording is for demo purposes only. VPN credentials in this recording are **NOT** valid.
2021-07-21 09:56:11 +03:00
<p align="center"><img src="docs/images/script-demo.svg"></p>
</details>
2021-07-23 09:25:25 +03:00
A pre-built [Docker image](https://github.com/hwdsl2/docker-ipsec-vpn-server) is also available. For other installation options and client setup, read the sections below.
2017-01-08 08:32:58 +03:00
2017-06-21 19:24:02 +03:00
\* A dedicated server or virtual private server (VPS). OpenVZ VPS is not supported.
2017-01-08 08:32:58 +03:00
2016-04-22 05:35:57 +03:00
## Features
2016-01-09 22:21:30 +03:00
2021-06-04 22:48:19 +03:00
- **New:** The faster IPsec/XAuth ("Cisco IPsec") and IKEv2 modes are supported
2021-06-05 01:27:21 +03:00
- **New:** A pre-built [Docker image](https://github.com/hwdsl2/docker-ipsec-vpn-server) of the VPN server is now available
2016-05-26 22:31:38 +03:00
- Fully automated IPsec VPN server setup, no user input needed
2016-02-11 00:16:41 +03:00
- Encapsulates all VPN traffic in UDP - does not need ESP protocol
2016-01-09 22:21:30 +03:00
- Can be directly used as "user-data" for a new Amazon EC2 instance
2016-10-30 02:30:35 +03:00
- Includes `sysctl.conf` optimizations for improved performance
2016-01-08 07:33:42 +03:00
## Requirements
2015-08-19 00:14:42 +03:00
2021-07-29 07:33:29 +03:00
A dedicated server or virtual private server (VPS), freshly installed with one of the following OS:
2016-01-08 07:33:42 +03:00
2022-02-13 19:38:49 +03:00
- Ubuntu 20.04 or 18.04
- Debian 11[\*](#debian-10-note), 10[\*](#debian-10-note) or 9
2022-02-01 08:15:51 +03:00
- CentOS 7, Rocky Linux 8 or AlmaLinux 8[\*\*](#centos-8-note)
2021-07-29 07:33:29 +03:00
- Red Hat Enterprise Linux (RHEL) 8 or 7
- Amazon Linux 2
2022-01-02 11:20:53 +03:00
- Alpine Linux 3.15 or 3.14
2016-01-08 07:33:42 +03:00
This also includes Linux VMs in public clouds, such as [DigitalOcean](https://blog.ls20.com/digitalocean), [Vultr](https://blog.ls20.com/vultr), [Linode](https://blog.ls20.com/linode), [Microsoft Azure](https://azure.microsoft.com) and [OVH](https://www.ovhcloud.com/en/vps/). [Amazon EC2](https://aws.amazon.com/ec2/) users can deploy rapidly using [CloudFormation](aws/README.md) or [user data](https://blog.ls20.com/ipsec-l2tp-vpn-auto-setup-for-ubuntu-12-04-on-amazon-ec2/#vpnsetup).
2016-01-08 07:33:42 +03:00
[![Deploy to DigitalOcean](docs/images/do-install-button.png)](http://dovpn.carlfriess.com) [![Deploy to Linode](docs/images/linode-deploy-button.png)](https://cloud.linode.com/stackscripts/37239) [![Deploy to AWS](docs/images/aws-deploy-button.png)](aws/README.md) [![Deploy to Azure](docs/images/azure-deploy-button.png)](azure/README.md)
2016-08-11 06:15:11 +03:00
2021-06-05 01:27:21 +03:00
[**&raquo; I want to run my own VPN but don't have a server for that**](https://blog.ls20.com/ipsec-l2tp-vpn-auto-setup-for-ubuntu-12-04-on-amazon-ec2/#gettingavps)
2016-01-09 22:21:30 +03:00
2021-08-15 19:38:27 +03:00
A pre-built [Docker image](https://github.com/hwdsl2/docker-ipsec-vpn-server) is also available. Advanced users can install on a [Raspberry Pi](https://www.raspberrypi.org). [[1]](https://elasticbyte.net/posts/setting-up-a-native-cisco-ipsec-vpn-server-using-a-raspberry-pi/) [[2]](https://www.stewright.me/2018/07/create-a-raspberry-pi-vpn-server-using-l2tpipsec/)
<a name="debian-10-note"></a>
2022-01-18 09:07:35 +03:00
\* Debian 11/10 users should [use the standard Linux kernel](docs/clients.md#debian-10-kernel).
<a name="centos-8-note"></a>
2022-02-01 07:14:52 +03:00
\*\* CentOS Linux 8 [is no longer supported](https://www.centos.org/centos-linux-eol/). You may instead use e.g. Rocky Linux or AlmaLinux.
2016-04-16 01:47:52 +03:00
:warning: **DO NOT** run these scripts on your PC or Mac! They should only be used on a server!
2015-08-19 00:14:42 +03:00
## Installation
2022-02-05 06:55:50 +03:00
First, update your system with `sudo apt-get update && sudo apt-get dist-upgrade` (Ubuntu/Debian) or `sudo yum update` and reboot. This is optional, but recommended.
2016-06-29 22:35:28 +03:00
To install the VPN, please choose one of the following options:
2022-02-13 08:20:31 +03:00
<details open>
<summary>
Option 1: Have the script generate random VPN credentials for you (will be displayed when finished).
</summary>
2016-05-14 08:35:33 +03:00
```bash
2022-02-11 06:53:59 +03:00
wget https://git.io/vpnsetup -qO vpn.sh && sudo sh vpn.sh
2016-05-14 08:35:33 +03:00
```
2021-06-05 01:27:21 +03:00
After successful installation, it is recommended to [set up IKEv2](docs/ikev2-howto.md):
2021-03-29 08:09:15 +03:00
```bash
2021-08-15 19:38:27 +03:00
# Set up IKEv2 using default options
2021-05-24 09:20:32 +03:00
sudo ikev2.sh --auto
2021-08-15 19:38:27 +03:00
# Alternatively, you may customize IKEv2 options
sudo ikev2.sh
2021-03-29 08:09:15 +03:00
```
2022-02-13 08:20:31 +03:00
</details>
2021-03-27 22:51:18 +03:00
2022-02-18 06:40:15 +03:00
<details open>
2022-02-13 08:20:31 +03:00
<summary>
Option 2: Edit the script and provide your own VPN credentials.
</summary>
2016-05-14 08:35:33 +03:00
```bash
2022-02-14 09:37:18 +03:00
wget https://git.io/vpnsetup -nv -O vpn.sh
2021-02-05 06:43:03 +03:00
nano -w vpn.sh
2016-06-11 23:38:30 +03:00
[Replace with your own values: YOUR_IPSEC_PSK, YOUR_USERNAME and YOUR_PASSWORD]
2021-02-05 06:43:03 +03:00
sudo sh vpn.sh
```
2018-12-19 09:14:52 +03:00
**Note:** A secure IPsec PSK should consist of at least 20 random characters.
2022-02-13 08:20:31 +03:00
After successful installation, it is recommended to [set up IKEv2](docs/ikev2-howto.md):
2021-03-27 22:51:18 +03:00
2022-02-13 08:20:31 +03:00
```bash
# Set up IKEv2 using default options
sudo ikev2.sh --auto
# Alternatively, you may customize IKEv2 options
sudo ikev2.sh
```
</details>
<details>
<summary>
Option 3: Define your VPN credentials as environment variables.
</summary>
2016-06-29 22:35:28 +03:00
```bash
# All values MUST be placed inside 'single quotes'
2017-11-01 09:01:49 +03:00
# DO NOT use these special characters within values: \ " '
2022-02-14 09:37:18 +03:00
wget https://git.io/vpnsetup -nv -O vpn.sh
2020-12-27 09:16:49 +03:00
sudo VPN_IPSEC_PSK='your_ipsec_pre_shared_key' \
2016-06-29 22:35:28 +03:00
VPN_USER='your_vpn_username' \
VPN_PASSWORD='your_vpn_password' \
2021-02-05 06:43:03 +03:00
sh vpn.sh
2016-06-29 22:35:28 +03:00
```
2020-12-26 09:23:34 +03:00
2022-02-13 08:20:31 +03:00
After successful installation, it is recommended to [set up IKEv2](docs/ikev2-howto.md):
2021-01-18 09:02:04 +03:00
2022-02-13 08:20:31 +03:00
```bash
# Set up IKEv2 using default options
sudo ikev2.sh --auto
# Alternatively, you may customize IKEv2 options
sudo ikev2.sh
```
</details>
<details>
<summary>
Click here if you are unable to download using wget.
</summary>
2022-02-13 19:38:49 +03:00
You may also use `curl` to download. For example:
2022-02-13 08:20:31 +03:00
```bash
curl -fsSL https://git.io/vpnsetup -o vpn.sh
sudo sh vpn.sh
```
2022-02-13 19:38:49 +03:00
Alternatively, open [vpnsetup.sh](vpnsetup.sh) and click the `Raw` button on the right. Press `Ctrl/Cmd+A` to select all, `Ctrl/Cmd+C` to copy, then paste into your favorite editor.
2022-02-13 08:20:31 +03:00
</details>
2020-12-26 09:23:34 +03:00
2016-09-26 09:08:02 +03:00
## Next steps
2016-05-12 07:39:23 +03:00
2016-06-08 23:56:17 +03:00
Get your computer or device to use the VPN. Please refer to:
2016-05-12 07:39:23 +03:00
2021-07-29 07:33:29 +03:00
[**Guide: How to Set Up and Use IKEv2 VPN**](docs/ikev2-howto.md)
2021-06-05 01:27:21 +03:00
[**Configure IPsec/L2TP VPN Clients**](docs/clients.md)
2016-05-16 21:56:48 +03:00
2021-06-05 01:27:21 +03:00
[**Configure IPsec/XAuth ("Cisco IPsec") VPN Clients**](docs/clients-xauth.md)
2017-10-03 05:55:21 +03:00
2021-06-05 01:27:21 +03:00
If you get an error when trying to connect, see [Troubleshooting](docs/clients.md#troubleshooting).
2016-09-30 19:53:33 +03:00
2016-05-12 07:39:23 +03:00
Enjoy your very own VPN! :sparkles::tada::rocket::sparkles:
2016-09-26 09:08:02 +03:00
## Important notes
2015-08-19 00:14:42 +03:00
2022-01-23 00:34:38 +03:00
*Read this in other languages: [English](README.md#important-notes), [简体中文](README-zh.md#重要提示).*
2016-10-23 22:32:07 +03:00
2021-11-01 07:20:42 +03:00
**Windows users**: For IPsec/L2TP mode, a [one-time registry change](docs/clients.md#windows-error-809) is required if the VPN server or client is behind NAT (e.g. home router).
2016-06-08 04:10:57 +03:00
2022-02-13 19:38:49 +03:00
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](docs/ikev2-howto.md) or [IPsec/XAuth](docs/clients-xauth.md) mode.
2016-09-26 09:08:02 +03:00
2021-07-29 07:33:29 +03:00
To view or update VPN user accounts, see [Manage VPN users](docs/manage-users.md). Helper scripts are included for convenience.
2016-06-26 22:51:21 +03:00
2021-06-05 01:27:21 +03:00
For servers with an external firewall (e.g. [EC2](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-security-groups.html)/[GCE](https://cloud.google.com/vpc/docs/firewalls)), open UDP ports 500 and 4500 for the VPN. Aliyun users, see [#433](https://github.com/hwdsl2/setup-ipsec-vpn/issues/433).
2022-01-23 09:28:58 +03:00
Clients are set to use [Google Public DNS](https://developers.google.com/speed/public-dns/) when the VPN is active. If another DNS provider is preferred, see [Advanced usage](docs/advanced-usage.md).
2016-02-06 22:30:30 +03:00
2021-03-01 19:12:46 +03:00
Using kernel support could improve IPsec/L2TP performance. It is available on [all supported OS](#requirements). Ubuntu users should install the `linux-modules-extra-$(uname -r)` (or `linux-image-extra`) package and run `service xl2tpd restart`.
2016-05-09 09:39:17 +03:00
The scripts will backup existing config files before making changes, with `.old-date-time` suffix.
2016-06-29 22:35:28 +03:00
## Upgrade Libreswan
2016-01-16 19:51:47 +03:00
2022-01-23 09:28:58 +03:00
Use this one-liner to update [Libreswan](https://libreswan.org) ([changelog](https://github.com/libreswan/libreswan/blob/main/CHANGES) | [announce](https://lists.libreswan.org/mailman/listinfo/swan-announce)) on your VPN server.
2020-12-27 09:16:49 +03:00
2016-09-19 01:22:15 +03:00
```bash
2022-02-11 06:53:59 +03:00
wget https://git.io/vpnupgrade -qO vpnup.sh && sudo sh vpnup.sh
2020-12-27 09:16:49 +03:00
```
2016-01-16 19:51:47 +03:00
2022-01-23 09:28:58 +03:00
The latest supported Libreswan version is `4.6`. Check installed version: `ipsec --version`.
2021-10-10 07:44:25 +03:00
**Note:** `xl2tpd` can be updated using your system's package manager, such as `apt-get` on Ubuntu/Debian.
2021-06-06 20:04:19 +03:00
## Manage VPN users
2021-06-06 20:04:19 +03:00
See [Manage VPN users](docs/manage-users.md).
2021-06-06 23:27:56 +03:00
- [View or update the IPsec PSK](docs/manage-users.md#view-or-update-the-ipsec-psk)
- [View VPN users](docs/manage-users.md#view-vpn-users)
- [Manage VPN users using helper scripts](docs/manage-users.md#manage-vpn-users-using-helper-scripts)
- [Manually manage VPN users](docs/manage-users.md#manually-manage-vpn-users)
2021-06-06 20:04:19 +03:00
## Advanced usage
2021-06-06 20:04:19 +03:00
See [Advanced usage](docs/advanced-usage.md).
2021-06-06 23:27:56 +03:00
- [Use alternative DNS servers](docs/advanced-usage.md#use-alternative-dns-servers)
- [DNS name and server IP changes](docs/advanced-usage.md#dns-name-and-server-ip-changes)
2022-01-02 21:57:55 +03:00
- [IKEv2-only VPN](docs/advanced-usage.md#ikev2-only-vpn)
2021-06-06 23:27:56 +03:00
- [Internal VPN IPs and traffic](docs/advanced-usage.md#internal-vpn-ips-and-traffic)
- [Port forwarding to VPN clients](docs/advanced-usage.md#port-forwarding-to-vpn-clients)
2021-06-06 23:27:56 +03:00
- [Split tunneling](docs/advanced-usage.md#split-tunneling)
- [Access VPN server's subnet](docs/advanced-usage.md#access-vpn-servers-subnet)
- [Modify IPTables rules](docs/advanced-usage.md#modify-iptables-rules)
- [Deploy Google BBR congestion control algorithm](docs/advanced-usage.md#deploy-google-bbr-congestion-control-algorithm)
2021-06-06 23:27:56 +03:00
2016-06-25 04:42:57 +03:00
## Uninstallation
2021-06-05 01:27:21 +03:00
See [Uninstall the VPN](docs/uninstall.md).
2021-09-29 07:13:45 +03:00
- [Uninstall using helper script](docs/uninstall.md#uninstall-using-helper-script)
- [Manually uninstall the VPN](docs/uninstall.md#manually-uninstall-the-vpn)
2022-02-13 08:20:31 +03:00
## Feedback & Questions
- Have an improvement suggestion for documentation or VPN scripts? Open an [Enhancement request](https://github.com/hwdsl2/setup-ipsec-vpn/issues/new/choose). [Pull requests](https://github.com/hwdsl2/setup-ipsec-vpn/pulls) are also welcome.
2022-02-13 19:38:49 +03:00
- If you found a reproducible bug, open a bug report for the [IPsec VPN](https://github.com/libreswan/libreswan/issues?q=is%3Aissue) or for the [VPN scripts](https://github.com/hwdsl2/setup-ipsec-vpn/issues/new/choose).
2022-02-13 08:20:31 +03:00
- Got a question? Please first search [existing issues](https://github.com/hwdsl2/setup-ipsec-vpn/issues?q=is%3Aissue) and comments [in this Gist](https://gist.github.com/hwdsl2/9030462#comments) and [on my blog](https://blog.ls20.com/ipsec-l2tp-vpn-auto-setup-for-ubuntu-12-04-on-amazon-ec2/#disqus_thread).
- Ask VPN related questions on the [Libreswan](https://lists.libreswan.org/mailman/listinfo/swan) or [strongSwan](https://lists.strongswan.org/mailman/listinfo/users) mailing list, or read these wikis: [[1]](https://libreswan.org/wiki/Main_Page) [[2]](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/security_guide/sec-securing_virtual_private_networks) [[3]](https://wiki.strongswan.org/projects/strongswan/wiki/UserDocumentation) [[4]](https://wiki.gentoo.org/wiki/IPsec_L2TP_VPN_server) [[5]](https://wiki.archlinux.org/index.php/Openswan_L2TP/IPsec_VPN_client_setup).
2016-04-24 02:47:03 +03:00
## License
2015-08-19 00:14:42 +03:00
2022-01-02 09:09:03 +03:00
Copyright (C) 2014-2022 [Lin Song](https://github.com/hwdsl2) [![View my profile on LinkedIn](https://static.licdn.com/scds/common/u/img/webpromo/btn_viewmy_160x25.png)](https://www.linkedin.com/in/linsongui)
2021-06-05 01:27:21 +03:00
Based on [the work of Thomas Sarlandie](https://github.com/sarfata/voodooprivacy) (Copyright 2012)
2015-08-19 00:14:42 +03:00
2021-06-05 01:27:21 +03:00
[![Creative Commons License](https://i.creativecommons.org/l/by-sa/3.0/88x31.png)](http://creativecommons.org/licenses/by-sa/3.0/)
This work is licensed under the [Creative Commons Attribution-ShareAlike 3.0 Unported License](http://creativecommons.org/licenses/by-sa/3.0/)
2015-08-19 00:14:42 +03:00
Attribution required: please include my name in any derivative and let me know how you have improved it!