Ubuntu 16.04 配置 L2TP over IPSec VPN 服务器

VPN 即 Virtual Private Network(虚拟专用网),简单来说,就是在公共网络上搭建一条虚拟私有链路,可以通过该链路加入到远程的私有网络环境中。所以常用来帮助员工在办公室外安全地访问企业内部网。
创建私有链路需要使用隧道技术,用到的协议包括点对点隧道协议(PPTP)第2层隧道协议(L2TP)等。macOS 系统已经不再支持 PPTP 类型的 VPN。

一、安装软件包
sudo apt-get install strongswan xl2tpd ppp lsof

IPSec 是组建安全的 VPN 时使用的一个加密和认证标准,而 strongSwan 是一个完全支持 IKEv1 和 IKEv2 的 IKE 后台进程。

二、修改配置文件
1. 修改系统转发配置

在 /etc/sysctl.conf 文件末尾添加以下内容

net.ipv4.ip_forward = 1
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.icmp_ignore_bogus_error_responses = 1

 

启用配置

sudo sysctl -p
2. 配置 strongswan(IPSec)

在 /etc/ipsec.conf 文件末尾添加如下内容

version 2 

config setup
conn L2TP-PSK-noNAT
    authby=secret
    #shared secret. Use rsasig for certificates.

    auto=add
    #the ipsec tunnel should be started and routes created when the ipsec daemon itself starts.

    keyingtries=3
    #Only negotiate a conn. 3 times.

    ikelifetime=8h
    keylife=1h

    ike=aes256-sha1,aes128-sha1,3des-sha1

    type=transport
    #because we use l2tp as tunnel protocol

    left=%any
    # VPN 服务器的 IP 地址,'%any' 表示任意地址 

    leftprotoport=17/1701
    right=%any
    rightprotoport=17/%any

    dpddelay=10
    # Dead Peer Dectection (RFC 3706) keepalives delay
    dpdtimeout=20
    #  length of time (in seconds) we will idle without hearing either an R_U_THERE poll from our peer, or an R_U_THERE_ACK reply.
    dpdaction=clear
    # When a DPD enabled peer is declared dead, what action should be taken. clear means the eroute and SA with both be cleared.

 

配置共享密钥 /etc/ipsec.secrets

%any : PSK "PASSWORD"

%any 针对任意服务器地址,PASSWORD 需要改为足够安全的长密码

3. 配置 xl2tpd

在 /etc/xl2tpd/xl2tpd.conf 文件末尾添加如下内容

[global]
ipsec saref = yes
saref refinfo = 30

;debug avp = yes
;debug network = yes
;debug state = yes
;debug tunnel = yes

[lns default]
ip range = 192.168.100.100 - 192.168.100.200
local ip = 192.168.100.1
refuse pap = yes
require authentication = yes
;ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes

local ip 表示 VPN 虚拟网络的网关ip range 表示客户端连接 VPN 服务器时能分配到的 IP 地址
在 /etc/ppp/options.xl2tpd 文件中添加如下内容

require-mschap-v2
ms-dns 192.168.0.50
ms-dns 114.114.114.114
auth
mtu 1200
mru 1000
crtscts
hide-password
modem
name l2tpd
proxyarp
lcp-echo-interval 30
lcp-echo-failure 4

 

修改 ms-dns 为需要 vpn 客户端使用的 dns 服务器

4. 添加用户

修改 /etc/ppp/chap-secrets 文件

starky l2tpd password1 * bob l2tpd password2 *

格式为:用户名、服务、密码、限制 ip 。

以上的配置完成以后,重启服务就可以使用客户端连接了。不过此时还不能通过该 VPN 访问互联网,需要部署 IP 转发(使用 iptables )。

三、配置转发

输入下面的指令,开启 gre 协议,并打开服务器 47 和 1723 号端口。

sudo iptables -A INPUT -p gre -j ACCEPT 
sudo iptables -A INPUT -p tcp --dport 1723 -j ACCEPT   
sudo iptables -A INPUT -p tcp --dport 47 -j ACCEPT

 

开启一个 NAT 转发

sudo iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o wlp4s0 -j MASQUERADE

wlp4s0 表示当前服务器使用的网卡设备名。可以通过 ifconfig 命令查看

通过上面的指令,iptables 做了这样一件事:将所有从服务器上传出的源地址为 192.168.100.1-255 的数据包源 ip 改成服务器的 ip 。

四、连接测试

首先需要重启服务:

sudo ipsec restart
sudo service xl2tpd restart

 

然后就使用客户端连接试试吧!
若连接失败,可查看以下log:

/var/log/syslog
/var/log/auth.log

 

发表评论

7,102 次浏览