Centos 7、8安装配置L2TP

仅供在配置中遇到的问题心得分享
如果有控制台需开启 tcp 22 udp 500 1701 4500
修订:2022-06-26

更新系统并重启

yum update -y && shutdown -r now

安装软件及依赖关系

yum install -y epel-release && yum install -y xl2tpd libreswan lsof net-tools

启用防火墙和开机自启

systemctl start firewalld && systemctl enable firewalld

编辑xl2tpd文件

vi /etc/xl2tpd/xl2tpd.conf

修改ip range和local ip 如图 切记不要修改1段或是2段 例 192.168.1.x 或192.168.2.x 大多数家庭的光猫和路由器都是这个网段 总之不能和你当前网络冲突就行 我这修改成192.168.66.x 仅供参考

编辑options.xl2tpd文件

vi /etc/xl2tpd/xl2tpd.conf 

修改ip range和local ip 如图 切记不要修改1段或是2段 例 192.168.1.x 或192.168.2.x 大多数家庭的光猫和路由器都是这个网段 总之不能和你当前网络冲突就行 我这修改成192.168.66.x 仅供参考

编辑options.xl2tpd文件

vi /etc/ppp/options.xl2tpd

在connect-delay 5000 下一行新增 效果如图

refuse-pap
refuse-mschap
require-mschap-v2
persist
hide-password
+mschap-v2

编辑ipsec.conf文件

vi /etc/ipsec.conf

新增 如图 切记切记切记 是 键盘TAB按键对齐 不是 空格键 这个是必须的

protostack=netkey
dumpdir=/var/run/pluto/

新建l2tp-ipsec.conf 配置一 你最好一行一行来 用键盘TAB键对齐 直接全部复制很大几率出错 如图,配置二 不需要按TAB一行一行来,推荐使用配置二

vi /etc/ipsec.d/l2tp-ipsec.conf

下面为配置一 注意:如果服务器绑定了EIP直接输入公网ip 没绑就内网ip 如果你的ios连接不上L2TP你还需要把sha2-truncbug=yes 这行前面加个# 或直接删除这行

conn L2TP-PSK-NAT
        rightsubnet=0.0.0.0/0
        dpddelay=10
        dpdtimeout=20
        dpdaction=clear
        forceencaps=yes
        also=L2TP-PSK-noNAT
conn L2TP-PSK-noNAT
        authby=secret
        pfs=no
        auto=add
        keyingtries=3
        rekey=no
        ikelifetime=8h
        keylife=1h
        type=transport
        left=外网或内网IP    # 这个是网卡的外网IP,通过NAT转发 外网不行换成内网IP
        leftid=外网或内网IP     # 标识 (同上)
        leftprotoport=17/1701    # 端口,默认1701,不用改
        right=%any
        ike=3des-sha1,3des-sha2,aes-sha1,aes-sha1;modp1024,aes-sha2,aes-sha2;modp1024,aes256-sha2_256
        phase2alg=3des-sha1,3des-sha2,aes-sha1,aes-sha2,aes256-sha2_256
        sha2-truncbug=yes

下面为配置二 (一和二只能选一种,强烈推荐配置二)

conn shared
  left=%defaultroute
  leftid=$public_ip
  right=%any
  encapsulation=yes
  authby=secret
  pfs=no
  rekey=no
  keyingtries=5
  dpddelay=30
  dpdtimeout=120
  dpdaction=clear
  ikev2=never
  ike=aes256-sha2,aes128-sha2,aes256-sha1,aes128-sha1,aes256-sha2;modp1024,aes128-sha1;modp1024
  phase2alg=aes_gcm-null,aes128-sha1,aes256-sha1,aes256-sha2_512,aes128-sha2,aes256-sha2
  ikelifetime=24h
  salifetime=24h
  sha2-truncbug=no

conn l2tp-psk
  auto=add
  leftprotoport=17/1701
  rightprotoport=17/%any
  type=transport
  also=shared

conn xauth-psk
  auto=add
  leftsubnet=0.0.0.0/0
  rightaddresspool=$XAUTH_POOL
  modecfgdns=$DNS_SRVS
  leftxauthserver=yes
  rightxauthclient=yes
  leftmodecfgserver=yes
  rightmodecfgclient=yes
  modecfgpull=yes
  cisco-unity=yes
  also=shared

设置用户名密码

vi /etc/ppp/chap-secrets

最后一行增加 格式如下

Myname    *       123456       *

设置预共享密钥PSK

vi /etc/ipsec.d/default.secrets

密钥可以修改成自己的

%any %any : PSK "yourPassword123"

允许防火墙端口 创建文件

vi /usr/lib/firewalld/services/l2tpd.xml

增加如下内容

<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>l2tpd</short>
  <description>L2TP IPSec</description>
  <port protocol="udp" port="500"/>
  <port protocol="udp" port="4500"/>
  <port protocol="udp" port="1701"/>
</service>

firewall 开放端口

firewall-cmd --permanent --zone=public --add-service=ipsec 
firewall-cmd --permanent --zone=public --add-service=l2tpd
firewall-cmd --permanent --zone=public --add-port=22/tcp
firewall-cmd --permanent --zone=public --add-port=500/udp
firewall-cmd --permanent --zone=public --add-port=1701/udp
firewall-cmd --permanent --zone=public --add-port=4500/udp
firewall-cmd --permanent --zone=public --add-masquerade
firewall-cmd --reload

这里遇到个问题 增加udp 4500端口报错 如图

需要重新载入在增加端口

firewall-cmd --reload 
firewall-cmd --permanent --zone=public --add-port=4500/udp

在使用命令查看是否开放端口 如图

firewall-cmd --zone=public --list-ports

编辑配置文件 sysctl.conf

vi /etc/sysctl.conf

增加如下代码,强烈推荐使用 / 搜索名字避免重复

net.ipv4.ip_forward = 1
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.eth0.accept_redirects = 0
net.ipv4.conf.eth0.rp_filter = 0
net.ipv4.conf.eth0.send_redirects = 0
net.ipv4.conf.lo.accept_redirects = 0
net.ipv4.conf.lo.rp_filter = 0
net.ipv4.conf.lo.send_redirects = 0

ifocnfig查看网卡名称,如果不是上面eth0 以你的为准修改上面eth0为自己网卡名称,如果是多网卡还需要在多增加3行 以eth1举例如下

net.ipv4.conf.eth1.accept_redirects = 0
net.ipv4.conf.eth1.rp_filter = 0
net.ipv4.conf.eth1.send_redirects = 0

保存退出 加载内核参数使生效

sysctl -p

设置开机启动并启动ipsec

systemctl enable ipsec && systemctl start ipsec

检查配置

ipsec verify

设置开机启动并启动xl2tp

systemctl enable xl2tpd && systemctl start xl2tpd

上面说了IOS连接 PC连接需要修改注册表 以win10为例
新建文本文档 打开复制以下内容

Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\PolicyAgent]
"AssumeUDPEncapsulationContextOnSendRule"=dword:00000002

把文本文档.txt修改成vpn.reg 管理员模式运行 点确定 必须重启电脑 好了你pc可以连接上了

注: 连接上L2TP没网? 检查l2tp-ipsec.conf 是否正确,反复确认没问题还是连接不上把firewalld打开按上面说的开放firewalld对应端口

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注