V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
vimutt
V2EX  ›  问与答

centos 上 搭建 l2tp ,win7 下使用正常,ios 下提示 服务器未响应 请重试连接,什么原因呢

  •  
  •   vimutt · 2014-10-20 03:02:00 +08:00 via iPad · 15202 次点击
    这是一个创建于 3494 天前的主题,其中的信息可能已经有所发展或是发生改变。
    如上,系统centos6.5, openswan xl2tp iptable 。win7没有问题,ios无论怎么配置都不行。 另外有v友知道centos7下如何配置l2tp吗
    4 条回复    2015-03-15 14:09:22 +08:00
    archey
        1
    archey  
       2014-10-20 08:30:40 +08:00 via iPhone   ❤️ 2
    kkxxxxxxx
        2
    kkxxxxxxx  
       2014-10-20 10:12:55 +08:00   ❤️ 1
    用的OCSERV
    ftsm
        3
    ftsm  
       2015-01-29 09:40:27 +08:00
    @archey 哥们 可以不可以把 blackdoodle.com/2014/07/set-up-ipsec-l2tp-vpn-server-on-linode-vps-centos-7/ 这个教程在发一遍 已经失效了
    archey
        4
    archey  
       2015-03-15 14:09:22 +08:00   ❤️ 2
    1、安装ipsec

    openswan 是linux 下vpn协议ipsec的一种实现,centos 7 带的是它的开源社区分支,现在叫libreswan, 我们通过yum来安装

    yum install libreswan or yum install openswan

    修改配置文件

    1、vi /etc/ipsec.conf 文件末尾找到 include /etc/ipsec.d/*.conf 这行解注

    2、在 /etc/ipsec.d/ 目录中新建一个配置文件 l2tp_vpn.conf

    vi /etc/ipsec.d/l2tp_vpn.conf

    内容如下:


    conn L2TP-PSK-NAT
    rightsubnet=vhost:%priv
    also=L2TP-PSK-noNAT
    conn L2TP-PSK-noNAT
    authby=secret
    pfs=no
    auto=add
    type=transport
    keyingtries=3
    rekey=no
    ikelifetime=4h
    salifetime=4h
    left=106.185.46.48
    leftprotoport=17/1701
    right=%any
    rightprotoport=17/%any

    注意将YOU.SERVER.PUBLIC.IP替换成你的VPS公网地址

    配置项的说明可以参考: https://libreswan.org/man/ipsec.conf.5.html

    设置共享密钥

    cat /etc/ipsec.secrets文件内容如下:


    include /etc/ipsec.d/*.secrets

    然后在 /etc/ipsec.d/ 目录中新建一个配置文件 l2tp_vpn.secrets


    vim /etc/ipsec.d/l2tp_vpn.secrets
    内容如下:


    106.185.46.48 %any: PSK "archey"

    注意将YOU.SERVER.PUBLIC.IP替换成你的公网地址

    fuckgfw 即是共享密钥,可以替换成你自己的。

    修改包转发设置

    vi /etc/sysctl.conf , 修改以下选项的值(若没有就添加)


    net.ipv4.ip_forward = 1

    net.ipv4.conf.all.rp_filter = 0
    net.ipv4.conf.default.rp_filter = 0
    net.ipv4.conf.eth0.rp_filter = 0

    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

    启动 ipsec

    终端执行 systemctl start ipsec 来启动ipsec.
    终端执行 systemctl enable ipsec 将ipsec 加入开机启动。

    然后执行 systemctl status ipsec 可以看到状态为 Active: active (running)

    执行 ipsec verify, 输出如下:


    Verifying installed system and configuration files

    Version check and ipsec on-path [OK]
    Libreswan 3.8 (netkey) on 3.15.4-x86_64-linode45
    Checking for IPsec support in kernel [OK]
    NETKEY: Testing XFRM related proc values
    ICMP default/send_redirects [OK]
    ICMP default/accept_redirects [OK]
    XFRM larval drop [OK]
    Pluto ipsec.conf syntax [OK]
    Hardware random device [N/A]
    Two or more interfaces found, checking IP forwarding [OK]
    Checking rp_filter [OK]
    Checking that pluto is running [OK]
    Pluto listening for IKE on udp 500 [OK]
    Pluto listening for IKE/NAT-T on udp 4500 [OK]
    Pluto ipsec.secret syntax [OK]
    Checking NAT and MASQUERADEing [TEST INCOMPLETE]
    Checking 'ip' command [OK]
    Checking 'iptables' command [OK]
    Checking 'prelink' command does noted interfere with FIPSChecking for obsolete ipsec.conf options [OK]
    Opportunistic Encryption [DISABLED]

    NAT and MASQUERADEing [TEST INCOMPLETE] 不用担心,这不是说checking没完,是ipsec这个检查NAT的功能还没开发完, man ipsec verify 可以看到说明

    修改防火墙设置

    centos 7 防火墙服务用的是 firewalld。防火墙的配置方法感觉比以前更容易理解很多。

    让防火墙允许ipsec通过

    firewall-cmd --add-service=ipsec --permanent


    FirewallD is not running

    是你的防火墙还没开。 可以执行 systemctl start firewalld 开启防火墙。

    执行 systemctl status firewalld 查看状态

    执行 systemctl enable firewalld 让其开机自启 。

    之前没开启防火墙的此时记得执行 firewall-cmd --add-service=ipsec --permanent 这个哦。

    因为默认的ipsec service 只开放了500端口,如果要穿nat 需要再开放 4500端口。

    扫行 cp /lib/firewalld/services/ipsec.xml /etc/firewalld/services/ipsec.xml

    拷贝默认的ipsec配置到自定义目录下。

    然后编辑它, vi /etc/firewalld/services/ipsec.xml 加入如下行:


    <port protocol="udp" port="4500"/>

    执行 firewall-cmd --reload 生效

    可以再执行 iptables -n -L 确认一下是不是500和4500端口开启了。


    Chain IN_public_allow (1 references)
    target prot opt source destination
    ACCEPT ah -- 0.0.0.0/0 0.0.0.0/0 ctstate NEW
    ACCEPT esp -- 0.0.0.0/0 0.0.0.0/0 ctstate NEW
    ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:500 ctstate NEW
    ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:4500 ctstate NEW

    测试ipsec配置

    在客户端新建 ipsec+l2tp 的连接,填好服务器地址和共享密码,点击连接。

    在服务器端执行 tail -100f /var/log/secure 看日志,如果出现了类似


    STATE_QUICK_R2: IPsec SA established transport mode

    这样的日志就说明 ipsec 没问题了。

    在客户端 如果你用MAC OSX,也可以看 tail /var/log/system.log |grep ppp,有类似


    pppd[2056]: IPSec connection established

    说明客户端也是成功了。

    至此ipsec 配置完成。

    2、安装l2tp

    xl2tpd是l2tp的实现,centos 6 是可以通过添加 epel再yum安装的。但是笔者写此文的时候centos 7的epel源中貌似还没有这个包,所以只能比较土地编译安装了。
    也许你可以尝试 yum install xl2tpd 试试看现在是不是已经有打包了。

    或者打开 http://dl.fedoraproject.org/pub/epel/ 找找看centos 7中现在是不是加上了

    编译安装 xl2tpd

    安装gcc和依赖

    yum install gcc libpcap-devel

    从这里找到最新的release包 https://github.com/xelerance/xl2tpd/releases

    下载解压源码
    cd ~

    wget https://github.com/xelerance/xl2tpd/archive/v1.3.6.tar.gz

    tar -xvzf v1.3.6.tar.gz

    make

    make install

    配置xl2tpd


    mkdir /etc/xl2tpd/
    vi /etc/xl2tpd/xl2tpd.conf (没有的话就创建一个),内容如下:

    [global]
    listen-addr = 106.185.46.48

    auth file = /etc/ppp/chap-secrets

    port = 1701

    [lns default]
    ip range = 10.5.1.10-10.5.1.100
    local ip = 10.5.1.2
    ; leave chap unspecified for maximum compatibility with windows, iOS, etc
    ; require chap = yes
    refuse pap = yes
    require authentication = yes
    name = L2TPVPN
    ppp debug = yes
    pppoptfile = /etc/ppp/options.xl2tpd
    length bit = yes

    注意将YOU.SERVER.PUBLIC.IP替换成你的公网地址

    编辑 /etc/ppp/options.xl2tpd (没有的话就创建一个),内容如下:


    ipcp-accept-local
    ipcp-accept-remote
    #use googledns 8.8.8.8
    ms-dns 8.8.8.8
    ms-dns 8.8.4.4
    noccp
    auth
    crtscts
    idle 1800
    #tundown mtu for ios
    mtu 1356
    mru 1356
    nodefaultroute
    debug
    lock
    proxyarp
    connect-delay 5000

    最后编辑 vi /etc/ppp/chap-secrets (没有的话就创建),格式如下:

    # Secrets for authentication using CHAP
    # client server secret IP addresses
    archey * fuckgf *

    这样添加的客户帐号就是username ,密码是fuckgfw.

    修改防火墙

    执行 firewall-cmd --new-service=xl2tpd --permanent

    如果是yum 安装的可以找找看是不是 /lib/firewalld/services 下有xl2tpd的文件,有的话拷贝到 /etc/firewalld/services

    这样会在 /etc/firewalld/services/ 生成一个 xl2tpd.xml 文件。编辑


    <?xml version="1.0" encoding="utf-8"?>
    <service>
    <short>xl2tpd</short>
    <port protocol="udp" port="1701"/>
    </service>

    执行firewall-cmd --add-service=xl2tpd --permanent,添加。

    可以执行iptables -n -L 看一下 1701 udp端口是不是打开了。

    如果还没有生效的话 执行firewall-cmd --reload重新载入一下。

    执行xl2tpd -D
    输入类似如下


    xl2tpd[11944]: L2TP kernel support not detected (try modprobing l2tp_ppp and pppol2tp)
    xl2tpd[11944]: open_controlfd: Unable to open /var/run/xl2tpd/l2tp-control for reading.

    kernel support的不理它。

    后面那行需要创建一下目录 mkdir /var/run/xl2tpd

    然后客户端发起连接测试一下。

    记得帐号密码在 /etc/ppp/chap-secrets, 共享密钥在/etc/ipsec.d/l2tp_vpn.secrets哦

    xl2tpd 输出类似如下


    Call established with xx.xx.xx.xx , Local: 28828, Remote: 10057, Serial: 1

    表示连接成功了。

    现在就只差最后一步了,有点开心。

    将防火墙开启 MASQUERADE 就可以通过vpn翻墙了

    firewall-cmd --add-masquerade --permanent

    重新加载一下, firewall-cmd --reload

    OK 至此,所有的配置完成。

    在你的浏览器里 打开 http://facebook.com 试试看吧。

    it works!

    最后将xl2tpd 加到开机启动

    编辑 vi /usr/lib/systemd/system/xl2tpd.service 文件 ,内容如下。


    [Unit]
    Description=xl2tpd server daemon
    After=syslog.target network.target ipsec.service

    [Service]
    Type=simple
    PIDFile=/var/run/xl2tpd/xl2tpd.pid
    ExecStart=/usr/local/sbin/xl2tpd -p /var/run/xl2tpd/xl2tpd.pid
    Restart=on-abort
    KillMode=process

    [Install]
    WantedBy=multi-user.target

    然后执行systemctl daemon-reload

    再执行 systemctl restart xl2tpd,
    systemctl enable xl2tpd
    下一篇将介绍 搭建另一种更加稳定的vpn,通过openvpn翻墙。
    @ftsm
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1044 人在线   最高记录 6547   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:40 · PVG 03:40 · LAX 12:40 · JFK 15:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.