最新消息:重新回归WordPress,我要比较认真的开始更新我的博客了。

linux用xl2tpd安装vpn服务的经过和经验

服务器相关 hanlei 3369浏览

在学习的道路上免不了需要花钱的时候。经过这两年断断续续自学,对linux有了一点初步的认识,然后在几天前买了一个月的VPS来练练手,这是在几年前我不敢想的事,但现在我做了。

经过两天的换系统,装软件,各种设置和百度,我的VPS终于稳定下来了。又用了两天时间装好了VPN服务,现在用刚连上网的VPN把这两天的经过记录下来。

其实像我这种菜鸟这两天的经过就是不断的百度,不断的实践。网上的资料太多,大多数都不是很详细,所有没有什么基础的话很难从中分辨出对自己有用的信息。期间我又耐下心学习了iptables的配置,因为VPN终于可以连上的时候却发现上不了网。

当然最终问题解决了,解决的方法是我在百度的时候找到了我正需要的。先发上链接,再看看要不要把内容COPY过来.

http://iqdutao.blog.51cto.com/2597934/1436671

Linux VPS CentOS 安装L2TP-VPN

第二层隧道协议L2TP(Layer 2 Tunneling Protocol)是一种工业标准的Internet隧道协议,它使用UDP的1701端口进行通信。L2TP本身并没有任何加密,但是我们可以使用IPSec对L2TP包进行加密。

L2TP VPN比PPTP VPN搭建复杂一些。

第一阶段

Openswan是Linux系统上IPsec的一个实现,我跟大家一样也用了Openswan

1、安装必备软件(基本应该都已默认安装)

# yum install make gcc gmp-devel bison flex lsof

make,gcc应该知道是什么吧

gmp-devel: Development tools for the GNU MP arbitrary precision library.

bison: A GNU general-purpose parser generator.

flex: A tool for creating scanners (text pattern recognizers).

看上去好像都和编译器有关

2、安装Openswan

依次输入以下命令,将Openswan 2.6.35安装在/tmp文件夹下

# cd /tmp

# wget http://www.openswan.org/download/openswan-2.6.35.tar.gz

(如果链接失效,请到官方网站openswan下载)

# tar -zxvf openswan-2.6.35.tar.gz

# cd openswan-2.6.35

# make programs install

3、配置IPSec

(1)备份配置文件

# cp /etc/ipsec.conf /etc/ipsec.conf.bak

(2)编辑配置文件

# vi /etc/ipsec.conf

查找protostack=auto,修改为protostack=netkey

并在文件末尾加入如下内容:

conn L2TP-PSK-NAT

rightsubnet=vhost:%priv

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=YOUR.SERVER.IP.ADDRESS

leftprotoport=17/1701

right=%any

rightprotoport=17/%any

其中一些设置的含义可以参考/etc/ipsec.d/examples/l2tp-psk.conf文件的内容。

4、设置共享密钥PSK

编辑配置文件

# vi /etc/ipsec.secrets

输入以下内容:

YOUR.SERVER.IP.ADDRESS %any: PSK “YourSharedSecret”

如:178.18.17.30 %any: PSK “123456abcdef”

(需要将YOUR.SERVER.IP.ADDRESS换成VPS的外网IP)

5、 修改/etc/sysctl.conf,开启路由功能

vi /etc/sysctl.conf

将下面两项找到:

net.ipv4.ip_forward = 0

net.ipv4.conf.default.rp_filter = 1

改为:

net.ipv4.ip_forward = 1

net.ipv4.conf.default.rp_filter = 0

之后先让修改后的配置生效,再测试一下,

sysctl -p

service ipsec start

ipsec verify

查看系统IPSec安装和启动的正确性

Checking your system to see if IPsec got installed and started correctly:

Version check and ipsec on-path ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [OK]

Linux Openswan U2.6.24/K2.6.32.16-linode28 (netkey)

Checking for IPsec support in kernel ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[OK]

NETKEY detected, testing for disabled ICMP send_redirects ? ? ? ? ? ? ? [OK]

NETKEY detected, testing for disabled ICMP accept_redirects ? ? ? ? ? ? [OK]

Checking for RSA private key (/etc/ipsec.secrets) ? ? ? ? ? ? ? ? ? ? ? [OK]

Checking that pluto is running ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[OK]

Pluto listening for IKE on udp 500 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[OK]

Pluto listening for NAT-T on udp 4500 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [OK]

Two or more interfaces found, checking IP forwarding ? ? ? ? ? ? ? ? ? ?[OK]

Checking NAT and MASQUERADEing

Checking for ‘ip’ command ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [OK]

Checking for ‘iptables’ command ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [OK]

Opportunistic Encryption Support ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [DISABLED]

这样的状态就是正常的了、已经通过了验证

如果没有问题,可以直接跳过,如果有问题,请参照下面的解决方案一个一个解决

vi /etc/sysctl.conf

可以直接在sysctl.conf最下面,加了这一段。

#added for xl2tpd

net.ipv4.ip_forward = 1

net.ipv4.conf.default.rp_filter = 0

net.ipv4.conf.all.send_redirects = 0

net.ipv4.conf.default.send_redirects = 0

net.ipv4.conf.all.log_martians = 0

net.ipv4.conf.default.log_martians = 0

net.ipv4.conf.default.accept_source_route = 0

net.ipv4.conf.all.accept_redirects = 0

net.ipv4.conf.default.accept_redirects = 0

net.ipv4.icmp_ignore_bogus_error_responses = 1

加好后别忘了再sysctl -p一下。

ipsec verify

6、重启IPSec

# /etc/init.d/ipsec restart

ipsec verify

ipsec安装好后运行ipsec verify命令发现错误的解决方案

=========================================

如果迩发现这行是WARNING的话

Checking /bin/sh is not /bin/dash ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [WARNING]

因为ubtuntu和debian系统已经使用dash来代替bash脚本执行器、虽然dash更快更方便、但在某些脚本兼容性上还是不太稳定、莪们可以将ubuntu默认的脚本执行方式改回bash来执行、执行以下命令

sudo dpkg-reconfigure dash

按英文提示、选择no、就可以把dash切换成bash、再执行ipsec verify就不会出现WARNING的提示了

=========================================

Pluto listening for IKE on udp 500 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[FAILED]

Cannot execute command “lsof -i UDP:500”: No such file or directory

Pluto listening for NAT-T on udp 4500 ? ? ? ? ? ? ? ? ? ? ? ? ? [FAILED]

Cannot execute command “lsof -i UDP:4500”: No such file or directory

如果出现以上的问题、就是没安装lsof的命令啦、安装就可以了

apt-get install lsof

=========================================

checking for IPsec support in kernel ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[FAILED]

whack:Pluto is not running(no “/var/run/pluto/pluto.ctl”) ? ? ? ?[FAILED]

如果出现的是这个问题,有三个可以尝试的解决方案,因为出现这个问题的原因不是一个问题造成的。

1)修改/etc/ipsec.conf文件,加进去一句话,version 2.0。(说实话,这有可能,但是一般openswan2.6.X的版本创建ipsec.conf的时候都会自动有2.0的说明)

2)执行ipsec setup start。如果执行完之后你再去看ipsec verify的结果,FAILED选项消失了,恭喜你,你没有遇到令人厌恶的第三种情况。

解释一下出现第二种情况的原因:ipsec 如果没有启动,它是暂时不会知道自己将会使用哪种内核栈的,有三种KLIPS,netkey和no stack。选择的依据是你的ipsec.conf文件里的protostack后边的选项,如果是auto,那么首选netkey,然后是KLIPS。否则就是protostack的结果。

如果还是出现其他问题,自行google搜索解决!

以上为第一阶段,可以正常安装完成没有问题后,进行第二阶段的安装

第二阶段

1、下载编译安装

cd /usr/src

wget http://downloads.sourceforge.net/project/rp-l2tp/rp-l2tp/0.4/rp-l2tp-0.4.tar.gz

tar zxvf rp-l2tp-0.4.tar.gz

cd rp-l2tp-0.4

./configure

make

cp handlers/l2tp-control /usr/local/sbin/

mkdir /var/run/xl2tpd/

ln -s /usr/local/sbin/l2tp-control /var/run/xl2tpd/l2tp-control

cd /usr/src

wget ?http://www.xelerance.com/software/xl2tpd/xl2tpd-1.2.4.tar.gz

tar zxvf xl2tpd-1.2.4.tar.gz

cd xl2tpd-1.2.4

make install

(如果链接失效,请到寻找新的连接地址进行替换下载)

2、修改相应的配置

mkdir /etc/xl2tpd

vi /etc/xl2tpd/xl2tpd.conf

内容,注意 ip range 不要和你的 lan ip 冲突。。。

[global]

ipsec saref = yes

[lns default]

ip range = 10.1.2.2-10.1.2.254

local ip = 10.1.2.1

#连接vpn后,本地获取到的虚拟vpn地址

refuse chap = yes

refuse pap = yes

require authentication = yes

ppp debug = yes

pppoptfile = /etc/ppp/options.xl2tpd

length bit = yes

3、修改配置

ppp 配置文件

vi /etc/ppp/options.xl2tpd

内容

require-mschap-v2

ms-dns 8.8.8.8

ms-dns 8.8.4.4

asyncmap 0

auth

crtscts

lock

hide-password

modem

debug

name l2tpd

proxyarp

lcp-echo-interval 30

lcp-echo-failure 4

4、设置拨号用户名以及密码

vi /etc/ppp/chap-secrets

内容格式

# user ? ? ?server ? ? ?password ? ? ? ? ? ?ip

username ? ? * ? ? ?userpass ? ? ? ?*

比如

vpn * password *

===========================================

以上为第二阶段的配置

配置完成后,命令

Xl2tpd –D

启用

[root@localhost ~]# xl2tpd -D

xl2tpd[9637]: Enabling IPsec SAref processing for L2TP transport mode SAs

xl2tpd[9637]: IPsec SAref does not work with L2TP kernel mode yet, enabling forceuserspace=yes

xl2tpd[9637]: setsockopt recvref[22]: Protocol not available

xl2tpd[9637]: This binary does not support kernel L2TP.

xl2tpd[9637]: xl2tpd version xl2tpd-1.2.4 started on localhost.localdomain PID:9637

xl2tpd[9637]: Written by Mark Spencer, Copyright (C) 1998, Adtran, Inc.

xl2tpd[9637]: Forked by Scott Balmos and David Stipp, (C) 2001

xl2tpd[9637]: Inherited by Jeff McAdams, (C) 2002

xl2tpd[9637]: Forked again by Xelerance (www.xelerance.com) (C) 2006

xl2tpd[9637]: Listening on IP address 61.187.98.31, port 1701

cat /var/log/secure

类似于这样,为正常情况

root@panshan:~# xl2tpd -D

xl2tpd[880]: Enabling IPsec SAref processing for L2TP transport mode SAs

xl2tpd[880]: IPsec SAref does not work with L2TP kernel mode yet, enabling forceuserspace=yes

xl2tpd[880]: init_network: Unable to bind socket: Address already in use. Terminating.

此问题显示为你的xl2tpd已经已经启动,不用处理

做完上面这堆步骤之后,客户端建个连接就可以验证进入vpn主机了。但是无法访问内外网。

正常启动后, vpn可以正常连接了,但是不能访问外网,需要进行第三阶段配置

第三阶段

最关键的iptables配置

在linux系统中添加以下规则

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

iptables -I FORWARD -s 192.168.7.0/24 -j ACCEPT

iptables -I FORWARD -d 192.168.7.0/24 -j ACCEPT

iptables -A FORWARD -j REJECT –reject-with icmp-host-prohibited

iptables -A INPUT -m state –state RELATED,ESTABLISHED -j ACCEPT

iptables -A INPUT -p icmp -j ACCEPT

iptables -A INPUT -i lo -j ACCEPT

iptables -A INPUT -p tcp -m state –state NEW -m tcp –dport 22 -j ACCEPT

iptables -A INPUT -p tcp -m state –state NEW -m tcp –dport 80 -j ACCEPT

iptables -A INPUT -p tcp -m state –state NEW -m tcp –dport 53 -j ACCEPT

iptables -A INPUT -p udp -m state –state NEW -m udp –dport 53 -j ACCEPT

iptables -A INPUT -p tcp -m state –state NEW -m tcp –dport 1194 -j ACCEPT

iptables -A INPUT -p udp -m state –state NEW -m udp –dport 1701 -j ACCEPT

iptables -A INPUT -p udp -m state –state NEW -m udp –dport 500 -j ACCEPT

iptables -A INPUT -p udp -m state –state NEW -m udp –dport 4500 -j ACCEPT

(根据本地的地址和配置分配的ip地址相应修改)

然后保存、重启iptables

Service ?iptables ?save

iptables-save

service ? iptables ? restart

然后查看iptables 看是否已经配置进去

vi /etc/sysconfig/iptables

下面filter表里,先把VPN要用到的udp端口1701,500,4500都打开。要用openvp的话,还要开1194。

另外filter表里,一定要有FORWARD规则。

#下面规则做参考啊,新手别完全照抄。

*nat

:PREROUTING ACCEPT [39:3503]

:POSTROUTING ACCEPT [0:0]

:OUTPUT ACCEPT [0:0]

-A POSTROUTING -s 192.168.7.0/24 -o eth0 -j MASQUERADE

COMMIT

# Completed on Thu Jun 28 15:50:40 2012

# Generated by iptables-save v1.4.7 on Thu Jun 28 15:50:40 2012

*filter

:INPUT ACCEPT [0:0]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [121:13264]

-A INPUT -m state –state RELATED,ESTABLISHED -j ACCEPT

-A INPUT -p icmp -j ACCEPT

-A INPUT -i lo -j ACCEPT

-A INPUT -p tcp -m state –state NEW -m tcp –dport 22 -j ACCEPT

-A INPUT -p tcp -m state –state NEW -m tcp –dport 80 -j ACCEPT

-A INPUT -p tcp -m state –state NEW -m tcp –dport 53 -j ACCEPT

-A INPUT -p udp -m state –state NEW -m udp –dport 53 -j ACCEPT

-A INPUT -p tcp -m state –state NEW -m tcp –dport 1194 -j ACCEPT

-A INPUT -p udp -m state –state NEW -m udp –dport 1701 -j ACCEPT

-A INPUT -p udp -m state –state NEW -m udp –dport 500 -j ACCEPT

-A INPUT -p udp -m state –state NEW -m udp –dport 4500 -j ACCEPT

-A INPUT -j REJECT –reject-with icmp-host-prohibited

-A FORWARD -d 192.168.7.0/24 -j ACCEPT

-A FORWARD -s 192.168.7.0/24 -j ACCEPT

-A FORWARD -j REJECT –reject-with icmp-host-prohibited

COMMIT

# Completed on Thu Jun 28 15:50:40 2012

之后已经配置完成,可以用本机尝试连接

window连接前,先处理

打开 ?IPSEC services ?开始-运行-输入services.msc,然后在服务中启用“IPSEC services”

 

linux系统中L2TP—VPN的配置安装(二)

如果遇到其他问题,一直解决不了,并且是centos系统的,请参考安装包

一、获取安装脚本

# wget ?http://soft.kwx.gd/vpn/l2tp_centos.sh

# sh ?./l2tp_centos.sh

执行以上命令后如下图:

wKioL1O-IVry-S0OAABVzj_uKek943.jpg

1、intall VPN service

全新安装L2TP VPN。

2、repaire VPN service

修复L2TP VPN 服务。

3、add VPN user

新建VPN帐号。

二、安装L2TP VPN 一键包

首次安装选择1即可。回车以后则会自动下载并编译安装。

安装完毕后,如下图。

wKiom1O-IYrjbKyiAACHDHS1DDU832.jpg

默认帐号(VPN username)为vpn,密码(VPN password)是系统随机生成的。

三、修复L2TP VPN 一键包

若出现问题,可在运行sh ./l2tp_centos.sh后,选择2修复。建议修复完成以后重启一次VPS。

wKiom1O-IYuDJQEDAACxSc3BQfE213.jpg

四、新建用户

若要添加VPN用户,可在运行sh ./l2tp_centos.sh后,选择3添加。

wKioL1O-IVyxaKJwAAC50jab3l8691.jpg

如上图。

input user name(填入VPN帐户名)。

input password(填入VPN密码)。

最后回车即可添加成功。

会显示安装成功,并且显示随机生成的用户名、密码

转载请注明:HANLEI'BLOG » linux用xl2tpd安装vpn服务的经过和经验