先保证Ubuntu上安装了最新的l2tp程序,通过以下命令
l2tp是需要Ipsec来进行加密的,又通常称为l2tp/ipsec,一般用strongswan来实现ipsec。检查系统安装了最新的strongswan程序,通过以下命令
sudo apt install strongswan
然后是配置部分了,需要用sudo权限来修改下面几个文件:
(1) 编辑/etc/ipsec.conf
conn L2TP-IPSEC
authby=secret
auto=add
keyingtries=3
ikelifetime=8h
keylife=1h
ike=aes128-sha256-modp3072
esp=aes128-sha256
type=transport
#Replace IP address with your current IP used for L2TP connection
left=192.168.10.162
leftsubnet=192.168.10.0/24[17/1701]
#Replace IP address with the remote end to connect this server
right=192.168.2.0/24
rightsubnet=192.168.2.0/24[17/%any]
dpddelay=30s
dpdtimeout=150s
dpdaction=clear
关于Ubuntu这个文件的配置,请参考官方文档:
这里left是表示Server端,即本机。leftsubnet=192.168.10.0/24[17/1701] 。这里 [ 前面是subnet,[ ]的部分是protocol/port。根据自己的需求更改。
right表示client端,即远端。right可以用子网段来表示,如上面192.168.2.0/24表示接收来自192.168.2.1~192.168.2.254的所有连接请求。rightsubnet中的%any表示任意。
(2) 编辑/etc/ipsec.secrets,设置ipsec的预共享秘钥
# 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.
#the selector is optional
192.168.10.162 192.168.2.0/24 : PSK "anyk-xgpon-unicom"
请参考官方文档:
正文里:的前面是selector,可选。前面一个是left的IP address, 后面一个是right的IP address。PSK后面双引号中的是密钥。这个秘钥在客户端创建VPN连接的时候要先输入。如下图中Win10上创建VPN连接时,在安全->高级属性 (密钥) 里输入。
(3) 编辑/etc/ppp/options.xl2tpd. l2tp是为PPP(point to point protocol)提供隧道服务的,所以ppp相关的配置也要设置。
require-mschap-v2
refuse-mschap
ms-dns 127.0.0.53
asyncmap 0
auth
crtscts
idle 1800
mtu 1410
mru 1410
hide-password
local
modem
lock
name l2tpd
connect-delay 5000
lcp-echo-interval 30
lcp-echo-failure 4
这里require-mschap-v2,refuse-mschap 表示需要支持Microsoft的chap协议第二版。
ms-dns后面是本机使用的DNS server的IP Address,可以通过$cat /etc/resolv.conf 查看到。
(4)编辑/etc/xl2tpd/xl2tpd.conf
[global]
ipsec saref = no
debug tunnel = no
debug avp = no
debug network = no
debug state = no
access control = no
rand source = dev
port = 1701
auth file = /etc/ppp/chap-secrets
[lns default]
ip range = 192.168.18.1 - 192.168.18.254
local ip = 192.168.18.162
name = l2tp
pass peer = yes
refuse pap = yes
refuse chap = yes
require authentication = yes
ppp debug = no
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes
具体各个参数的含义和如何设置请参考官方文档:
lns是指L2TP network server,即本机。和它对应的另一端是lac,即L2TP access concentrator,可以理解为客户端。
local ip是指lns给本机分配的ip address
(5)编辑/etc/ppp/chap-secrets,填写客户端连接时的用户名和密码
# Secrets for authentication using CHAP
# client server secret IP addresses
xpon * ubuntu18.04 *
在Win10上设置好VPN连接,在第一次点击连接时,会弹窗要求输入用户名和密码:
最后,重启xl2tpd和ipsec服务
$sudo service xl2tpd restart
$sudo service ipsec restart
这个l2tp服务器应该已经运行起来了。查看其运行状态
$sudo service xl2tpd status
客户端进行相应配置,连接l2tp服务器。可以看到上面的命令有如下输出:
● xl2tpd.service - LSB: layer 2 tunelling protocol daemon
Loaded: loaded (/etc/init.d/xl2tpd; generated)
Active: active (running) since Thu 2022-12-08 11:16:10 CST; 3h 33min ago
Docs: man:systemd-sysv-generator(8)
Process: 20838 ExecStop=/etc/init.d/xl2tpd stop (code=exited, status=0/SUCCESS)
Process: 20845 ExecStart=/etc/init.d/xl2tpd start (code=exited, status=0/SUCCESS)
Tasks: 1 (limit: 4915)
CGroup: /system.slice/xl2tpd.service
└─20852 /usr/sbin/xl2tpdDec 08 14:32:42 anyk-build pppd[21916]: remote IP address 192.168.2.1
Dec 08 14:36:46 anyk-build pppd[21916]: LCP terminated by peer (ix3M-F^@<M-Mt^@^@^@^@)
Dec 08 14:36:46 anyk-build pppd[21916]: Connect time 4.1 minutes.
Dec 08 14:36:46 anyk-build pppd[21916]: Sent 0 bytes, received 399025 bytes.
Dec 08 14:36:46 anyk-build xl2tpd[20852]: control_finish: Connection closed to 192.168.2.56, serial 0 ()
Dec 08 14:36:46 anyk-build xl2tpd[20852]: Terminating pppd: sending TERM signal to pid 21916
Dec 08 14:36:46 anyk-build xl2tpd[20852]: control_finish: Connection closed to 192.168.2.56, port 1701 (), Local: 22894, Remote: 2
Dec 08 14:36:46 anyk-build pppd[21916]: Modem hangup
Dec 08 14:36:46 anyk-build pppd[21916]: Connection terminated.
Dec 08 14:36:47 anyk-build pppd[21916]: Exit.
上面的前半部分,是描述xl2tpd这个服务什么时候启动的,相关进程等。