【笔记】CentOS 7 搭建 L2TP/Ipsec

Linux tytrock ⋅ 于 2022-09-15 18:15:07 ⋅ 2750 阅读

一、L2tp 环境搭建

1、先看看你的主机是否支持pptp,返回结果为yes就表示通过

modprobe ppp-compress-18 && echo yes


2、是否开启了TUN,有的虚拟机主机需要开启,返回结果为cat: /dev/net/tun: File descriptor in bad state,就表示通过。

cat /dev/net/tun


3、安装EPEL源(CentOS7官方源中已经去掉了xl2tpd )

yum install -y epel-release


4、建立ipsec 与 l2tp 服务关联的配置文件

vim /etc/ipsec.d/l2tp_psk.conf,这个文件没有所以需要手动创建,输入如下内容

conn L2TP-PSK-NAT
    rightsubnet=vhost:%priv
    also=L2TP-PSK-noNAT
conn L2TP-PSK-noNAT
    authby=secret
    pfs=no
    auto=add
    keyingtries=3
    dpddelay=30
    dpdtimeout=120
    dpdaction=clear
    rekey=no
    ikelifetime=8h
    keylife=1h
    type=transport
    left=192.168.4.197   ###192.168.4.197 是自己的网卡Ip地址
    leftprotoport=17/1701
    right=%any
    rightprotoport=17/%any


4、安装xl2tpd和libreswan(openswan已经停止维护)

yum install -y xl2tpd libreswan lsof


5、编辑xl2tpd配置文件

vim /etc/ipsec.conf
[lns default]
ip range = 192.168.1.100-192.168.1.120 #分配连接客户端的地址
local ip = 192.168.1.3#本地内网IP,如果没有本地内网IP可以在外网IP上增加子接口
require chap = yes
refuse pap = yes
require authentication = yes
name = LinuxVPNserver
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes


6、编辑pppoptfile文件

vim /etc/ppp/options.xl2tpd
ipcp-accept-local
    ipcp-accept-remote
    ms-dns  114.114.114.114
    ms-dns  8.8.8.8
    # ms-wins 192.168.1.2
    # ms-wins 192.168.1.4
    name xl2tpd
    #noccp
    auth
    #crtscts
    idle 1800
    mtu 1410
    mru 1410
    nodefaultroute
    debug
    #lock
    proxyarp
    connect-delay 5000
    refuse-pap
    refuse-mschap
    require-mschap-v2
    persist
    logfile /var/log/xl2tpd.log


7、编辑ipsec配置文件

vim /etc/ipsec.conf
#只修改一下选项,其他默认
protostack=netkey
dumpdir=/var/run/pluto/ #需要增加项


8、编辑include的conn文件

vim /etc/ipsec.d/l2tp-ipsec.conf
conn L2TP-PSK-NAT
    (TAB距离)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=X.X.X.X   #外网IP!
            leftprotoport=17/1701
            right=%any
            rightprotoport=17/%any



9、设置用户名密码

vim /etc/ppp/chap-secrets
# Secrets for authentication using CHAP
# client        server  secret                  IP addresses
用户名1   * 密码 *    #可以增加多个用户,修改后重启ipsec和xl2tpd服务
用户名2   * 密码 *
# 格式为: 用户名  类型  密码  允许访问的ip
# 这个配置文件,也是pptpd的用户密码配置文件,直接类型上用*表示所有。因为这里我们只搭建l2tp/ipsec

image.png


10、设置预共享密钥PSK

vim /etc/ipsec.d/default.secrets
: PSK "MyPSK"  #MyPSK为共享密钥,客户端连接时会用到此密码


11、CentOS7 防火墙设置

firewall-cmd --permanent --add-service=ipsec      # 放行ipsec服务
firewall-cmd --permanent --add-port=1701/udp      # xl2tp 的端口,默认1701
firewall-cmd --permanent --add-port=4500/udp
firewall-cmd --permanent --add-masquerade         # 启用NAT转发功能。必须启用此功能
firewall-cmd --reload                             # 重载配置


12、修改内核参数

vim /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
#有些参数没有加是由于服务器就一块网卡,没有eth1、eth2与ppp0的配置文件
#net.ipv4.conf.ppp0.accept_redirects = 0
#net.ipv4.conf.ppp0.rp_filter = 0
#net.ipv4.conf.ppp0.send_redirects = 0

修改完成后,加载内核参数使生效

sysctl -p


13、启动ipsec

systemctl enable ipsec     # 设为开机启动
systemctl start ipsec     # 启动服务


14、检查配置

ipsec verify

image.png


15、启动xl2tp

systemctl enable xl2tpd      # 设为卡机启动
systemctl start xl2tpd      # 启动xl2tp




参考资料:

https://blog.csdn.net/weixin_45150603/article/details/97645406

https://blog.51cto.com/qiangsh/1736283?abTest=51cto


回复数量: 0
    暂无评论~~
    • 请注意单词拼写,以及中英文排版,参考此页
    • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`, 更多语法请见这里 Markdown 语法
    • 支持表情,使用方法请见 Emoji 自动补全来咯,可用的 Emoji 请见 :metal: :point_right: Emoji 列表 :star: :sparkles:
    • 上传图片, 支持拖拽和剪切板黏贴上传, 格式限制 - jpg, png, gif
    • 发布框支持本地存储功能,会在内容变更时保存,「提交」按钮点击时清空
    Ctrl+Enter