Debian L2TP IPSEC 服务器搭建
系统版本:Debian 11.6
1. 下载软件包
sudo apt-get install strongswan xl2tpd ppp lsof -y
2. 开启 IP 转发功能
vi /etc/sysctl.conf
在末尾添加以下参数:
net.ipv4.ip_forward = 1
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.icmp_ignore_bogus_error_responses = 1
配置成功后,启用配置
sysctl -p
3. 修改 ipsec.conf 文件
先拷贝一份默认配置文件
mv /etc/ipsec.conf /etc/ipsec.conf.bak
创建一个新的ipsec.conf文件
vi /etc/ipsec.conf
配置如下:
version 2
config setup
conn L2TP-PSK-noNAT
authby=secret
auto=add
keyingtries=3
ikelifetime=8h
keylife=1h
ike=aes256-sha1
esp=aes256-sha1
type=transport
# left这里写的是服务器 IP 地址
left=1.1.1.1
leftprotoport=17/1701
right=%any
rightprotoport=17/%any
dpddelay=10
dpdtimeout=20
dpdaction=clear
4. 配置 IPsec PSK
vi /etc/ipsec.secrets
%any标识服务器地址
秘钥需要加上" "
# This file holds shared secrets or RSA private keys for authentication.
# RSA private key for this host, authenticating it to any other host
# which knows the public part.
%any : PSK "123456"
5. 修改 xl2tpd.conf 文件 先拷贝一份默认配置
mv /etc/xl2tpd/xl2tpd.conf /etc/xl2tpd/xl2tpd.conf.bak
创建一份新配置vi /etc/xl2tpd/xl2tpd.conf,具体参数如下:
[global]
ipsec saref = yes
saref refinfo = 30
;debug avp = yes
;debug network = yes
debug state = yes
;debug tunnel = yes
[lns default]
ip range = 10.10.10.100-10.10.10.200
local ip = 10.10.10.1
refuse pap = yes
require authentication = yes
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes
local ip = 10.10.10.1服务器l2tp的IP地址
ip range = 10.10.10.100-10.10.10.200客户端 l2tp 分配的地址池
注释的几个是调试参数,如果出现问题,可将注释去掉来查看报错原因
pppoptfile = /etc/ppp/options.xl2tpd指定调用option.xl2tp文件,因此需要在 ppp 目录下创建这个文件:
vi /etc/ppp/options.xl2tpd
配置内容如下:
require-mschap-v2
ms-dns 8.8.8.8
ms-dns 114.114.114.114
auth
mtu 1200
mru 1000
crtscts
hide-password
modem
name l2tpd
proxyarp
lcp-echo-interval 30
lcp-echo-failure 4
ms-dns为需要 vpn 客户端使用的 dns 服务器
6. 设置用户名密码
vi /etc/ppp/chap-secrets
内容如下:注意用户名密码需要加上" "
# Secrets for authentication using CHAP
# client server secret IP addresses
"ziyang" l2tpd "123456" *
7. 开启 NAT 转发并开放所需端口
sudo iptables -A INPUT -p gre -j ACCEPT
sudo iptables -I INPUT -p udp -m multiport --dports 500,4500,1701 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 47 -j ACCEPT
sudo iptables -t nat -A POSTROUTING -s 10.10.10.0/24 -o eth0 -j MASQUERADE
8. 重启服务
ipsec restart
service xl2tpd restart
9. 查看日志
tail -f /var/log/syslog