这几天买VPS上瘾,又到手2个XEN的VPS,由于联通把GRE协议封掉了,只好选择搭建L2TP满足所有的需求,PPTP的安装太简单,顺带就一起讲了

用是 onenetwork.net 的 XEN,1刀首月体验,以后每月5刀吧,可以的话就续租下去了。

(这里以CentOS为例,Ubuntu就用apt-get代替yum
这里预装的操作系统是CentOS 5.4,由于L2TP需要PPP设备,所以VPS必须是XEN而非OpenVZ的。

安装L2TP 需要 MPPE 和较高版本的 ppp 支持,不过一般Centos5.4 的内核已经集成了 MPPE,但不一定有ppp 。

更新软件源

32位
rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm

64位
rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/x86_64/epel-release-5-3.noarch.rpm

安装IPsec支持件Openswan,建议直接去openswan.org下载rpm安装,用yum install openswan有点小问题

http://www.openswan.org/download/binaries/centos/5/without-nss/openswan-2.6.24rc5-1.i386.rpm

http://www.openswan.org/download/binaries/centos/5/without-nss/openswan-2.6.24rc5-1.x86_64.rpm

安装openawan需要gmp协议支持,可以先用 yum install gmp 安装

然后yum install xl2tpd

没有iptables的继续安装 yum install iptables

最后在安装一个yum install lsof (打开4500 500端口需要)

软件就绪,开始配置,这里会有很多问题,我们慢慢解决。

配置IPsec部分

IPsec最后配置成功检查,下面是一个配置成功的检查,用ipsec verify查看,后面会对每一个FAILED的问题进行解决

#ipsec verify

Checking your system to see if IPsec got installed and started correctly:
Version check and ipsec on-path [OK]
Linux Openswan U2.6.24rc5/K2.6.18-164.11.1.el5xen (netkey)
Checking for IPsec support in kernel [OK]
Testing against enforced SElinux mode [OK]
NETKEY detected, testing for disabled ICMP send_redirects [OK] // 这里失败通过修改sysctl.conf解决
NETKEY detected, testing for disabled ICMP accept_redirects [OK]// 这里失败通过修改sysctl.conf解决
Checking for RSA private key (/etc/ipsec.secrets) [OK]
Checking that pluto is running [OK] // 这里失败,在提示目录下执行ipsec pluto
Pluto listening for IKE on udp 500 [OK] // 这里失败开提示,一般是没有安装lsof
Pluto listening for NAT-T on udp 4500 [OK] // 这里失败开提示,一般是没有安装lsof
Two or more interfaces found, checking IP forwarding [OK] //这里失败,是因为sysctl.conf下 net.ipv4.ip_forward = 0,要改成 1
Checking NAT and MASQUERADEing //这里失败,是因为iptables没有打开NAT
Checking for ‘ip’ command [OK]
Checking for ‘iptables’ command [OK]
Opportunistic Encryption Support [DISABLED] //这个之前可能会有一个DNS失败的提示,通过配置no_oe.conf解决

配置实例:

IPsec配置部分
vim /etc/ipsec.conf

# /etc/ipsec.conf – Openswan IPsec configuration file
#
# Manual: ipsec.conf.5
#
# Please place your own config files in /etc/ipsec.d/ ending in .conf
version 2.0 # conforms to second version of ipsec.conf specification

# basic configuration
config setup
# Debug-logging controls: “none” for (almost) none, “all” for lots.
# klipsdebug=none
# plutodebug=”control parsing”
# For Red Hat Enterprise Linux and Fedora, leave protostack=netkey
protostack=netkey
nat_traversal=yes
virtual_private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12 #一般默认这里是没有的,需要添加上,有些同学这里没有加上配置,PC端是可以连接的,手机就不行
#oe=off #关闭这个
# Enable this if you see “failed to find any available worker”
nhelpers=0

#You may put your configuration (.conf) file in the “/etc/ipsec.d/” and uncomment this.
#include /etc/ipsec.d/*.conf
# Add connections here

# sample VPN connections, see /etc/ipsec.d/examples/
include /etc/ipsec.d/l2tp-psk.conf #添加l2tp配置

#Disable Opportunistic Encryption
include /etc/ipsec.d/no_oe.conf #添加oe配置

no_oe配置

vim /etc/ipsec.d/no_oe.conf

conn block

auto=ignore

conn private

auto=ignore

conn private-or-clear

auto=ignore

conn clear-or-private

auto=ignore

conn clear

auto=ignore

conn packetdefault

auto=ignore

这个主要是解决#ipsec verify的如下错误

Opportunistic Encryption DNS checks:
Looking for TXT in forward dns zone: localhost.localdomain    [MISSING]
Does the machine have at least one non-private address?      [OK]
Looking for TXT in reverse dns zone: xx.xx.xx.xx.in-addr.arpa.    [MISSING]

l2tp-psk.conf的配置
vim /etc/ipsec.d/l2tp-psk.conf

conn L2TP-PSK-NAT
rightsubnet=vhost:%no,%priv #%no,这个参数要不要都行
#forceencaps=yes #这个参数默认是启用的,但如果连接失败抓包分析看到不停的循环在和500端口交换PSK的话就去掉它
also=L2TP-PSK-noNAT

conn L2TP-PSK-noNAT
#
# Configuration for one user with any type of IPsec/L2TP client
# including the updated Windows 2000/XP (MS KB Q818043), but
# excluding the non-updated Windows 2000/XP.
#
#
# Use a Preshared Key. Disable Perfect Forward Secrecy.
#
# PreSharedSecret needs to be specified in /etc/ipsec.secrets as
# YourIPAddress %any: “sharedsecret”
authby=secret #认证方式为PSK
pfs=no
auto=add
keyingtries=3
# we cannot rekey for %any, let client rekey
rekey=no
#type=transport #传输类型,有transport和 tunnel模式,但实际应用中没区别
#
left=1.2.3.4 #VPS的IP地址
leftnexthop=1.2.3.5 #吓一跳地址,通过route -n 可以查看 gateway地址
# or you can use: left=YourIPAddress
#
# For updated Windows 2000/XP clients,
# to support old clients as well, use leftprotoport=17/%any
leftprotoport=17/1701 #这是客户端连进来的端口
#
# The remote user.
#
right=%any
# Using the magic port of “0″ means “any one single port”. This is
# a work around required for Apple OSX clients that use a randomly
# high port, but propose “0″ instead of their port.
rightprotoport=17/%any #这里%any也可以换成0,但有些同学会遇到不停的往4500端口发312字节的报文,那么就要换成%any了

vim /etc/ipsec.secrets

#include /etc/ipsec.d/*.secrets 下面是psk的配置,主机IP 2个空格 %any: PSK “密码”,有些配置说要2个空格,我看一个空格也可以,这里的密码就是设置预共享的密钥

1.2.3.4 %any: PSK “pskpassword”

配置sysctl.conf

vim /etc/sysctl.conf

修改如下

net.ipv4.ip_forward = 1

添加以下

net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0

然后 sysctl -p 生效,解决ICMP的失败问题。

到此IPsec配置完成,然后运行 ipsec setup –restart 重新运行并用ipsec verify查看运行状态,然后排错直到全部都ok

XL2TPD配置部分

xl2tpd服务器配置

vim /etc/xl2tpd/xl2tpd.conf

[global]

listen-addr = 1.2.3.4 ;服务器地址,特别说明一下,这里的注释使用分号;

;

; requires openswan-2.5.18 or higher – Also does not yet work in combination

; with kernel mode l2tp as present in linux 2.6.23+

ipsec saref = yes ;打开这里

; forceuserspace = yes

;

; debug tunnel = yes

[lns default]

ip range = 192.168.1.128-192.168.1.254 ;客户端的IP范围

local ip = 192.168.1.99 ;设置l2tp服务器的IP

;require chap = yes ;特别说明2号,这里要注释掉,不然你会抓包发现一直往服务器的4500端口(ipsec-nat)发148字节的报文直到停止

refuse pap = yes

refuse chap = yes ;添加这行

require authentication = yes

name = l2tpd ;设置名称

ppp debug = yes

pppoptfile = /etc/ppp/options.xl2tpd

length bit = yes

options设置

vim /etc/ppp/options.xl2tpd

ipcp-accept-local

ipcp-accept-remote

ms-dns 8.8.8.8 #这样配置就成了,取消掉wins

ms-dns 8.8.4.4

noccp

auth

crtscts

idle 1800

mtu 1410

mru 1410

nodefaultroute

debug

lock

proxyarp

connect-delay 5000

配置用户密码

vim /etc/ppp/chap-secrets

# Secrets for authentication using CHAP

# client server secret IP addresses

#user servername password IP

suoluo * suoluo *

#用户名_服务名称(用*代表所有的服务l2tpd和pptpd)_密码_允许的IP(*代表所有地址)

xl2tp配置完成,使用 service xl2tpd restart 重启,一般不会有什么问题。


最后就是iptable的设置了

iptables -A INPUT -p 50 -j ACCEPT

iptables -A INPUT -p udp -d 1.2.3.4 –dport 500 -j ACCEPT

iptables -A INPUT -p udp -d 1.2.3.4 –dport 4500 -j ACCEPT

iptables -A INPUT -p udp -d 1.2.3.4 –dport 1701 -j ACCEPT

iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE

/etc/init.d/iptables save

/etc/init.d/iptables restart

然后检查启动组

chkconfig –list

把没有自启动的服务加上,比如 chkconfig xl2tpd on

至此服务器就配完了,客户端就不用我介绍了吧,太简单了。要注意的就是别忘了设置IPsec的预共享密钥。

有什么问题就联系我,或留言。

PPTPD

随带把PPTPD的一块说了,下面是下载地址,用rpm安装。

http://poptop.sourceforge.net/yum/stable/packages/pptpd-1.3.4-1.rhel5.1.i386.rpm

http://poptop.sourceforge.net/yum/stable/packages/pptpd-1.3.4-1.rhel5.1.x86_64.rpm

鉴于前面用yum install xl2tpd的时候把相关的包都安装了,一个ppp,一个libpcap。如果仅装PPTPD的请yum安装这两个包。

配置那是相当的简单,给2个配置实例看看明白

vim /etc/ppp/options.pptpd

name pptpd

refuse-pap

refuse-chap

refuse-mschap

require-mschap-v2

require-mppe-128

proxyarp

lock

nobsdcomp

novj

#nomultilink 不能多人链接

#nomp 不能多人连接一个帐户

novjccomp

nologfd

ms-dns 8.8.8.8 #这两行是要添加的

ms-dns 8.8.4.4

vim /etc/pptpd.conf

localip 172.38.39.1 #打开这两个选项设置客户端IP个pptp服务器ip

remoteip 172.38.39.8-254

配置密码,参见前文

vim /etc/ppp/chap-secrets

最后设置iptables

iptables -A INPUT -p tcp –dport 1723 -j ACCEPT

iptables -A INPUT -p tcp –dport 47 -j ACCEPT

iptables -A INPUT -p gre -j ACCEPT

iptables -t nat -A POSTROUTING -s 172.38.39.0/24 -o eth0 -j MASQUERADE

/etc/init.d/iptables save

/etc/init.d/iptables restart

chkconfig pptpd on

servcie pptpd restart

全文结束,配置有问题的时候留言。