准备

所需软件介绍:

  • openswan(ipsec):提供一个密钥
  • ppp:提供用户名和密码
  • xl2tpd:提供L2TP服务
  • sysctl:提供服务器内部转发
  • iptables:提供请求从服务器内部转向外部,外部响应转向服务器内部依赖环境

1.先检查主机是否支持pptp,返回结果为yes就表示通过。

1
modprobe ppp-compress-18 && echo yes

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

1
cat /dev/net/tun

3.更新yum源(可选)

1
2
yum install update 
yum update -y

安装

1.安装EPEL

1
yum install -y epel-release

2.安装xl2tpdlibreswan

1
yum install -y xl2tpd libreswan lsof

配置

配置ipsec

1.编辑ipsec配置文件(可选)

1
vim /etc/ipsec.conf

修改内容如下:

1
2
3
4
5
config setup
protostack=netkey
dumpdir=/var/run/pluto/
virtual_private=%v4:10.0.0.0/8,%v4:172.100.0.0/12,%v4:25.0.0.0/8,%v4:100.64.0.0/10,%v6:fd00::/8,%v6:fe80::/10
include /etc/ipsec.d/*.conf

2.编辑include的conf文件

1
vim /etc/ipsec.d/l2tp-ipsec.conf

修改内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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=xxx.xxx.xxx.xxx
leftprotoport=17/1701
right=%any
rightprotoport=17/%any

注意:修改其中的left为实际的ip,查看eth0网卡的ip地址,填写这个ip地址。

3.设置PSK

1
vim /etc/ipsec.d/default.secrets

写入如下内容:

1
: PSK "testvpn"

表示你的共享密钥为testvpn

配置PPP

PPP就是一个拨号软件,用来提供用户登录的用户名和密码验证用的。实际上,PPTP和L2TP是可以共存在一台服务器上的,而且它们还可以共享用户登录账号信息,因为它们都用PPP作为用户登录连接。

1.编辑pppoptfile文件(可选)

1
vim /etc/ppp/options.xl2tpd

修改内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
ipcp-accept-local
ipcp-accept-remote
ms-dns 8.8.8.8
ms-dns 209.244.0.3
ms-dns 208.67.222.222
name xl2tpd
#noccp
auth
crtscts
idle 1800
mtu 1410 #第一次配置不建议设置mtu,mru,否则可能789错误
mru 1410
nodefaultroute
debug
lock
proxyarp
connect-delay 5000
refuse-pap
refuse-chap
refuse-mschap
require-mschap-v2
persist
logfile /var/log/xl2tpd.log

2.设置用户名密码

1
vim /etc/ppp/chap-secrets

添加一行内容如下:

1
2
3
# Secrets for authentication using CHAP
# client server secret IP addresses
yourusername * yourpassword *

含义为:用户名[空格]service[空格]密码[空格]指定IP,*表示任意。

配置xl2tpd

编辑xl2tpd配置文件(可选)

1
vim /etc/xl2tpd/xl2tpd.conf

设置分配给客户端的地址池和本机的IP地址,修改内容如下:

1
2
3
4
5
6
7
8
9
10
11
[global]
[lns default]
ip range = 172.100.1.100-172.100.1.150 #分配给客户端的地址池
local ip = 172.100.1.1 #本机ip地址
require chap = yes
refuse pap = yes
require authentication = yes
name = LinuxVPNserver
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes

配置sysctl

sysctl的功能是开启转发。它能够将服务器内部的ip地址关系进行转发和映射,从而实现我们链接VPN之后的用户,能够通过内部的一些端口进行请求的转发。

1
vim /etc/sysctl.conf

找到下面几个选项,并把值改为我提供的值,如果没有找到某些选项,直接把它写到文件的末尾。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
net.ipv4.ip_forward=1
net.ipv4.tcp_syncookies=1
net.ipv4.icmp_echo_ignore_broadcasts=1
net.ipv4.icmp_ignore_bogus_error_responses=1
net.ipv4.conf.all.accept_source_route=0
net.ipv4.conf.all.accept_redirects=0
net.ipv4.conf.all.send_redirects=0
net.ipv4.conf.all.rp_filter=0
net.ipv4.conf.default.accept_source_route=0
net.ipv4.conf.default.accept_redirects=0
net.ipv4.conf.default.send_redirects=0
net.ipv4.conf.default.rp_filter=0
net.ipv4.conf.ens32.accept_source_route=0
net.ipv4.conf.ens32.accept_redirects=0
net.ipv4.conf.ens32.send_redirects=0
net.ipv4.conf.ens32.rp_filter=0
net.ipv4.conf.lo.accept_source_route=0
net.ipv4.conf.lo.accept_redirects=0
net.ipv4.conf.lo.send_redirects=0
net.ipv4.conf.lo.rp_filter=0

保存之后,执行:

1
sysctl -p #重新加载系统参数

或者重启网络服务:

1
systemctl restart network

配置iptables

1.以udp协议开放4500,500,1701这3个端口

1
iptables -I INPUT -p udp -m multiport --dports 500,4500,1701 -j ACCEPT

2.为配置的客户端ip池设置转发规则:

1
2
3
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth0 -j MASQUERADE
iptables -I FORWARD -s 192.168.0.0/24 -j ACCEPT
iptables -I FORWARD -d 192.168.0.0/24 -j ACCEPT

注意:这里的192.168.0.0/24设置为实际分配的客户端ip池。

保存配置并重启服务:

1
2
service iptables save
systemctl restart iptables

如果提示没有iptables服务,很可能服务器装的是firewalld而没有安装iptables,二者都可以用作防火墙,一般二选一。

执行安装命令yum install iptables-services

完成

1.启动ipsec服务

1
2
systemctl enable ipsec
systemctl restart ipsec

执行检查

1
ipsec verify

输出全为ok代表正常。

2.启动xl2tpd服务

1
2
systemctl enable xl2tpd
systemctl restart xl2tpd

FAQ

1.连接报错

服务端报错日志:initial Main Mode message received on xxx.xxx.xxx:500 but no connection has been authorized with policy PSK+IKEV1_ALLOW

修改/etc/ipsec.d/l2tp-ipsec.conf文件,将left=xx.xx.xx.xx这里改为内网ip。主要看eth0网卡的ip是什么,配置和其保持一致。

2.ios无法连接

修改/etc/ipsec.d/l2tp-ipsec.conf文件,在内容最后添加一行:sha2-truncbug=no,重启ipsec服务。

3.客户端连上后无法上网

查看iptables转发规则iptables --list -n。检查是否为连接的客户端IP设置了正确的转发规则。