行业知识
如何在 Debian / Ubuntu 服务器上设置 L2TP / IPSec VPN?
Jan.08.2025
在 Debian / Ubuntu 服务器上设置 L2TP / IPSec VPN 需要遵循一系列步骤。您将需要安装必要的软件包,配置 VPN,调整防火墙设置,并且确保服务可以正常启动和运行。以下是一个详细的指南,帮助您在 Debian 或 Ubuntu 服务器上设置 L2TP/IPSec VPN。
第一步,确保您的系统是最新的,您可以使用以下命令来更新包列表并升级软件:
```
sudo apt-get update
sudo apt-get upgrade
```
这有助于确保系统上的所有软件和库都是最新的版本,以避免潜在的兼容性问题。
接下来,安装所需的软件包 xl2tpd、strongswan 和 ppp。使用以下命令进行安装:
```
sudo apt-get install xl2tpd strongswan ppp
```
这些包包含了实现 VPN 服务所需要的所有工具。
您需要配置 strongSwan 来处理 IPSec。编辑配置文件 `/etc/ipsec.conf` 并添加以下内容:
```
config setup
charondebug="ike 2, knl 2, cfg 2"
uniqueids=no
conn %default
keyexchange=ikev1
authby=secret
ike=aes256-sha1-modp1024!
esp=aes256-sha1!
conn myvpn
auto=add
left=SERVER_PUBLIC_IP
leftid=SERVER_PUBLIC_IP
leftsubnet=0.0.0.0/0
right=%any
rightid=%any
rightauth=psk
rightsourceip=10.0.0.0/24
rightdns=8.8.8.8
```
确保将 `SERVER_PUBLIC_IP` 替换为您的服务器的公有 IP 地址。
然后,编辑 `/etc/ipsec.secrets`,为您的 VPN 配置预共享密钥:
```
: PSK "your-vpn-psk"
```
将 `your-vpn-psk` 替换为您选择的强密码。
配置 L2TPd。编辑文件 `/etc/xl2tpd/xl2tpd.conf`,修改或者添加下列内容:
```
[global]
port = 1701
[lns default]
ip range = 10.0.0.2-10.0.0.255
local ip = 10.0.0.1
require chap = yes
refuse pap = yes
require authentication = yes
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes
```
上面的 IP 地址可以根据您的网络环境调整。
您还需要配置 PPP。编辑文件 `/etc/ppp/options.xl2tpd`,确保包含以下内容:
```
require-mschap-v2
ms-dns 8.8.8.8
asyncmap 0
auth
crtscts
lock
hide-password
modem
debug
name l2tpd
proxyarp
lcp-echo-interval 30
lcp-echo-failure 4
```
这将启用 MS-CHAP v2 认证和 DNS 解析。
另外,为用户配置登录凭证。在 `/etc/ppp/chap-secrets` 文件中添加以下内容:
```
username l2tpd your-vpn-password *
```
将 `username` 和 `your-vpn-password` 替换为你想要使用的用户名和强密码。
配置完成后,您需要设置防火墙以允许必要的流量。您可以使用以下命令:
```
sudo iptables -A INPUT -p udp --dport 1701 -j ACCEPT
sudo iptables -A INPUT -p udp --dport 500 -j ACCEPT
sudo iptables -A INPUT -p udp --dport 4500 -j ACCEPT
sudo iptables -A INPUT -p udp --dport 50 -j ACCEPT
```
这些命令会允许 L2TP 和 IPSec 所需的流量通过。
启用并启动 strongSwan 和 xl2tpd 服务:
```
sudo systemctl enable strongswan
sudo systemctl enable xl2tpd
sudo systemctl start strongswan
sudo systemctl start xl2tpd
```
这些命令会确保在系统引导时自动启动服务。
测试您的 VPN 连接,确保您配置的所有设置都正确,并且服务能够正常运行。可以通过使用 VPN 客户端