// article · linux
【笔记】CentOS 7 搭建 L2TP/Ipsec
一、L2tp 环境搭建
1、先看看你的主机是否支持pptp,返回结果为yes就表示通过
modprobe ppp-compress-18 && echo yes2、是否开启了TUN,有的虚拟机主机需要开启,返回结果为cat: /dev/net/tun: File descriptor in bad state,就表示通过。
cat /dev/net/tun3、安装EPEL源(CentOS7官方源中已经去掉了xl2tpd )
yum install -y epel-release4、建立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/%any4、安装xl2tpd和libreswan(openswan已经停止维护)
yum install -y xl2tpd libreswan lsof5、编辑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 = yes6、编辑pppoptfile文件
vim /etc/ppp/options.xl2tpdipcp-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.log7、编辑ipsec配置文件
vim /etc/ipsec.conf#只修改一下选项,其他默认
protostack=netkey
dumpdir=/var/run/pluto/ #需要增加项8、编辑include的conn文件
vim /etc/ipsec.d/l2tp-ipsec.confconn 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/%any9、设置用户名密码
vim /etc/ppp/chap-secrets# Secrets for authentication using CHAP
# client server secret IP addresses
用户名1 * 密码 * #可以增加多个用户,修改后重启ipsec和xl2tpd服务
用户名2 * 密码 *
# 格式为: 用户名 类型 密码 允许访问的ip
# 这个配置文件,也是pptpd的用户密码配置文件,直接类型上用*表示所有。因为这里我们只搭建l2tp/ipsec
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 -p13、启动ipsec
systemctl enable ipsec # 设为开机启动
systemctl start ipsec # 启动服务14、检查配置
ipsec verify
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