阿里云ECS搭建L2TP/IPsec VPN完整教程 目录
服务器端配置 环境准备 系统要求
阿里云ECS实例(推荐Ubuntu 18.04/20.04或CentOS 7/8)
公网IP地址
至少1GB内存
开放UDP 500、UDP 4500、TCP 1701端口
阿里云安全组配置 在阿里云控制台配置安全组规则,开放以下端口:
协议
端口范围
授权对象
描述
UDP
500
0.0.0.0/0
IPsec IKE
UDP
4500
0.0.0.0/0
IPsec NAT-T
TCP
1701
0.0.0.0/0
L2TP
安装必要软件 Ubuntu/Debian系统 1 2 3 4 5 6 7 8 sudo apt update && sudo apt upgrade -y sudo apt install -y strongswan xl2tpd ppp lsof sudo apt install -y linux-modules-extra-$(uname -r)
CentOS/RHEL系统 1 2 3 4 5 6 7 8 sudo yum update -y sudo yum install -y epel-release sudo yum install -y strongswan xl2tpd ppp lsof
配置IPsec 1. 编辑IPsec配置文件 1 sudo nano /etc/ipsec.conf
添加以下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 config setup charondebug ="ike 1, knl 1, cfg 0" uniqueids =no conn L2TP-PSK authby =secret pfs =no auto =add keyingtries =3 rekey =no ikelifetime =8 h keylife =1 h type =transport left =%defaultroute leftid =YOUR_SERVER_PUBLIC_IP leftprotoport =17 /1701 right =%any rightprotoport =17 /%any dpddelay =40 dpdtimeout =130 dpdaction =clear
注意: 将 YOUR_SERVER_PUBLIC_IP
替换为你的阿里云ECS公网IP地址。
2. 配置IPsec密钥 1 sudo nano /etc/ipsec.secrets
添加以下内容:
1 2 # /etc/ipsec.secrets - strongSwan IPsec secrets file YOUR_SERVER_PUBLIC_IP %any : PSK "YOUR_IPSEC_PSK"
注意:
将 YOUR_SERVER_PUBLIC_IP
替换为你的公网IP
将 YOUR_IPSEC_PSK
替换为一个强密码(建议16位以上随机字符)
配置L2TP 编辑xl2tpd配置文件 1 sudo nano /etc/xl2tpd/xl2tpd.conf
添加以下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 [global] ipsec saref = yes saref refinfo = 30 [lns default] ip range = 192.168 .42.10 -192.168 .42.250 local ip = 192.168 .42.1 require chap = yes refuse pap = yes require authentication = yes name = l2tpdppp debug = yes pppoptfile = /etc/ppp/options.xl2tpdlength bit = yes
配置PPP 1. 创建PPP选项文件 1 sudo nano /etc/ppp/options.xl2tpd
添加以下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ipcp-accept-local ipcp-accept-remote ms-dns 8.8.8.8 ms-dns 114.114.114.114 noccp auth crtscts idle 1800 mtu 1410 mru 1410 nodefaultroute debug proxyarp connect-delay 5000 refuse-pap refuse-chap refuse-mschap require-mschap-v2 persist logfile /var/log/xl2tpd.log
2. 配置VPN用户账号 1 sudo nano /etc/ppp/chap-secrets
添加用户账号(格式:用户名 服务名 密码 IP地址):
1 2 3 4 # Secrets for authentication using CHAP # client server secret IP addresses vpnuser1 l2tpd password123 * vpnuser2 l2tpd password456 *
配置防火墙 Ubuntu/Debian (UFW) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 echo 'net.ipv4.ip_forward = 1' | sudo tee -a /etc/sysctl.confecho 'net.ipv4.conf.all.accept_redirects = 0' | sudo tee -a /etc/sysctl.confecho 'net.ipv4.conf.all.send_redirects = 0' | sudo tee -a /etc/sysctl.confecho 'net.ipv4.conf.default.rp_filter = 0' | sudo tee -a /etc/sysctl.confecho 'net.ipv4.conf.default.accept_source_route = 0' | sudo tee -a /etc/sysctl.confecho 'net.ipv4.conf.default.send_redirects = 0' | sudo tee -a /etc/sysctl.confecho 'net.ipv4.icmp_ignore_bogus_error_responses = 1' | sudo tee -a /etc/sysctl.confsudo sysctl -p sudo iptables -t nat -A POSTROUTING -s 192.168.42.0/24 -o eth0 -j MASQUERADE sudo iptables -A FORWARD -s 192.168.42.0/24 -j ACCEPT sudo iptables -A FORWARD -d 192.168.42.0/24 -j ACCEPT sudo apt install -y iptables-persistent sudo netfilter-persistent save
CentOS/RHEL (firewalld) 1 2 3 4 5 6 7 8 9 10 11 echo 'net.ipv4.ip_forward = 1' | sudo tee -a /etc/sysctl.confsudo sysctl -p sudo firewall-cmd --permanent --add-service=ipsec sudo firewall-cmd --permanent --add-port=1701/tcp sudo firewall-cmd --permanent --add-port=500/udp sudo firewall-cmd --permanent --add-port=4500/udp sudo firewall-cmd --permanent --add-masquerade sudo firewall-cmd --reload
启动服务 1 2 3 4 5 6 7 8 9 10 11 12 13 sudo systemctl enable strongswan sudo systemctl start strongswan sudo systemctl enable xl2tpd sudo systemctl start xl2tpd sudo systemctl status strongswan sudo systemctl status xl2tpd sudo netstat -tulpn | grep -E ':(500|4500|1701)'
客户端配置 iPhone/iPad配置 步骤1:打开设置
在iPhone/iPad上打开「设置」应用
点击「通用」
点击「VPN与设备管理」或「VPN」
步骤2:添加VPN配置
点击「添加VPN配置…」
选择「L2TP」类型
填写以下信息:
描述 :自定义名称(如:我的VPN)
服务器 :你的阿里云ECS公网IP
账户 :在chap-secrets中配置的用户名
密码 :对应的用户密码
密钥 :IPsec预共享密钥(YOUR_IPSEC_PSK)
点击「完成」
步骤3:连接VPN
在VPN设置页面,打开VPN开关
选择刚才创建的VPN配置
等待连接成功
Android手机配置 步骤1:打开网络设置
打开「设置」应用
点击「网络和互联网」或「连接」
点击「VPN」
步骤2:添加VPN配置
点击「+」或「添加VPN配置文件」
填写以下信息:
名称 :自定义名称
类型 :选择「L2TP/IPSec PSK」
服务器地址 :你的阿里云ECS公网IP
L2TP密钥 :留空(可选)
IPSec标识符 :留空
IPSec预共享密钥 :YOUR_IPSEC_PSK
点击「保存」
步骤3:连接VPN
在VPN列表中点击刚创建的配置
输入用户名和密码
点击「连接」
Mac电脑配置 步骤1:打开网络偏好设置
点击屏幕左上角的苹果菜单
选择「系统偏好设置」
点击「网络」
步骤2:创建VPN连接
点击左下角的「+」号
在弹出窗口中选择:
接口 :VPN
VPN类型 :IPSec上的L2TP
服务名称 :自定义名称
点击「创建」
步骤3:配置VPN设置
填写以下信息:
服务器地址 :你的阿里云ECS公网IP
账户名称 :chap-secrets中的用户名
点击「鉴定设置」
在弹出窗口中填写:
点击「好」
点击「应用」
步骤4:连接VPN
点击「连接」按钮
等待连接成功,状态显示为「已连接」
常见问题解决 1. 连接失败问题 检查服务器状态 1 2 3 4 5 6 7 8 9 sudo systemctl status strongswan xl2tpd sudo netstat -tulpn | grep -E ':(500|4500|1701)' sudo tail -f /var/log/syslog sudo tail -f /var/log/xl2tpd.log
检查防火墙设置 1 2 3 4 5 6 sudo ufw status sudo iptables -L -n sudo firewall-cmd --list-all
2. 阿里云特殊配置 由于阿里云的网络环境特殊性,可能需要额外配置:
修改IPsec配置 在 /etc/ipsec.conf
中添加:
1 2 3 4 5 conn L2TP-PSK leftsubnet =0.0 .0.0 /0 rightsubnet =0.0 .0.0 /0 forceencaps =yes
检查内网IP配置 确保xl2tpd.conf中的local ip设置正确:
3. 客户端连接问题 iOS设备
错误809 :通常是IPsec配置问题,检查预共享密钥
自动断开 :iOS设备在锁屏后会断开WiFi,这是正常行为
Android设备
认证失败 :检查用户名密码是否正确
连接超时 :尝试重启VPN服务
Mac设备
连接失败 :检查系统防火墙设置
DNS问题 :手动设置DNS服务器
4. 性能优化 调整MTU值
启用硬件加速 1 2 3 4 5 6 7 lsmod | grep -E '(aes|crypto)' conn L2TP-PSK esp=aes128-sha1-modp2048!
安全建议 1. 密码安全
使用强密码(至少16位,包含大小写字母、数字、特殊字符)
定期更换密码
不同用户使用不同密码
2. 访问控制
限制VPN用户数量
定期审查用户账号
监控连接日志
3. 网络安全
定期更新系统和软件包
配置fail2ban防止暴力破解
使用证书认证替代密码认证(高级配置)
4. 监控和日志 1 2 3 4 5 6 7 8 sudo tail -f /var/log/syslog | grep -E '(xl2tpd|pluto|charon)' sudo tail -f /var/log/auth.log sudo ipsec status
5. 备份配置 1 2 3 4 5 6 7 sudo tar -czf vpn-config-backup.tar.gz \ /etc/ipsec.conf \ /etc/ipsec.secrets \ /etc/xl2tpd/xl2tpd.conf \ /etc/ppp/options.xl2tpd \ /etc/ppp/chap-secrets