Ubuntu上设置L2TP over IPsec
1. L2TP over IPsec基本概念
L2TP(第2层隧道协议)是一种隧道协议,旨在支持互联网上的虚拟专用网络(VPN)连接。L2TP本身不提供加密服务,通常与IPsec(Internet协议安全)结合使用,以在L2TP隧道内提供加密。这种组合方式称为L2TP over IPsec,它结合了L2TP的隧道能力和IPsec的加密功能,从而确保数据在公共网络上的安全传输。
2. 在Ubuntu上设置L2TP over IPsec的步骤
- 安装必要的软件包 对于Ubuntu 20.04及更高版本,可以使用以下命令安装L2TP和IPsec相关的软件包:
sudo apt update
sudo apt install network-manager-l2tp network-manager-l2tp-gnome strongswan xl2tpd ppp
- 配置IPsec 编辑IPsec配置文件/etc/ipsec.conf,添加L2TP over IPsec相关的配置:
conn L2TP-PSK
type=transport
authby=psk
keyexchange=ikev1
keyingtries=3
rekey=no
left=%defaultroute
leftprotoport=17/1701
right=%any
rightprotoport=17/%any
auto=add
- 创建或编辑/etc/ipsec.secrets文件,添加预共享密钥(PSK):
%any %any : PSK "your_pre_shared_key"
请将your_pre_shared_key替换为实际的预共享密钥。
- 配置xl2tpd 编辑xl2tpd配置文件/etc/xl2tpd/xl2tpd.conf,添加或修改以下配置:
[global]
ipsec saref = yes
[lns default]
ip range = 192.168.100.100-192.168.100.200
local ip = 192.168.100.1
refuse pap = yes
refuse chap = yes
require authentication = yes
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
- 创建或编辑/etc/ppp/options.xl2tpd文件,添加PPP相关配置:
require-mschap-v2
ms-dns 8.8.8.8
ms-dns 8.8.4.4
asyncmap 0
auth
crtscts
lock
hide-password
modem
debug
name l2tpd
proxyarp
lcp-echo-interval 30
lcp-echo-failure 4
- 配置VPN用户密码 编辑/etc/ppp/chap-secrets文件,添加VPN用户和密码:
username * your_password *
请将username替换为实际的用户名,your_password替换为实际的密码。
- 启动并启用服务 重启IPsec和xl2tpd服务,并设置它们开机自启动:
sudo systemctl restart strongswan-starter xl2tpd
sudo systemctl enable strongswan-starter xl2tpd
3. 配置文件示例
/etc/ipsec.conf
conn L2TP-PSK
type=transport
authby=psk
keyexchange=ikev1
keyingtries=3
rekey=no
left=%defaultroute
leftprotoport=17/1701
right=%any
rightprotoport=17/%any
auto=add
/etc/ipsec.secrets
%any %any : PSK "your_pre_shared_key"
/etc/xl2tpd/xl2tpd.conf
[global]
ipsec saref = yes
[lns default]
ip range = 192.168.100.100-192.168.100.200
local ip = 192.168.100.1
refuse pap = yes
refuse chap = yes
require authentication = yes
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
/etc/ppp/options.xl2tpd
require-mschap-v2
ms-dns 8.8.8.8
ms-dns 8.8.4.4
asyncmap 0
auth
crtscts
lock
hide-password
modem
debug
name l2tpd
proxyarp
lcp-echo-interval 30
lcp-echo-failure 4
/etc/ppp/chap-secrets
username * your_password *
4. 可能遇到的问题及其解决方案
IP转发未启用:确保IP转发已启用。可以通过编辑/etc/sysctl.conf文件并添加以下代码来启用IP转发,然后运行sudo sysctl -p使其生效。
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
防火墙阻止VPN端口:确保防火墙允许VPN使用的端口(如1701、500、4500等)。可以使用ufw命令来允许这些端口,代码如下,如果是云服务器还需在控制台开启端口
sudo ufw allow 1701/tcp
sudo ufw allow 500/udp
sudo ufw allow 4500/udp
预共享密钥不匹配:确保VPN服务器和客户端使用的预共享密钥完全相同。
5. 测试并验证L2TP over IPsec连接
在Ubuntu上配置好L2TP over IPsec VPN后,可以通过NetworkManager添加一个新的VPN连接,并配置相应的服务器地址、用户名、密码和预共享密钥。然后尝试连接VPN,如果连接成功,则表示L2TP over IPsec VPN已正确设置。
可以使用ip a命令查看是否获取到了VPN分配的IP地址,以及使用ping命令测试与VPN内部网络的连通性。
321

被折叠的 条评论
为什么被折叠?



