centos 6.5下搭建ipsec/xl2tpd VPN

来源:互联网 发布:瑞斯康微电子知乎 编辑:程序博客网 时间:2024/04/30 13:37
 本文安装都是直接yum安装,省事,省心。

一、安装(一条命令解决了)

   yum install openswan ppp xl2tpd

喜欢源码安装的朋友可去去 http://pkgs.org  去下载相就的源码包。

 

二、配置

1.编辑/etc/ipsec.conf

   vim /etc/ipsec.conf

   把下面xx.xxx.xxx.xxx换成你自己VPS实际的外网固定IP。其他的不动。

config setup

    nat_traversal=yes

    virtual_private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12

    oe=off

    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=xx.xxx.xxx.xxx

    leftprotoport=17/1701

    right=%any

    rightprotoport=17/%any

 


2.编辑/etc/ipsec.secrets

   vim /etc/ipsec.secrets

192.168.1.100 %any: PSK "YourPsk"

192.168.1.100换成你自己VPS实际的公网IP, YourPsk你自己定一个密钥,到时候连VPN的时候在安全设置里要用。

 

3.修改/添加 /etc/sysctl.conf

vim /etc/sysctl.conf

确保下面的字段都有,对应的值或下面一样。省事的话直接在/etc/sysctl.conf的末尾直接把下面内容的粘过去。

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.conf生效:
sysctl -p

有可以报一些关于ipv6的error,不要管它,继续下一步。

 

4.验证ipsec的运行状态

 

    service ipsec start

    ipsec verify

   执行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.32/K2.6.32-431.el6.x86_64 (netkey)

Checking for IPsec support in kernel                            [OK]

 SAref kernel support                                           [N/A]

 NETKEY:  Testing for disabled ICMP send_redirects              [OK]

NETKEY detected, testing for disabled ICMP accept_redirects     [OK]

Checking that pluto is running                                  [OK]

 Pluto listening for IKE on udp 500                             [OK]

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

Checking for 'ip' command                                       [OK]

Checking /bin/sh is not /bin/dash                               [OK]

Checking for 'iptables' command                                 [OK]

Opportunistic Encryption Support                                [DISABLED]

  如果有failed,就无法进行下去,那需要自己去百度、google找答案吧,我搭建的时候是没有遇到,记得把selinux关掉。

 

5.编辑/etc/xl2tpd/xl2tpd.conf

   vim /etc/xl2tpd/xl2tpd.conf   

    内容如下:

[global]

ipsec saref = yes

listen-addr = 120.26.115.39 ;服务器地址

[lns default]

ip range = 192.168.1.2-192.168.1.100 ;这里是VPN client的内网ip地址范围

local ip = 192.168.1.1               ;这里是VPN server的内网地址

refuse chap = yes

refuse pap = yes

require authentication = yes

ppp debug = yes

pppoptfile = /etc/ppp/options.xl2tpd

length bit = y

 

6.编辑 /etc/ppp/options.xl2tpd

    vim /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

 

7.编辑 /etc/ppp/chap-secrets

   此配置文件是设置VPN的用户名,密码:

     vim /etc/ppp/chap-secrets

     格式如下:

# Secrets for authentication using CHAP

# client        server  secret                  IP addresses

    admin         *       admin                    *

此设置表示,vpn的账户和密码都为admin,可以在任何可以上网的客机上登陆此vpn账户。

 

8.启动 xl2tpd 服务

   service xl2tpd start

 

9.开放端口及转发

1.iptables -t nat -A POSTROUTING -m policy --dir out --pol none -j MASQUERADE

2.iptables -A FORWARD -i ppp+ -p all -m state --state NEW,ESTABLISHED,RELATED    -j ACCEPT

3.iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT

4.iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth1 -j MASQUERADE

  保存规则:
service iptables save  

至此 IPsec/xl2tpd VPN 已全部配置完成,下面就可以测试了。

 

10.在win7上测试连接vpn

  打开网络共享中心--设置新的连接或网络--连接到工作区--创建vpn

  点击右下角网络图标--vpn连接--点击属性

  1.常规中的ip为vps的公网ip

  2.vpn类型:使用Ipsec 的第二层隧道协议(L2TP/IPsec)

    数据加密:需要加(如果服务器拒绝将断开连接)

    高级设置:使用预共享的密钥作身份验证

         密钥:YourPsk    (前面设置过的)

  点击确定后输入用户名和密码即可登陆。
0 0
原创粉丝点击