Вы находитесь на странице: 1из 6

2017/9/14 CentOS 6 下基于 strongswan 和 xl2tpd 的简易 VPN Server 安装配置

Phoenix Follow
Jul 19, 2016 · 5 min read

CentOS 6 下基于 strongswan 和 xl2tpd 的


简易 VPN Server 安装配置
配置一个基于 CentOS 的 ipsec/l2tp 协议的简易 VPN 服务。可以支持
Android、iOS、OSX、Windows 等常见操作系统的客户端。

概述
ipsec/l2tp VPN 协议由两个独立的协议组成:

ipsec, 负责点对点的数据传输加密。工作在 IP 层 l2tp, 用于实现除了数


据加密外其他 VPN 功能。

如果需要通过 VPN 访问虚拟局域网外的网络(Internet),则需要进


行 NAT 设置。

环境要求
外网 IP 地址 1 个 CentOS 6 或 7 strongswan xl2tpd pppd

软件安装

安装 EPEL 源
具体请参考: https://fedoraproject.org/wiki/EPEL

安装 strongswan

#> yum install strongswan -y

安装 xl2tpd

#> yum install xl2tpd -y

安装 pppd
https://medium.com/@elvewyn/centos-6-%E4%B8%8B%E5%9F%BA%E4%BA%8E-strongswan-%E5%92%8C-xl2tpd-%E7%9A%84%E7%AE%… 1/6
2017/9/14 CentOS 6 下基于 strongswan 和 xl2tpd 的简易 VPN Server 安装配置

安装 pppd
pppd 会随 xl2tpd 一起安装。无需单独安装
apt -y install strongswan xl2tpd
软件配置工作
配置工作包含如下几项:

* strongswan 配置 * xl2tpd 配置 * pppd 配置 * 防火墙配置 * 客户


端配置

strongswan 配置
配置文件位于 /etc/strongswan

ipsec.conf
此文件是 strongswan 的主配置文件。配置示例如下:

conn vpnserver
type=transport
authby=secret
rekey=no
keyingtries=1
left=%any
leftfirewall=yes
right=%any
leftfirewall=yes
auto=add

其中第一行的 “vpnserver” 可以修改为其它的名称

ipsec.secrets
此文件中主要配置了 VPN 服务的 “预共享密钥”, 英文名称是
“Preshared Secret Key”

: PSK “1122334455”

其中 “1122334455” 请替换为实际中的预共享密钥。

启动 strongswan

https://medium.com/@elvewyn/centos-6-%E4%B8%8B%E5%9F%BA%E4%BA%8E-strongswan-%E5%92%8C-xl2tpd-%E7%9A%84%E7%AE%… 2/6
2017/9/14 CentOS 6 下基于 strongswan 和 xl2tpd 的简易 VPN Server 安装配置

通过如下命令启动:

/etc/init.d/strongswan start

也可以通过 service、chkcon g 等命令来启动或设置自启动

防火墙配置
需要向互联网开放 UDP 协议的 500 和 4500

xl2tpd 配置
xl2tpd 负责实现具体的 VPN 交互。它的配置文件位于 /etc/xl2tpd 目
录中。 xl2tpd 自带了 DHCP 服务, 可以简化配置操作

xl2tpd.conf

[global]
port = 1701
access control = no

[lns default]
ip range = 10.17.3.2–10.17.3.255
local ip = 10.17.3.1
require authentication = yes
name = vpnserver
pppoptfile = /etc/ppp/options.xl2tpd

ip range 是 xl2tpd 内置 DHCP 服务的地址池, 可以按照需要进行修


改。 local ip 是在 vpn 网络中, vpn 服务器的 ip 地址 name 是这个服
务的名称, 无需与 strongswan 配置中的 conn 名称相同。但是会与
pppd 配置中的凭据有关联

启动 xl2tpd
通过如下命令启动:

/etc/init.d/xl2tpd start

https://medium.com/@elvewyn/centos-6-%E4%B8%8B%E5%9F%BA%E4%BA%8E-strongswan-%E5%92%8C-xl2tpd-%E7%9A%84%E7%AE%… 3/6
2017/9/14 CentOS 6 下基于 strongswan 和 xl2tpd 的简易 VPN Server 安装配置

也可以通过 service、chkcon g 等命令来启动或设置自启动

防火墙配置
需要开放 UDP 1701 端口。如果可以,建议只开放在 ipsec 协议保护下
的 1701 端口。

pppd 配置
pppd 由 xl2tpd 控制,完成具体的数据交换。其配置文件位于
/etc/ppp

options.xl2tpd

noccp
auth
crtscts
mtu 1410
mru 1410
#nodefaultroute
lock
proxyarp
silent
ms-dns 8.8.8.8
ms-dns 8.8.4.4

chap-secrets
此文件记录了登录 vpn 服务器用的用户名和密码

Secrets for authentication using CHAP


# client server secret IP addresses
dante * 123456 *
key * 44556677 *

请根据需要来添加用户。每行对应一位用户。

启动 pppd
无需启动, pppd 将会由 xl2tpd 来负责启动。每个用户登录时,都会
启动一个对应的 pppd 进程

NAT 设置
https://medium.com/@elvewyn/centos-6-%E4%B8%8B%E5%9F%BA%E4%BA%8E-strongswan-%E5%92%8C-xl2tpd-%E7%9A%84%E7%AE%… 4/6
2017/9/14 CentOS 6 下基于 strongswan 和 xl2tpd 的简易 VPN Server 安装配置

NAT 设置
默认情况下, VPN 客户机将只能访问虚拟基于网内的资源。如果希望
访问外部网络资源,则需要配置相应的 NAT 规则。

假设 VPN 的网段是: 10.17.0.0/24, 记得公网 IP 地址是


125.173.213.221/eth0, 如果用 iptables, 则需要如下规则:

-A POSTROUTING -d 10.17.0.0/24 -j MASQUERADE


-A POSTROUTING -s 10.17.0.0/24 -j MASQUERADE

同时需要内核参数 net.ipv4.ip_forward 设为 “1”

https://medium.com/@elvewyn/centos-6-%E4%B8%8B%E5%9F%BA%E4%BA%8E-strongswan-%E5%92%8C-xl2tpd-%E7%9A%84%E7%AE%… 5/6
2017/9/14 CentOS 6 下基于 strongswan 和 xl2tpd 的简易 VPN Server 安装配置

https://medium.com/@elvewyn/centos-6-%E4%B8%8B%E5%9F%BA%E4%BA%8E-strongswan-%E5%92%8C-xl2tpd-%E7%9A%84%E7%AE%… 6/6

Вам также может понравиться